LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2005, 12:06 AM   #1
morph_ind
Member
 
Registered: Feb 2005
Distribution: fedora 2/suse
Posts: 38

Rep: Reputation: 15
doubts about assembly code generated from gcc


Hi all
i m doin a project on gcc compiler and need some help regarding some instructions ..........

1) my first problem starts at the beginning of the program only ....have a look

main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp

ok... now i know what these instructions do and what basic registers are being initialized to ,my question is little stupid i m not able to get the overall objective of these instructions like why we creating stack memory of 8 bytes and why we setting last 4 bits of %esp to 0 etc etc ....... it would be nice if someone would tell me the overall objective of all the instructions listed above


2)k my second problem is the commands ..... all of them end with a 'l'
ok.. i know just the arguments get reversed my problem is ...... is there
any way possible that gcc might use commands that do not end with 'l'
and if yes then under what cases


3)ok third problem i have yet to give it a try .... if i compile the program all the way through to build an executable file and then using a diassembler (i think i messed the spellings ) i get back the assembly code ,will this code match the code i got just by compiling a .c file with -s option


4) ok i m not sure of this one as i got this instruction way back and cant get it now i think it was something like

movl (%eax,3) , %ebx

if i made a mistake then correct me can anybody tell me what this does .....
i concluded that it multiplies the first 2 arguments and add the results to the 3rd but still i would prefer some input on it


thanx and i would really appreciate some help

take care
 
Old 05-27-2005, 03:42 AM   #2
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
http://www-106.ibm.com/developerworks/library/l-ia.html

This might help you.
 
Old 05-27-2005, 07:04 AM   #3
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
1) The eight stack bytes are probably for local variable storage. Clearing the last four bits of the stack pointer is likely just for aligning on a page boundary - i.e improving performance.

2) this is nicely covered by murugesan's link

3) You should see the same ASM code (more or less... sometimes an assembler will substitue one identical command for another, but I don't know if gas does)

4) This is memory addressing. I think the example you gave translates to this in Intel syntax:
mov ebx, dword ptr [eax+3]
(more technically, [eax + 3*1], which amounts to the same thing)
which means take the value of EAX, add 3, then move the value in memory at that address into EBX. In other words, it uses the value in EAX as a pointer to memory, rather than just a value. There is a link at the bottom of the IBM page that helps explain this a little better (but not much - it mostly assumes you already understand the Intel syntax).
 
Old 05-27-2005, 08:13 AM   #4
morph_ind
Member
 
Registered: Feb 2005
Distribution: fedora 2/suse
Posts: 38

Original Poster
Rep: Reputation: 15
okkkk talk of fast reply
thanx man (CroMagnon,murugesan) you cleared most of my doubts the link is indeed helpful in what i m doin some last doubt .......
in (1) why are we subtracting zero ie the value stored in eax from esp
can this value differ

also 1 more ques some times in the assembly code a direct function call is made like in the case of using scanf routine in 'C' we get something like
pushl %eax
pushl $.LC1
call scanf
addl $16, %esp

now because of some stack operations inside this function i m losing the current stack pointer location which i need terribly i have searched thru net but not able to find the assembly code for these functions can somebody help me in giving me some link or any other resource regarding this

and finally pls suggest some good diassembler for gcc

thanx for ur help
u guys rock
 
Old 05-27-2005, 10:11 AM   #5
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
Quote:
4) This is memory addressing. I think the example you gave translates to this in Intel syntax:
mov ebx, dword ptr [eax+3]
(more technically, [eax + 3*1], which amounts to the same thing)
which means take the value of EAX, add 3, then move the value in memory at that address into EBX. In other words, it uses the value in EAX as a pointer to memory, rather than just a value. There is a link at the bottom of the IBM page that helps explain this a little better (but not much - it mostly assumes you already understand the Intel syntax).
Actually, the example is invalid. Memory adderssing works like this:
Code:
displacement(base,index,scale)
or, using Intel syntax:
Code:
[base + index*scale + displacement]
where base and index must be registers, displacement is an integer constant, and scale is one of 1, 2, 4, or 8. You can leave out unneeded parts, for example:
Code:
(,eax,4)
and the same in Intel syntax:
Code:
[eax*4]
Quote:
now because of some stack operations inside this function i m losing the current stack pointer location which i need terribly i have searched thru net but not able to find the assembly code for these functions can somebody help me in giving me some link or any other resource regarding this
This is not possible. The call instruction stores the return address in the stack, and ret gets it from there. If esp is messed up in the function, the function returns to wrong place, usually causing a segmentation fault.

