I need to compare a signed number N with 32 bits against zero and against other 32 signed number. How can I do it?

Suppose that N32 is on DX:AX. I was trying to do to compare against zero:

sub ax,0
sbb dx,0
jz
;do else

But here I was compare only MSByte (dx), isn't it?

To compare against another number. Supposing in memory referenced by SI:

sub ax,[si]
sbb dx,[si+2]
jz
;do else

And here we have the same problem.

How can I do it?

有帮助吗?

解决方案

If you have two 16-bit quantities in memory or register and you want to test if they are both zero you can OR them together. If the result of the OR is zero, then both operands were zero.

If you have two 32-bit quantities each represented as two 16-bit quantities and you want to know if the two 32-bit quantities compare equal, then subtract the upper 16-bits of the two quantities and the lower 16-bits of the two quantities and OR those differences together... again, if the result of the OR is zero, then both 32-bit quantities were equal.

Let's say you have a 32-bit quantity in DX:AX and another in CX:BX and you want to know if they are equal:

sub ax,bx ; ax = ax - bx
sub dx,cx ; dx = dx - cx
or  ax,dx ; ax = ax OR dx
je  they_are_equal

The SUB can be replaced with XOR:

xor ax,bx ; ax = ax ^ bx
xor dx,cx ; dx = dx ^ cx
or  ax,dx ; ax = ax OR dx
je  they_are_equal
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top