Jump to content
Tuts 4 You

What is the assembly source code of the following C code?


James Taylor

Recommended Posts

James Taylor
Posted

What is the assembly source code of the following C code?

Quote

 

char string[1001];

   while(string[index] != '\0') {
    diff = string[index] - string[index - 1];
    if(diff < 0) {
    count += (26 + diff);
    } else {
    count += diff;
    }
    index += 1;
    }hints: 

 

 

Quote

 

loc_4013B4:
1. lea     edx, [esp+13h]
2. mov     eax, [esp+408h]
3. add     eax, edx
4. movzx   eax, byte ptr [eax]
5. movsx   edx, al
6. mov     eax, [esp+408h]
7. sub     eax, 1
8. movzx   eax, byte ptr [esp+eax+13h]
9. movsx   eax, al
sub     edx, eax
mov     eax, edx
mov     [esp+400h], eax
cmp     dword ptr [esp+400h], 0
jns     short loc_401401[/php]

note: 

 

Quote

 


line 1 loading address of array from stack.
what the 2 and 3 and others line are doing?

Thanks

Posted (edited)

The C code doesnt fully match the assembler code. It's only the first two lines of the loop body.

Please check if you copied the asm correctly and/or clarify what you want to know.

Generally, always make sure to let us know all types involved (count, diff) and locations (loc_401401).

Edited by deepzero
James Taylor
Posted (edited)
47 minutes ago, deepzero said:

The C code doesnt fully match the assembler code. It's only the first two lines of the loop body.

Please check if you copied the asm correctly and/or clarify what you want to know.

Generally, always make sure to let us know all types involved (count, diff) and locations (loc_401401).

I want to understand the following in C in assembly. (indexing)

Quote

char string[1001];

   while(string[index] != '\0') {
    diff = string[index] - string[index - 1];

I want to understand the following as well. 

 

Quote

add     eax, edx

 

Edited by James Taylor
Posted

deja vu... it was explained once, if you didnt understand it then you wont now...  base.. index ? .. 

some days i wonder if some people are truly beyond help.. today is one of them

Posted
1. lea     edx, [esp+13h]
2. mov     eax, [esp+408h]
3. add     eax, edx
4. movzx   eax, byte ptr [eax]

Let's look at above code snippet.

* There is an array at esp+13h.

* There is a counter variable at esp+408h

 

Also remember that lea doesnt actually load the dereferences value, but rather stores the address.

lea edx, [esp+13]  ------->   edx = esp+13

So we have:

First line: load the starting address of the array into edx -> edx = esp+13

Second line: load the value of the counter variable into eax -> eax = [esp+408]

third line: add them together. we are adding the base address of the array (edx = esp+13) and the value of the counter variable (eax = [esp+408]). So we store in eax a pointer into the array at the specified index.

fourth line: load a byte from the pointer we just calculated. So we load a byte from the byte array at address esp+13 at index [esp+408].

 

Conceptually, the four lines can be summed up in C as : int diff = string[index].

Where string is the array esp+13, and index is the int-variable at esp+408.

-----------

Maybe this pseudo-assembly makes it more clear. The four lines can be rewritten as:

mov eax, byte ptr [esp + 13h + dword ptr [esp+408h]]

again, esp+13 is the base address of the array, dword ptr [esp+408] is the index variable.

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...