0

I tried to write a program in Assembly that calculate operand_a + operand_b.

My big problem if that operanda and operandb are representable in 16-byte (4 d-word). So I wrote this code:

mov eax, operanda
mov ebx, operandb
add eax, ebx

But than I understood that my operand is big than 32-bit.

I missed something?

Adam Sh
  • 8,137
  • 22
  • 60
  • 75
  • 16 byte operands (which is 128 bit) is uncommon for basic arithmetic instructions. x86 processors support only logic operations (AND, OR, XOR, NAND) in 16 byte width and you need to use SSE for it. – Gunther Piez Nov 24 '11 at 12:46

3 Answers3

1

There is a carry flag for doing larger than dword size additions. Just add the two least significant dwords using add. This will set the carry flag if the addition doesn't fit in a dword. Then, add the next two dwords using adc. This will add the two operand dwords plus the carry flag. Keep adding dwords using adc until you reach the two most significant dwords.

ninjalj
  • 42,493
  • 9
  • 106
  • 148
  • Thanks, I wrote: mov eax, dword [edx] add eax, [ebx] mov dword [buf32], eax mov eax, dword [edx+4] adc eax, dword [ebx+4] mov dword [buf32+4], eax mov eax, dword [edx+8] adc eax, dword [ebx+8] mov dword [buf32+8], eax mov eax, dword [edx+12] adc eax, dword [ebx+12] mov dword [buf32+12], eax where buf32 is defined in .bss. ebx & ebx contain my numbers. it's give n segmantion fault. What can be the reason? – Adam Sh Dec 03 '11 at 22:01
1

Define another variable, operandc, for the result.

Then do long addition from the least significant dwords to the most significant dwords not forgetting about the intermediate carry's:

mov eax, dword ptr [operanda]
add eax, dword ptr [operandb]
mov dword ptr [operandc], eax

mov eax, dword ptr [operanda+4]
adc eax, dword ptr [operandb+4]
mov dword ptr [operandc+4], eax

mov eax, dword ptr [operanda+8]
adc eax, dword ptr [operandb+8]
mov dword ptr [operandc+8], eax

mov eax, dword ptr [operanda+12]
adc eax, dword ptr [operandb+12]
mov dword ptr [operandc+12], eax
Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • Thanks, I wrote: mov eax, dword [edx] add eax, [ebx] mov dword [buf32], eax mov eax, dword [edx+4] adc eax, dword [ebx+4] mov dword [buf32+4], eax mov eax, dword [edx+8] adc eax, dword [ebx+8] mov dword [buf32+8], eax mov eax, dword [edx+12] adc eax, dword [ebx+12] mov dword [buf32+12], eax where buf32 is defined in .bss. ebx & ebx contain my numbers. it's give n segmantion fault. What can be the reason? – Adam Sh Dec 03 '11 at 21:59
  • @AdamSh: edx and ebx must contain the addresses of the numbers that you want to add, not the numbers themselves. – Alexey Frunze Dec 03 '11 at 22:46
0

you need to use multiple registers, 2 at minimum for x64. your assembly indicates that your using 32bit assembly though, so you'd need to use 4 registers, which makes it more complex.

there is a C++ class here for it, you need only emulate the method, but seeing as it can get complex in 32bit scenarios, you might wanna compile this in terms of how you'd use it and disassemble it. if you truely are using x64, this will be of use.

Community
  • 1
  • 1
Necrolis
  • 25,836
  • 3
  • 63
  • 101