assembly - 字符串打印机不打印换行符

标签 assembly x86 nasm x86-16 bootloader

我正在尝试编写一个简单的操作系统,但在将十六进制数字转换为字符串时遇到问题。我编写了一个字符串打印机,如果我在某个预定义的字符串上调用它,它就可以正常工作。但是,将十六进制数字转换为字符串后,它会打印该数字,但下一次调用打印机会在同一行上打印所有内容,在某些设置中甚至会打印奇怪的字符。

我使用 NASM 生成代码并使用 Bochs 模拟 x86 CPU。

[org 0x7c00] ;tell nasm add the adress, specified by org to the relative adresses of the code


mov bx, helloMsg
call stringPrinter
mov dx, 0xde3f
call hexPrinter
mov bx, byeMsg
call stringPrinter
mov bx, newLine
call stringPrinter
mov bx, testMsg
call stringPrinter
jmp end


%include "src/asm/printer.asm"
%include "src/asm/hexParser.asm"

helloMsg:
    db 'Hello, World!', 0xa, 0xd , 0
byeMsg:
    db 'Bye Bye!', 0xa, 0xd, 0

testMsg:
    db 'this is a test', 0xa, 0xd, 0

newLine:
    db 0xa, 0xd

end:
jmp $

times 510-($-$$) db 0 ; pad with zeros until the 510th byte of the sector is reached

dw 0xaa55



stringPrinter:
    pusha
    mov ah,0x0e ; tele output
    start:
        mov al, [bx]
        cmp al, 0x0
        je stop
        int 0x10 ; interupt 10 for screen printing
        inc bx
        jmp start
    stop:
    popa
    ret

charPrinter:
    push ax
    mov ah,0x0e ; tele output
    mov al, bl
    int 0x10
    mov al, 0xa
    int 0x10
    mov al, 0xd
    int 0x10
    pop ax
    ret

hexPrinter:
    pusha
    call hexToString
    mov bx, ax
    call stringPrinter
    popa
    ret



hexToString:
    push bx
    push dx
    mov bx, output
    mov ax, dx
    shr ax, 12
    call determineValue
    add bx, 2
    mov [bx], ax

    mov ax, dx
    and ax, 0x0f00
    shr ax, 8
    call determineValue
    inc bx
    mov [bx], ax

    mov ax, dx
    and ax, 0x00f0
    shr ax, 4
    call determineValue
    inc bx
    mov [bx], ax

    mov ax, dx
    and ax, 0x000f
    call determineValue
    inc bx
    mov [bx], ax

    mov ax, output
    pop dx
    pop bx
    ret

determineValue:
    cmp ax, 0x0
    je zeroDigit
    cmp ax, 0x1
    je oneDigit
    cmp ax, 0x2
    je twoDigit
    cmp ax, 0x3
    je threeDigit
    cmp ax, 0x4
    je fourDigit
    cmp ax, 0x5
    je fiveDigit
    cmp ax, 0x6
    je sixDigit
    cmp ax, 0x7
    je sevenDigit
    cmp ax, 0x8
    je eightDigit
    cmp ax, 0x9
    je nineDigit
    cmp ax, 0xa
    je aDigit
    cmp ax, 0xb
    je bDigit
    cmp ax, 0xc
    je cDigit
    cmp ax, 0xd
    je dDigit
    cmp ax, 0xe
    je eDigit
    cmp ax, 0xf
    je fDigit

    zeroDigit:
        mov ax, '0'
        ret
    oneDigit:
        mov ax, '1'
        ret
    twoDigit:
        mov ax, '2'
        ret
    threeDigit:
        mov ax, '3'
        ret
    fourDigit:
        mov ax, '4'
        ret
    fiveDigit:
        mov ax, '5'
        ret
    sixDigit:
        mov ax, '6'
        ret
    sevenDigit:
        mov ax, '7'
        ret
    eightDigit:
        mov ax, '8'
        ret
    nineDigit:
        mov ax, '9'
        ret
    aDigit:
        mov ax, 'a'
        ret
    bDigit:
        mov ax, 'b'
        ret
    cDigit:
        mov ax, 'c'
        ret
    dDigit:
        mov ax, 'd'
        ret
    eDigit:
        mov ax, 'e'
        ret
    fDigit:
        mov ax, 'f'
        ret

output:
    db '0x0000' , 0

最佳答案

正如评论中提到的,您应该以零终止 newLine 消息。

The number and the bye-message are now on the same line and I don't understand why this is happening.

您需要输出换行符才能将下一条消息放在另一行上。在调用 hexPrinter 之后使用 newLine 添加对 stringPrinter 的调用,如下所示:

mov dx, 0xde3f
call hexPrinter
mov bx, newLine
call stringPrinter

或者更改 hexPrinter 的 output 变量以包含换行符,如下所示:

output:
    db '0x0000', 13, 10, 0

关于assembly - 字符串打印机不打印换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56875268/

相关文章:

assembly - NASM 汇编器 - 生成的机器代码中不需要的 66

linux - Cygwin:兼容性问题

linux -/usr/bin/ld : skipping incompatible foo. 所以在搜索 foo 时

assembly - NASM - JBE 指令出现段错误

计算C文件代码部分的大小

macos - osx x64 反向 tcp shell 代码程序成功终止

assembly - 来自 FS (MASM) 的 LODS

c++ - 忙等待现代处理器的优缺点

汇编语言 - 如何做模?

c++ - 返回 std::pair 与通过非常量引用传递