Your goal is to analyse the following compiler-generated assembly language code and understand how it works. It contains a very simple loop.
... mov dword ptr [esi], 1 xor edx, edx mov [ebx], edx jmp short loc_4012F1 loc_4012E8: mov ecx, [esi] imul ecx, [esi] mov [esi], ecx inc dword ptr [ebx] loc_4012F1: cmp dword ptr [ebx], 8 jl short loc_4012E8 ...
You must retrieve the proper C/C++ code or pseudo code of this commented procedure. Your solution has to contain either a full commented C/C++ code or a detailed pseudo code describing the function of the above snippet.
x = (any number);
x ^= 8;
(Not too much of a C programmer but I believe this ^^^ is right)
Basically takes whatever is in ecx and finds ecx to the power of 8 (multiply ecx to itself 8 times).
I would say it’s something like this:
int n = 1;
for (int i = 0; i < 8; i++)
n = n * n;
mov dword ptr [esi], 1 ; move 1 into esi pointer , [esi] = 1
xor edx, edx ; edx = 0
mov [ebx], edx ; EBX = 0
jmp short loop_until_8 ; jmp sub loop_until_8
loop_until_8: ;{
mov ecx, [esi] ; ecx = 1
imul ecx, [esi] ; 1 * 1 = 1
mov [esi], ecx ; [esi] = 1
inc dword ptr [ebx] ; EBX = ebx + 1 ; (2) ebx = ebx + 1.. until EBX = 8
compare_ebx:
cmp dword ptr [ebx], 8 ; if(EBX >= 8) break;
jl loop_until_8 ; }
————————————-
var_esi = 1;
var_ebx = 0;
for(var_ebx = 0; var_ebx < 8;var_ebx++){
var_ecx = var_esi; // 1;
var_esi = var_ecx * var_esi; // var_esi = 1 * 1
}
Any feedback would be appreciated.
-Daniel Clemens
I think the above comments are correct but it’s kind of weird to choose ‘1′ as base for the power (since 1^x == 1).
I have the same result like tadas.
int result = 1; // [esi]
for(int i = 0; i < 8; i++) // i is [ebx]
{
result *= result;
}
Like shokora said, result is always 1. So you can simplify it and just write "result = 1" with the same result :D
Here is my first version of the C++-Code for the ones who have problems to get the solution:
int p_esi = 1;
int edx = 0;
int p_ebx = edx;
int ecx;
while(p_ebx < 8)
{
ecx = p_esi;
ecx = ecx * p_esi
p_esi = ecx
p_ebx++;
}