assembly - NASM assembled bootloader memory issue -


i writing bootloader nasm. @ moment designed output welcome string, record keystrokes while displaying them, printing stored keystrokes upon finding enter key, , halting.

                bits       16                 org        0x7c00 start:          jmp        main  bgetkey:        pusha                 mov        ax, 0                 mov        ah, 10h                 int        16h                 mov        [.buf], ax                 popa                 mov        ax, [.buf]                 ret                 .buf       dw 0  prints:         mov        ah, 0x0e                 mov        al, [si]                 cmp        al, 0                 jz         print_end                 mov        bh, 0x00                 mov        bl, 0x07                 int        0x10                 inc        si                 jmp        prints print_end:      ret  main:           mov        ax, 0x0000           ; set register                 mov        ds, ax               ;                  mov        bx, mem                 add        bx, word 1                 mov        word [mem], bx                 mov        si, welcome          ; set , prints                 call       prints               ;  type:           mov        si, qbuf             ; set prints ptr                 call       bgetkey              ; capture  input                 mov        [qbuf], al           ; set char sz                 call       prints               ; call print str                  mov        bx, [mem]            ; put chr in mem                 cmp        bx, stop             ; compare loader                 je         oom                  ; end , memory                 mov        byte [bx], al                 add        bx, byte 1                 mov        [mem], bx            ;                   cmp        byte [qbuf], 0x0d    ; cmpr enter key                 jne        type                 ; continue  next                 mov        si, newline          ; print  newline                 call       prints               ;                   mov        bx, mem printmem:       cmp        byte [bx], 0x00      ; check 0                 je         halt                 ; halt   cpu                 mov        cl, [bx]                 mov        byte [qbuf], cl      ; buffer , cpy                 mov        si, qbuf             ; pointer  si                 call       prints               ; print char                 inc        bx                 jmp        printmem             ; jump beginning  oom:            mov        si, outomem          ; no more memory                 call       prints               ; print  message  halt:           mov        si, halting          ; cpu halting                 call       prints               ; print last msg                 hlt                             ; halt   cpu                  welcome db "bootloader", 0x0a, 0x0d, 0x00                 newline db 0x0a, 0x00                 outomem db "out of memory", 0x0a, 0x0d, 0x00                 halting db "halting", 0x00                 qbuf       dw 0, 0                 mem        db 0  times 0200h - 2 - ($ - $$)db 0                 stop       dw 0xaa55 

the program not functioning desired. ceaselessly prints same character after enter pressed. how error corrected?

the immediate problem prints destroys bx (because sets bl , bh) printmem loop requires bx preserved blows up.

however, destroys al input loop won't storing correct value in memory start with, either.

furthermore, while want mem pointer mem + 2, pointer mem + 1 overwrite pointer input. also, start printing mem , not mem + 2.

finally, input not terminated 0 checking for, it's terminated 0x0d (the enter).

a working version be:

                bits       16                 org        0x7c00 start:          jmp        main  bgetkey:        pusha                 mov        ax, 0                 mov        ah, 10h                 int        16h                 mov        [.buf], ax                 popa                 mov        ax, [.buf]                 ret                 .buf       dw 0  prints:         pusha .loop:                 mov        ah, 0x0e                 mov        al, [si]                 cmp        al, 0                 jz         print_end                 mov        bh, 0x00                 mov        bl, 0x07                 int        0x10                 inc        si                 jmp        .loop print_end:      popa                 ret  main:           mov        ax, 0x0000           ; set register                 mov        ds, ax               ;                 mov        bx, mem                 add        bx, word 2           ; point after pointer :)                 mov        word [mem], bx                 mov        si, welcome          ; set , prints                 call       prints               ; type:           mov        si, qbuf             ; set prints ptr                 call       bgetkey              ; capture  input                 mov        [qbuf], al           ; set char sz                 call       prints               ; call print str                  mov        bx, [mem]            ; put chr in mem                 cmp        bx, stop             ; compare loader                 je         oom                  ; end , memory                 mov        byte [bx], al                 add        bx, byte 1                 mov        [mem], bx            ;                  cmp        byte [qbuf], 0x0d    ; cmpr enter key                 jne        type                 ; continue  next                 mov        si, newline          ; print  newline                 call       prints               ;                  mov        bx, mem+2            ; start after pointer printmem:       cmp        byte [bx], 0x0d      ; check end                 je         halt                 ; halt   cpu                 mov        cl, [bx]                 mov        byte [qbuf], cl      ; buffer , cpy                 mov        si, qbuf             ; pointer  si                 call       prints               ; print char                 inc        bx                 jmp        printmem             ; jump beginning  oom:            mov        si, outomem          ; no more memory                 call       prints               ; print  message  halt:           mov        si, halting          ; cpu halting                 call       prints               ; print last msg                 hlt                             ; halt   cpu                  welcome db "bootloader", 0x0a, 0x0d, 0x00                 newline db 0x0a, 0x00                 outomem db "out of memory", 0x0a, 0x0d, 0x00                 halting db "halting", 0x00                 qbuf       dw 0, 0                 mem        db 0  times 0200h - 2 - ($ - $$)db 0                 stop       dw 0xaa55 

ps: learn use debugger.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -