Interprete assembly instruction from C code

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
	addl	$28, %eax
	movl	(%eax), %eax
	movl	%eax, -188(%ebp)

Open in new window

isuhendroAsked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
>>         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'.

>>         addl    $28, %eax

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
 
isuhendroAuthor Commented:
Thanks for the excellent and prompt answer!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.