I am trying to write an x86 assembly code for NASM assembler, which will convert a hexadecimal number into a string and print it. For simplicity I have assumed that my hexadecimal number will only contain digits(eg. 0x1234). Here is the code:
print_hex.asm
[org 0x7c00]
mov dx, 0x1234
call print_hex
jmp $
print_hex:
push bx
push cx
push dx
push ax
mov bx, HEX_STR
mov cx, 0x000f
mov ax, 0x0000 ; length counter
loop:
push bx ; save bx
and cx, dx ; 0xabcd & 0x000f
add bx, 5 ; find position in the template
sub bx, ax
add [bx], cl
pop bx
shr dx, 4 ; next digit
mov cx, 0x000f
inc ax ; increment counter
cmp ax, 4
jl loop ; loop through the digits
pop ax
call print_string
pop dx
pop cx
pop bx
ret
%include "print_string.asm"
HEX_STR: ; template string
db '0x0000', 0
times 510-($-$$) db 0
dw 0xaa55
print_string.asm
print_string:
mov ah, 0x0e
push bx
loop:
cmp BYTE [bx], 0
je end
mov al, [bx]
int 0x10
inc bx
jmp loop
end:
pop bx
ret
If I try to assemble/compile print_hex.asm
with NASM, it gives following error:
print_hex.asm:error: Can't find valid values for all labels after 1004 passes, giving up.
print_hex.asm:error: Possible causes: recursive EQUs, macro abuse.
I have noticed if I don't use any labels(like loop label here) code works fine.