[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
2
Medium Priority
?
256 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:isuhendro
2 Comments
 
LVL 53

Accepted Solution

by:
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'.

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

Author Closing Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question