LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 03-07-2010, 05:27 PM   #106
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231

Quote:
But I think whatever Smeezekitty showed you at best applies to 32 bit x86 not 64 bit x86 and more likely doesn't even get 32 bit x86 right.
Because i can have only a 32 bit OS.
 
Old 03-07-2010, 07:09 PM   #107
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Also, is it possible to just embed asm code into a C source file?
 
Old 03-07-2010, 07:26 PM   #108
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by MTK358 View Post
Also, is it possible to just embed asm code into a C source file?
use the asm tag:
Code:
asm __volatile__ (
"movl 5,%eax\n"
"pushl %eax\n"
);
Use one string quote set per line, you must end each instruction with \n.

This is actually the most common way to use asm.
edit: too many percent signs

Last edited by smeezekitty; 03-07-2010 at 07:47 PM. Reason: too many percent signs
 
Old 03-07-2010, 07:44 PM   #109
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Why the double '%' characters?
 
Old 03-07-2010, 07:45 PM   #110
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by MTK358 View Post
Why the double '%' characters?
Typo
 
Old 03-07-2010, 08:02 PM   #111
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I was reading through the examples and got confused -- aren't you supposed to push function parameters onto the stack, not save them in registers?
 
Old 03-07-2010, 08:49 PM   #112
Mol_Bolom
Member
 
Registered: Nov 2008
Location: S.W. Kansas
Distribution: Slackware64 14.0 / 14.2
Posts: 245
Blog Entries: 2

Rep: Reputation: 41
Quote:
Originally Posted by MTK358 View Post
I was reading through the examples and got confused -- aren't you supposed to push function parameters onto the stack, not save them in registers?
You can use the registers as well. Often times I don't use push or pop with simple calls at all, because the registers already hold all the information that is needed in a call.

Though, I think I'm not the best to help you because I've been learning to write pure assembly, so I haven't read much on utilizing it with C or any other programming language as of yet. As well as I only program in 32bit at the moment and only using the intel manuals which you don't need.

However, though, you might find www.asmcommunity.net interesting.
 
Old 03-07-2010, 09:03 PM   #113
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
I was reading through the examples and got confused -- aren't you supposed to push function parameters onto the stack, not save them in registers?
In fact, some "C" compilers allow (using non-standard built-ins/pragmas) to pass parameters through registers - for efficiency.
 
Old 03-08-2010, 04:03 AM   #114
fateh
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Rep: Reputation: 0
Getting Started Learn ASM

Getting Started Learn ASM

The very first thing you need to do is to make certain that you have the right programming tools at hand. You can either write your first program with a debugger or else with an assembler and linker.

One debugger tool is called DEBUG.EXE and it is from Microsoft. It is sometimes installed from the CD-ROM onto the hard disk when the operating system is first placed there. But not always. (It is on the CD-ROM, but not always copied to the hard disk.) Another possible debugger is a 16-bit capable debugger like GRDB from David Lindauer. While Microsoft traditionally includes DEBUG on most operating systems it makes, it isn't always installed. If you cannot find it on your machine, consider using GRDB from David's site (see my PC Tools web page, near the bottom of the page.)

For more general purpose assembly programming, you will need an assembler and linker. ML is an assembler from Microsoft and LINK is the linker, also from Microsoft. They are also pointed at by my PC Tools web page. I'd recommend getting all of these you can and installing them. That way you can try out various ways of writing your programs.

Be sure that you update your PATH variable so that these programs are accessible from the command line or else put them all into a common directory where you will be working and just create your programs there.

Last edited by pixellany; 03-08-2010 at 06:48 AM.
 
0 members found this post helpful.
Old 03-08-2010, 06:49 AM   #115
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
fateh;

Please remove the advertising links from your signature.
 
Old 03-08-2010, 07:17 AM   #116
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by fateh View Post
Getting Started Learn ASM

The very first thing you need to do is to make certain that you have the right programming tools at hand. You can either write your first program with a debugger or else with an assembler and linker.

One debugger tool is called DEBUG.EXE and it is from Microsoft. It is sometimes installed from the CD-ROM onto the hard disk when the operating system is first placed there. But not always. (It is on the CD-ROM, but not always copied to the hard disk.) Another possible debugger is a 16-bit capable debugger like GRDB from David Lindauer. While Microsoft traditionally includes DEBUG on most operating systems it makes, it isn't always installed. If you cannot find it on your machine, consider using GRDB from David's site (see my PC Tools web page, near the bottom of the page.)

For more general purpose assembly programming, you will need an assembler and linker. ML is an assembler from Microsoft and LINK is the linker, also from Microsoft. They are also pointed at by my PC Tools web page. I'd recommend getting all of these you can and installing them. That way you can try out various ways of writing your programs.

Be sure that you update your PATH variable so that these programs are accessible from the command line or else put them all into a common directory where you will be working and just create your programs there.
I do not have ANY Microsoft products on my computer (including Windows), I don't want any, and they won't run on Linux.

