shadeos/learning/old_src/print_hex.asm

38 lines
835 B
NASM

; data in dx
print_hex:
pusha
mov cx, 0 ; index var
hex_loop:
cmp cx, 4 ; loop 4 times
je end
; 1) convert last char of 'dx' to ascii
mov ax, dx ; ax is working register
and ax, 0x000f ; 0x1234 -> 0x0004
add al, 0x30 ; add 0x30 to N to convert it to ascii "n"
cmp al, 0x39 ; if > 9, add extra 8 to represent a-f
jle step2
add al, 7 ; A is ascii 64 instead of 58, 65-58 = 7
step2:
; 2) get correct pos of string to place char
; bx <- base address + string length - index
mov bx, HEX_OUT + 5
sub bx, cx ; cx: index
mov [bx], al ; copy ascii char on al to pos pointed by bx
ror dx, 4 ; 1234 - 4123 - 3412 - 2341 - 1234
add cx, 1
jmp hex_loop
end:
mov bx, HEX_OUT
call print
popa
ret
HEX_OUT:
db '0x0000', 0 ; reserve memory for string