Search code examples
assemblyx86-16tasm

JL command in TASM works when it shouldn't


In my TASM program I need to compare a double word variable with a set of other double word variables, which are declared as this:

dd_buf dd 0
powers_of_10 dd 3B9ACA00h, 05F5E100h, 00989680h, 000F4240h, 000186A0h, 00002710h, 000003E8h, 00000064h, 0000000Ah

And I compare these values like this:

I know that in this code bit I only compare first 16 bits of each variable

mov ax, word ptr dd_buf
mov bx, word ptr powers_of_10[di]
cmp ax, bx
jg greater
jl less
greater:
    mov dx, offset greater_output_hint
    call putstr
    call clrf
    jmp end
less:
    mov dx, offset less_output_hint
    call putstr
    call clrf

But when I run this program in Turbo Debugger, and check the values, I see that when ax register is set to FFFF, and bx is set to 03E8, after cmp ax, bx line my code jumps into less, as if AX is less than BX, when it is not!

What I was expecting, is that when ax is less than bx, after jl command it would jump into less section, and similar for case when ax is greater than bx.

I tried to do PUSHF and POPF commands before and after the instructions, but it didn't help. I also tried to compare ax and bx in two steps: firstly compare ah and bh, and then al and bl, but that didn't help also.

So, I am stuck. Googling this problem didn't bring anything, so I hope for your help.


Solution

  • You think that when AX=0FFFFh, it is above 03E8h, but it really depends on whether you are comparing signed or unsigned numbers. If 0FFFFh is treated as an unsigned number, it is 65535. However, if 0FFFFh is treated as a signed number, is -1. For signed comparisons, use JG/JL; but for unsigned comparisons, use JA/JB.