1

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?

kirugan
  • 2,514
  • 2
  • 22
  • 41
  • `0xda` is a negative 2's complement number. `jge` is a *signed* condition. Use `jae` if you want unsigned `>=`, although *every* number is >= 0U so that would be pointless. 0 isn't a special case, you can just let your conversion code handle it. – Peter Cordes Nov 13 '21 at 22:47
  • @PeterCordes thank you, it works. Actually I used 0 for debugging this issue, there should be 16 – kirugan Nov 14 '21 at 13:01

0 Answers0