Here I have some examples of handling large integers.
Adding
The principle is pretty simple. You need to use CF (carry flag) for any larger overflow. Let's think about adding two 128-bit numbers.
num1_lo: dq 1<<63 num1_hi: dq 1<<63 num2_lo: dq 1<<63 num2_hi: dq 1<<62 ;Result of addition should be 0xC0000000 0x000000001 0x00000000 0x00000000 mov eax, dword [num1_lo] mov ebx, dword [num1_lo+4] mov ecx, dword [num1_hi] mov edx, dword [num1_hi+4] add eax, dword [num2_lo] adc ebx, dword [num2_lo+4] adc ecx, dword [num2_hi] adc edx, dword [num2_hi+4] jc .overflow
Subtraction
Very similar to a supplement, although you now call CF loan.
mov eax, dword [num1_lo] mov ebx, dword [num1_lo+4] mov ecx, dword [num1_hi] mov edx, dword [num1_hi+4] sub eax, dword [num2_lo] sbb ebx, dword [num2_lo+4] sbb ecx, dword [num2_hi] sbb edx, dword [num2_hi+4] jb .overflow ;or jc
Multiplication
This is much more complicated. You need to multiply each part of the fisrt number by each part of the second number and add the results. You do not need to multiply only the two higher parts, which are undoubtedly overflowing. Pseudocode:
long long int result = 0; long long int n1 = ; long long int n2 = ; #define PART_WIDTH 32
Department
even harder. A user at Brendan forum OsDev.org posted a pseudo-code example for dividing n-bit integers. I insert it here because the principle is the same.
result = 0; count = 0; remainder = numerator; while(highest_bit_of_divisor_not_set) { divisor = divisor << 1; count++; } while(remainder != 0) { if(remainder >= divisor) { remainder = remainder - divisor; result = result | (1 << count); } if(count == 0) { break; } divisor = divisor >> 1; count--; }
user35443
source share