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 :)