I have a very strange behavior with cmp
and jmp
commands in x86 assembly and I don't know why.
Here is my code (initial value of dx
register is 0xdada
):
1 ;prints the value of DX as hex.
2 print_hex:
3 pusha
4 print_hex_body:
5 cmp dl, 0
6 jge print_hex_else
7 jmp print_hex_epilogue
8 print_hex_if:
9 jmp print_hex_epilogue
10 print_hex_else:
11 ; TODO replace '1' with '0'
12 mov al, '1'
13 mov [HEX_OUT + 0x5], al
14 ; moving byte < 16 to bx register so it can be used by xlat
15 mov bx, HEX_TABLE
16 ; dx store value less than 16 so it stored in dl entirely
17 ;and dl, 0xf
18 mov al, dl
19 xlat
20 mov [HEX_OUT + 0x4], al
21 print_hex_epilogue:
22 mov bx, HEX_OUT
23 call print_string
24 popa
25 ret
26
27 HEX_OUT: db '0x0000',0
28 HEX_TABLE: db '0123456789abcdef'
The main problem is on line 5: I'm comparing dl value (which should contain 0xda
) with zero and if dl >= 0 then jump to print_hex_else
label.
But what actually happens is that my code just prints 0x0000
(i.e. jump to print_hex_epilogue
). Why?