I really don't understand why you are pushing M$ products on me. Could you have at least done some research?

I already have all the programming tools I need thanks to the GNU project ("gcc" C compiler, "g++" C++ compiler, "gdb" debugger "ld" linker, "as" assembler).

Last edited by MTK358; 03-08-2010 at 07:21 AM.
 
Old 03-08-2010, 07:23 AM   #117
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by MTK358 View Post
Also, is it possible to just embed asm code into a C source file?
Yes, but it is much harder than you would expect. There is a very complicated counter intuitive syntax for telling the compiler the inputs, outputs and clobbers of a block of asm code.

I know that method of asm coding and recently I've used it more than other methods, but I strongly recommend against it for anyone just learning asm.

Once you know asm, if you decide to use it in serious work, you might need to learn the syntax for embedding asm in C.

The method Smeezekitty showed you (skipping the whole syntax for coordinating your register use with the compiler's register use) is unsound.

Quote:
Originally Posted by MTK358 View Post
I was reading through the examples and got confused -- aren't you supposed to push function parameters onto the stack, not save them in registers?
In many architectures, the ABI specifies that all parameters are pushed onto the stack.

But in x86_64, the ABI specifies some parameters are passed in registers (which typically makes much more efficient code). If you have more parameters, the rest would be passed on the stack.

Quote:
Originally Posted by Mol_Bolom View Post
You can use the registers as well. Often times I don't use push or pop with simple calls at all, because the registers already hold all the information that is needed in a call.
When you write an asm function to be called only by other asm functions, you can pass parameters however you like (you can ignore the ABI). So even in 32 bit x86 (where parameters are passed on the stack by functions obeying the ABI) it is common to use registers, because that is more efficient when you can ignore the ABI.

If your asm code is called by C code or if it calls C code or calls standard library functions, all those calls must obey the ABI. You don't get to choose whether parameters go on the stack or in which registers. The ABI specifies that.

Even for asm functions called only by asm, obeying the C ABI may be a good idea, especially in x86_64 where it is a pretty efficient ABI.

Last edited by johnsfine; 03-08-2010 at 07:25 AM.
 
Old 03-08-2010, 07:24 AM   #118
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
see my PC Tools web page, near the bottom of the page.
We may have some kind of advertising troll here. The links in his sig (which I removed) were for something totally unrelated.
 
Old 03-08-2010, 07:25 AM   #119
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Mol_Bolom View Post
You can use the registers as well. Often times I don't use push or pop with simple calls at all, because the registers already hold all the information that is needed in a call.

Though, I think I'm not the best to help you because I've been learning to write pure assembly, so I haven't read much on utilizing it with C or any other programming language as of yet. As well as I only program in 32bit at the moment and only using the intel manuals which you don't need.

However, though, you might find www.asmcommunity.net interesting.
How does the function know whether the parameters are in the registers or on the stack?

EDIT: One other thing that puzzles me is whether the main() function in C code is actually turned into a function in Assembler, or is it not really a "function"?

Last edited by MTK358; 03-08-2010 at 07:29 AM.
 
Old 03-08-2010, 07:37 AM   #120
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by MTK358 View Post
How does the function know whether the parameters are in the registers or on the stack?
Same way it know the sequence, meaning, types etc. of its parameters in a normal function (that obeys the ABI):

The author of the functions decides on the number of parameters and the types and sequence and meaning etc.

In a high level language, a lot of that info is specified by the function declaration. But there is usually still some that must be specified by comments or other documentation.

The author of a call to a function should read the comments and/or documentation to find out how to call the function.

In asm, less is specified by declarations, so more must be specified by comments and documentation.

If a function doesn't obey the ABI, a whole lot of details of its interface must be specified by comments or documentation, so it will be possible to write correct calls to that function.

Quote:
EDIT: One other thing that puzzles me is whether the main() function in C code is actually turned into a function in Assembler, or is it not really a "function"?
main() in C code is compiled as an ordinary function, obeying the ABI the same way other functions do.

main() is not the starting point of a program. The starting point is usually some asm code that gcc tells the linker to link in. That code does various initialization, then calls the function main(). If main() returns, that startup code preserves the return code from main and calls some version of exit() (which is also a function, but doesn't return).

Last edited by johnsfine; 03-08-2010 at 07:39 AM.
 
  


Reply



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
ASM or C++? Hb_Kai Programming 16 01-20-2010 09:12 AM
Is ASM dangerous? MrCode Programming 37 11-18-2009 08:29 AM
ASM x32 vs ASM x64 Tegramon Programming 3 02-27-2008 02:26 PM
I/O in ASM Mercurius Programming 10 11-16-2006 07:02 PM
ASM question zWaR Programming 2 06-26-2004 11:42 AM

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

All times are GMT -5. The time now is 01:46 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