implement 64-bit arithmetic on a 32-bit machine

前端 未结 3 1361
天命终不由人
天命终不由人 2020-12-29 17:16

The following code computes the product of x and y and stores the result in memory. Data type ll_t is defined to be equivalent to long long.

typedef long lon         


        
3条回答
  •  悲哀的现实
    2020-12-29 17:56

    Consider the context of line 8 and 9.

    By this time, ESI contains the lower half of y and EBX contains sgn(x). So line 8 is just computing sgn(x) * (y % 2^32) and storing it in EBX.

    Line 9 draws upon that result. By the time Line 9 happens, ECX contains a partial upper half of the multiplication, that is, x * (y >> 32) signed. So EBX+ECX ends up being what we computed in the last step plus the partial upper half we found on a previous line.

    The full algorithm itself is pretty neat ;)

    EDIT: In response to a comment below...

    Line 4: Consider what SAR EDX, 31 (or if you like, sar $31, %edx) really means. Since EDX is a 32-bit register, you'll end up with one of two values. Which two? Consider what they mean in the context of signed arithmetic.

    Line 7: EDX by this point contains something pretty useful for the following operations. I'm just moving it where it needs to go.

提交回复
热议问题