【问题标题】:NASM with Bochs - Assembly - Printing a stringNASM with Bochs - 组装 - 打印字符串
【发布时间】:2017-07-14 21:56:38
【问题描述】:

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

问题:

; 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

    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

;
; 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 配置:

# 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 的新手。)

【问题讨论】:

  • 你忘了说问题是什么......另外,bochs有一个内置的调试器,确保你使用它。 PS:pusha; pop bx 是不好的做法,可能不会做你认为它做的事情(当然你没有写评论你想要做什么)
  • 我去看看调试器,谢谢你的建议。在做pusha; pop bx 时,我打算将所有变量推入堆栈,只取出 bx。我将尝试推入除 bx 以外的所有 var,并在完成后将它们弹出。
  • pop bx 不会从推送的那些中搜索出bx 的原始值,它只是从堆栈中删除最顶部的项目。由于pusha 的工作方式,这不会是bx。反正pusha不会改变bx,值还在所以你不需要pop

标签: assembly nasm boot bochs


【解决方案1】:

str_out少了一条RET指令,但是少了显示是因为你垃圾啊

mov ah, 0x0e    ; BIOS teletyping for 0x10 interrupt
mov ax, 0x00    ;prep counter

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

            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

不完全确定在修改寄存器时确保函数小心是什么意思,但这个例子确实有效。由于这本质上是您的代码的副本,您需要做的就是记录它,但您可能会被问到两个特质。你为什么使用这些指令,为什么它们是按这个顺序排列的。

【讨论】:

  • 非常感谢您的回复。我不太清楚你所说的垃圾是什么意思,你的意思是 AX 吗? (经过测试,是的!我将在继续之前阅读寄存器,我认为小心的意思是不要使用你不理解的寄存器......)另外,我注意到你包括代码中的函数。我想这在某种程度上是一个优先的事情(以及方便/可访问性与一个二进制文件),就像我们在格式上的差异一样。再次感谢您的帮助。
  • @Kohrokho 是的,在这种情况下,垃圾处理意味着您设置 AH=14,然后在下一条指令中将其删除。虽然我同意使用%include,但我会推荐格式化风格,因为一旦你达到了拥有数千行代码的地步,它确实会有所作为。没有争论,不管你怎么做,如果结果是没有错误的代码并且你很容易维护,那么不管你怎么做,这一切都很好。
猜你喜欢
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 2019-04-23
  • 2011-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多