Ah, math. What a beautiful discipline. Lots of math is used within application development. Understandable in C++ might be problematic in assembly code. This exercise is not a problem at all, just some few math lines which are not complicated. Anyway, it is very important that you start playing such little math game if we later dig into cryptographic schemes. So tell me: what is this code doing?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | .text:00401000 _main proc near .text:00401000 .text:00401000 var_18= dword ptr -18h .text:00401000 var_14= dword ptr -14h .text:00401000 var_10= qword ptr -10h .text:00401000 var_8= dword ptr -8 .text:00401000 var_4= dword ptr -4 .text:00401000 argc= dword ptr 8 .text:00401000 argv= dword ptr 0Ch .text:00401000 envp= dword ptr 10h .text:00401000 .text:00401000 push ebp .text:00401001 mov ebp, esp .text:00401003 sub esp, 18h .text:00401006 mov [ebp+var_4], 5 .text:0040100D mov [ebp+var_8], 6 .text:00401014 mov eax, [ebp+var_4] .text:00401017 add eax, [ebp+var_8] .text:0040101A mov [ebp+var_14], eax .text:0040101D mov ecx, [ebp+var_8] .text:00401020 sub ecx, [ebp+var_4] .text:00401023 mov [ebp+var_14], ecx .text:00401026 mov edx, [ebp+var_4] .text:00401029 imul edx, [ebp+var_8] .text:0040102D mov [ebp+var_14], edx .text:00401030 mov eax, [ebp+var_4] .text:00401033 cdq .text:00401034 idiv [ebp+var_8] .text:00401037 mov [ebp+var_18], eax .text:0040103A fild [ebp+var_18] .text:0040103D fstp [ebp+var_10] .text:00401040 xor eax, eax .text:00401042 mov esp, ebp .text:00401044 pop ebp .text:00401045 retn .text:00401045 _main endp |
int main()
{ unsigned int var_4;
unsigned int var_8;
double var_10;
unsigned int var_14;
signed int var_18;
var_4 = 5;
var_8 = 6;
var_14 = var_4 + var_8;
var_14 = var_8 – var_4;
var_14 = var_4 * var_8;
var_18 = var_4 / var_8;
var_10 = (double)var_18;
return 0;
}
.text:00401000 var_18= dword ptr -18h signed int var_18
.text:00401000 var_14= dword ptr -14h signed int var_14
.text:00401000 var_10= qword ptr -10h double int var_10
.text:00401000 var_8= dword ptr -8 signed int var_8
.text:00401000 var_4= dword ptr -4 signed int var_4
.text:00401000 argc= dword ptr 8 int argc
.text:00401000 argv= dword ptr 0Ch char* argv
.text:00401000 envp= dword ptr 10h
.text:00401000
.text:00401000 push ebp put ebp into a stack
.text:00401001 mov ebp, esp ebp = esp
.text:00401003 sub esp, 18h esp – 24
.text:00401006 mov [ebp+var_4], 5 var_4 =5
.text:0040100D mov [ebp+var_8], 6 var_8 = 6
.text:00401014 mov eax, [ebp+var_4] eax = var_4
.text:00401017 add eax, [ebp+var_8] eax = var_4 + var_8 = 5 + 6 = 11
.text:0040101A mov [ebp+var_14], eax var_14=eax = 11
.text:0040101D mov ecx, [ebp+var_8] ecx=var_8=6
.text:00401020 sub ecx, [ebp+var_4] ecx=var_8 – var_4 = 6 -5 = 1
.text:00401023 mov [ebp+var_14], ecx var_14 = 1
.text:00401026 mov edx, [ebp+var_4] edx = var_4
.text:00401029 imul edx, [ebp+var_8] edx =edx * var_8 = 5 *6 = 30
.text:0040102D mov [ebp+var_14], edx var_14 = edx =30
.text:00401030 mov eax, [ebp+var_4] eax = var_4
.text:00401033 cdq converts the double word in eax to the quad word in edx:eax
.text:00401034 idiv [ebp+var_8] eax:=edx:eax/var_8 = 4/6 = 0.666
.text:00401037 mov [ebp+var_18], eax var_18 = eax
.text:0040103A fild [ebp+var_18] pushes var_18 into a FPU stack
.text:0040103D fstp [ebp+var_10] it pops the value off the top of stack when moving it to the dest.
.text:00401040 xor eax, eax eax = 0 (empty)
.text:00401042 mov esp, ebp esp = ebp
.text:00401044 pop ebp pops ebp from the stack
.text:00401045 retn return ebp
.text:00401045 _main endp
after posting the previous post, I wish there were a buttom for either delete or edit the post to make it more clear and easier to read.