7.2 Disassembly Solution

The preceding code consists of two memory addresses (ebp-4 and ebp-8); let's rename ebp-4 to x and ebp-8 to y. The modified code is shown here:

 mov dword ptr [y], 1
mov dword ptr [x], 0

loc_401014:
cmp dword ptr [x], 4 ➋
jge loc_40102E ➌
mov eax, [y]
add eax, [x]
mov [y], eax
mov ecx, [x] ➎
add ecx, 1
mov [x], ecx ➏
jmp loc_401014 ➊

loc_40102E: ➍

In the preceding code, at ➊, there is a backward jump to loc_401014, indicating a loop; so, let's rename loc_401014 to loop. At ➋ and ➌, there is a condition check for the variable x (using cmp and jge); the code is checking whether x is greater than or equal to 4. If the condition is met, it will jump outside of the loop to loc_40102E (at ➍). The value of x is incremented to 1 (from ➎ to ➏), which is the update statement. Based on all of this information, it can be deduced that x is the loop variable that controls the loop. Now, we can write the preceding code to a high-level language equivalent; but to do that, remember that we need to reverse the condition from jge  (jump if greater than or equal to) to jump if less than. After the changes, the code looks as follows:

y = 1
x = 0
while (x<4) {
eax = y
eax = eax + x ➐
y = eax ➐
ecx = x
ecx = ecx + 1 ➐
x = ecx ➐
}

Replacing all of the registers on the right-hand side of the = operator (at ➐) with their previous values, we get the following code:

y = 1
x = 0
while (x<4) {
eax = y ➑
eax = y + x ➑
y = y + x
ecx = x ➑
ecx = x + 1 ➑
x = x + 1
}

Now, removing all of the entries containing registers on the left-hand side of the = sign (at ➑), we get the following code:

y = 1;
x = 0;
while (x<4) {
y = y + x;
x = x + 1;
}

If you are curious, the following is the original C program of the disassembled output. Compare the preceding code that we determined with the code that follows from the original program; notice how it was possible to reverse engineer and decompile the disassembled output to its original equivalent:

int a = 1;
int i = 0;
while (i < 4) {
a = a + i;
i++;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.159.10