Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

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

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

  • Author
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

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

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.