简体   繁体   中英

Comparison of 128 bit unsigned integers in x86-32 assembly

I am trying to conduct comparison between 2 128 bit integers, and I just can't seem to figure it out!

Specifically, I have a whole bunch of IPv6 IP addresses (start and end address of allocation blocks) and I'm trying to establish if a particular IP address sits between these blocks, so it's not a direct comparison, more like trying to check if greater than start address and lower than end address.

I'm using WinAsm Studio with MASM32 if that makes any difference.

Conducting this against IPv4 addresses is simple, however, I have no clue how to conduct this against 128 bit unsigned integers (4 x DWORD).

Thank you in advance.

Otherwise just use scalar code starting from the most-significant chunk for your range check.

On a CPU that uses cmp and a flags register, this is done the following way:

  • Compare the highest parts (eg highest 32 bits) of the numbers ( cmp instruction)
  • If not "Equal", jump to "EndOfCompare" (x86: jne instruction)
  • Compare the next parts (eg 32 bits) of the numbers
  • If not "Equal", jump to "EndOfCompare"
  • ...
  • Compare the next parts (eg 32 bits) of the numbers
  • If not "Equal", jump to "EndOfCompare"
  • Compare the lowest parts (eg lowest 32 bits) of the numbers
  • "EndOfCompare":
    At this point the flags register contains information about the order ( a<b , a=b or a>b ) of the two large numbers just like you did a simple cmp instruction comparing two small numbers.

Unfortunately this simple variant will only work with unsigned numbers.

BTW, usually a block is "aligned" in IP address space so you only need to check mask away the low bits and compare the high bits for equality.

A check for (A AND MASK) = (B AND MASK) can be done the following way on a 32-bit CPU:

mov ecx, part 1 of A
xor ecx, part 1 of B
and ecx, part 1 of MASK

mov eax, part 2 of A
xor eax, part 2 of B
and eax, part 2 of MASK
or  ecx, eax

mov eax, part 3 of A
xor eax, part 3 of B
and eax, part 3 of MASK
or  ecx, eax
...

In the case of a 128-bit number, you need 4 "parts".

It does not matter if "part 1" is the upper or the lower bits of the numbers.

If (A AND MASK) = (B AND MASK) is true, ecx will have the value 0 (and the zero flag will be set because of the or instruction).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM