ARITHMETIC OPERATIONS
#C CODE SNIPPET
int a = 0;
int b = 1;
a = a + 11;
a = a - b;
a--;
b++;
b = a % 3;
#ASSEMBLY CODE SNIPPET
00401006 mov [ebp+var_4], 0 ;a = 0 - local variables since they are referenced by the stack
0040100D mov [ebp+var_8], 1 ;b = 1
00401014 mov eax, [ebp+var_4] ❶
00401017 add eax, 0Bh ; a = a + 11
0040101A mov [ebp+var_4], eax ;move result of a + 11 into ebp-4 memory location
0040101D mov ecx, [ebp+var_4] ;move ebp-4 from memory to ecx
00401020 sub ecx, [ebp+var_8] ❷ ; a = a - b
00401023 mov [ebp+var_4], ecx ;move result of a - b into ebp-4 memory location
00401026 mov edx, [ebp+var_4]
00401029 sub edx, 1 ❸ ;a--
0040102C mov [ebp+var_4], edx
0040102F mov eax, [ebp+var_8]
00401032 add eax, 1 ❹ ;b++
00401035 mov [ebp+var_8], eax
00401038 mov eax, [ebp+var_4]
0040103B cdq
0040103C mov ecx, 3
00401041 idiv ecx
00401043 mov [ebp+var_8], edx ❺
* cqd means "convert double to quad"
- this instruction is used to sign-extend the value in the eax register into the edx register
- this is typically used when performing division operation to ensure that the full 64-bit result can be stored in both EDX:EAX for a 64-bit dividend
- the cdq instruction takes the 32-bit value in EAX, extends the sign of that value (MSB) & stores the result in EDX
* idv means "integer division"
- this instruction is used to perform signed division
- unlike the div which is for unsigned division, idiv handles signed numbers by taking the sign into account during the division
- when performing the div or idiv instruction (5), you are dividing EDX:EAX
by the operand & storing the result in EAX & the remainder in EDX
- this is why EDX is moved into var_8
Last updated