0

I am trying to write 16-bit code to print a hexadecimal value as it passed to dx register. For simplicity on this stage I assuming that passed values contains only digits 0-9, and currently I am trying to use div instruction, that just hangs up processor or something. It leaves just blinking cursor. The main idea as were proposed over the network is to divide number by 16 and to add some value to remainder for further printing.

my print_hex.asm function code currently look like this:

; Print string function, note that this code written after jmp $ instruction,
; otherwise the string were printed twice
print_hex:
    push ax     ; pushing registers that used onto a stack
    push dx

    print_hex_loop:
        mov dx, 16
        mov ax, 16
        div ax ; i think after this instruction execution stops somehow
        mov ah, 0x0e    ; scrolling teletype BIOS routine
        add dx, 48
        mov al, dl
        int 0x10        ; print what in al
        ;cmp dx, 0
        ;jnz print_hex_loop 

    mov al, 0x0a    ; line feed and carriage return added after printed number
    int 0x10
    mov al, 0x0d
    int 0x10

    pop dx ; popping registers that used from stack
    pop ax
ret

this code called within this file:

[org 0x7c00]

mov bp , 0x8000
mov sp , bp

mov dx, 0x7c00
call print_hex

jmp $

%include "print_hex.asm"

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

How to use div correctly? Here I was expecting, that value 16 in dx will be divided by value 16 in ax, and that remainder will be in ax after instruction div (according to https://en.wikibooks.org/wiki/X86_Assembly/Arithmetic), but something went wrong :)

stackoverflower
  • 395
  • 4
  • 19
  • 1
    You were probably confused by the notation `dx:ax` which is not a division. It means "32 bit number formed from dx and ax with dx the top 16 bits and ax the low 16". But to divide by powers of 2 (and 16 is one) you use shifts and masks. See also [How to convert a number to hex?](https://stackoverflow.com/a/53823757/547981) – Jester Dec 04 '19 at 20:58
  • Okay, sounds like I misunderstood the div instruction – stackoverflower Dec 04 '19 at 21:01
  • 1
    [See here for details about div](https://stackoverflow.com/a/38416896/547981) – Jester Dec 04 '19 at 21:02

0 Answers0