Chris
Chris

Reputation: 31

String printer doesn't print newlines

I am trying to write a simple OS and I'm having trouble converting hexnumbers to strings. I wrote a stringprinter and if I call it on some predefined string it works fine. But after transforming a hexnumber into a string it prints the number but a next call to the printer prints everything on the same line and in some setup even weird characters.

I am using NASM to generate the code and Bochs to emulate a 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

Upvotes: 2

Views: 70

Answers (1)

ecm
ecm

Reputation: 2763

As mentioned in the comments, you should zero-terminate the newLine message.

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

You need to output a linebreak to have the next message on another line. Either add a call to stringPrinter with newLine after the call to hexPrinter, like this:

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

Or change the output variable of hexPrinter to include a linebreak, like so:

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

Upvotes: 2

Related Questions