Quote:
and finally pls suggest some good diassembler for gcc
HT hex editor has a good built-in disassembler.
 
Old 05-27-2005, 10:47 AM   #6
morph_ind
Member
 
Registered: Feb 2005
Distribution: fedora 2/suse
Posts: 38

Original Poster
Rep: Reputation: 15
okkk
now i think i agree with harmaa kettu that the (4)
instruction might be incorrect i m not able to remember it as i never got it again and i overwrited the file that had it


for function call problem i m not able to explain my point properly i think

k have a look at this

subl $12, %esp
pushl $.LC0
call printf
addl $16, %esp
subl $8, %esp
leal -4(%ebp), %eax
pushl %eax
pushl $.LC1
call scanf
addl $16, %esp

now we are adding 16 bytes to the stack ie deallocating memory .... but we didnt allocated
16 bytes we only allocated 8 ... so i think now u should get my point thr must be some operation on stacks taking place ie the esp register inside the function so i basically need the assembly codes for these functions ....

anyway i deeply appreciate ur help

Last edited by morph_ind; 05-27-2005 at 11:15 AM.
 
Old 05-27-2005, 11:40 AM   #7
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
Code:
subl $8, %esp        <- 8 bytes
leal -4(%ebp), %eax
pushl %eax           <- +4 bytes
pushl $.LC1          <- +4 bytes
call scanf
addl $16, %esp       <- =16 bytes
BTW, try compiling with -Os switch. It removes the alignment stuff, making the resulting code much easier to understand.
 
Old 05-27-2005, 12:06 PM   #8
morph_ind
Member
 
Registered: Feb 2005
Distribution: fedora 2/suse
Posts: 38

Original Poster
Rep: Reputation: 15
ok thanx
i got it now ,was not considering the pushl instructions ,mistake on my part
thanx for clearing my doubt
 
Old 05-27-2005, 08:10 PM   #9
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Oops, looks like it has been too long since I worked with assembly language

You had a question earlier about subtracting 0 from ESP - I can think of two possibilities for that. One is, as you said, a value could change (possibly at runtime).
The other is to do with optimization. I don't know much about this in detail, but if you are asking the compiler to optimize for a CPU with multiple pipelines, sometimes it might add in extra instructions that don't do anything useful, but actually improve performance by separating dependent instructions, to make sure they are not pipelined in a negative way. I'm certainly no expert on this, so I don't know what the reason really is. As Harmaa said, concentrate on the ASM generated without optimization first; you'll have a better chance of spotting and understanding whether something is a low-level optimization, or a piece of template code.
 
Old 05-28-2005, 12:08 AM   #10
morph_ind
Member
 
Registered: Feb 2005
Distribution: fedora 2/suse
Posts: 38

Original Poster
Rep: Reputation: 15
Thanx CroMagnon
all i needed was to check that the instructions in (1) will not affect me ,the project is basically such that i have to take into account the instruction which were mapped from the c language ,the optimization instructions ,or the instructions which increase page access efficiency will not bother me so much now and i can simply ignore them anyway i deeply appreciate all ur guys help
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading Assembly code max_rsr Programming 4 07-22-2005 08:06 AM
Can gcc cross-compile C code to M68K Assembly? lowpro2k3 Programming 1 03-25-2005 07:50 AM
optcode, assembly Code, or binary? mrpc_cambodia Programming 5 01-05-2005 05:59 AM
Need Assembly Code Reference Table marky782 Linux - General 6 02-16-2004 06:53 PM
Command for getting assembly code aizkorri Programming 1 06-18-2002 05:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration