[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Interprete assembly instruction from C code

Posted on 2009-05-01
Medium Priority
256 Views
Dear experts,

Below I have C code (in which I've understood the logic) and its assembly interpretation.
Could you help me to understand the logic of the assembly instruction?

Thanks..
``````// in C

int j;
int a[3][3];
int *pa = &a[0][0];
j =   *(pa+7);

// in assembly j = *(pa+7) is interpreted as below
movl	-172(%ebp), %eax
movl	(%eax), %eax
movl	%eax, -188(%ebp)
``````
0
Question by:isuhendro

LVL 53

Accepted Solution

Infinity08 earned 500 total points
ID: 24277207
>>         movl    -172(%ebp), %eax

Moves a 4-byte local function variable from the stack (at -172(%ebp) ) into the eax register.
At -172(%ebp), which is 172 bytes below the current stack frame base pointer, the local variable 'pa' is stored.
So, eax now contains a copy of 'pa'.

28 is added to the eax register. Note that 28 == 7 * sizeof(int) == 7 * 4.
So, eax now contains (pa + 7).

>>         movl    (%eax), %eax

The value in eax is interpreted as an address, the value at that address is retrieved, and stored back into eax.
So, eax now contains *(pa + 7).

>>         movl    %eax, -188(%ebp)

The value in eax is stored back onto the stack at -188(%ebp).
At -188(%ebp), which is 188 bytes below the current stack frame base pointer, the local variable 'j' is stored.
So, 'j' now contains *(pa + 7).

Or, in summary, these 4 instructions correspond to this one line  in C :

>>       j =   *(pa+7);
0

Author Closing Comment

ID: 31576791
Thanks for the excellent and prompt answer!!!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
###### Suggested Courses
Course of the Month18 days, 16 hours left to enroll