首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NASM with Bochs - Assembly - string a string

NASM with Bochs - Assembly - string a string
EN

Stack Overflow用户
提问于 2017-07-15 05:56:39
回答 1查看 452关注 0票数 1

我想打印一个字符串,并且使用NASM Assembly,Bochs来运行程序,并且有两个简单的文件。我正在做一个非常简单的引导扇区来开始学习汇编。我正在尝试自学,并且正在使用这个PDF:https://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-dev.pdf我正在尝试创建我自己的字符串打印函数。

问题:

代码语言:javascript
复制
; Question 4
; Put together all of the ideas in this section to make a self-contained function for printing
; null-terminated strings, that can be used as follows:
;
; A boot sector that prints a string using our function.
;
[org 0x7c00] ; Tell the assembler where this code will be loaded
mov bx, HELLO_MSG ; Use BX as a parameter to our function , so
call print_string ; we can specify the address of a string.
mov bx, GOODBYE_MSG
call print_string
jmp $ ; Hang
%include "print_string.asm"
; Data
HELLO_MSG:
db ’Hello , World!’, 0 ; <-- The zero on the end tells our routine
; when to stop printing characters.
GOODBYE_MSG:
db ’Goodbye!’, 0
; Padding and magic number.
times 510-($-$$) db 0
dw 0xaa55
; For good marks, make sure the function is careful when modifying registers and that
; you fully comment the code to demonstrate your understanding.

我的代码:

bootsect.asm

代码语言:javascript
复制
    org 0x7c00 ; Start at boot sector. Allows us to add offset for labels efficiently


    mov bx, loading_sys_msg
    calltest:
    call str_out
    ;jmp calltest

    jmp $ ; Jump forever. Because end of program lol

; Includes
    %include "str_out.asm"

; Database Resources

    loading_sys_msg:
    db 'Loading OIK v0.0.1', 0

; Padding and magic BIOS number.

times 510-($-$$) db 0
dw 0xaa55
`

str_out.asm

代码语言:javascript
复制
;
; String-printing function
;
str_out:
pusha
pop bx
;add bx, 0x7c00 ; Adds current address if boot sect and no org called (not used)
mov ah, 0x0e    ; BIOS teletyping for 0x10 interrupt
mov ax, 0x00    ;prep counter
mov al, [bx]    ; character to print placed in al (bx address contents)
prnt:                   ; printing loop
int 0x10            ; interrupt print
add ax, 0x01    ; add to counter for removal afterwards
add bx, 0x01    ; move bx address forward by 1
mov al, [bx]    ; character to print placed in al (bx address contents)
cmp al, 0           ; Compare [al -?- 0]
jg prnt             ; If greater, jump to print
sub bx, ax      ;remove the counter amount
;sub bx, 0x7c00 ;remove address addition if used earlier (not used)
push bx
popa

Bochs配置:

代码语言:javascript
复制
# Tell bochs to use our boot sector code as though it were
# a floppy disk inserted into a computer at boot time.
floppya: 1_44=boot_sect.bin, status=inserted
boot: a

当Bochs启动时,屏幕会消失,不会有任何反应。我做错了什么?

(感谢Jester告诉我,我没有说明问题。我还不熟悉Stack Overflow。)

EN

回答 1

Stack Overflow用户

发布于 2017-07-15 08:50:31

str_out缺少一条RET指令,但缺少显示是因为您丢弃了AH

代码语言:javascript
复制
mov ah, 0x0e    ; BIOS teletyping for 0x10 interrupt
mov ax, 0x00    ;prep counter

通常我不会给出练习的例子,但在这种情况下,你已经做出了合理的努力,你可以理解你的逻辑。

代码语言:javascript
复制
            org     0x7c00

            mov     bx, Msg
            call    str_out
            hlt
            jmp     $ - 1

str_out:    mov     ah, 0x0e
prnt:       mov     al, [bx]
            int     0x10
            add     bx, 0x01
            cmp     al, 0
            jg      prnt
            ret

  Msg:      db      'Loading OIK v0.0.1', 0

    times 510-($-$$) db 0
        dw  0xaa55

不能确切地确定是什么意思请确保函数在修改寄存器时非常小心,但此示例确实有效。由于这本质上是代码的副本,您所需要做的就是对其进行文档记录,但可能会询问您两个特性。您为什么要使用这些指令?为什么它们是按该顺序排列的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45112137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档