Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Assembly x86 > Re: global star...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 8 Topic 4574 of 4729
Post > Topic >>

Re: global start, global main or anything I want?

by Tim Roberts <spamtrap@[EMAIL PROTECTED] > Mar 16, 2008 at 09:25 PM

phoenix  <spamtrap@[EMAIL PROTECTED]
> wrote:
>
>I am a complete newbie who is trying to learn assembly. I am using
>NASM and working on Linux.
>I have written a (very) simple program that calculates ah+al.
>...
>So I type in the terminal: nasm -f elf ass.s -o ass.o
>and then I type: gcc ass.o -o as***e
>
>An error message appears:
>/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crt1.o: In function
>`_start':
>(.text+0x18): undefined reference to `main'
>collect2: ld returned 1 exit status
>
>If the label (in the program) is called main (and not prog), there's
>no error message.
>If the label is called start, the error message appears again.
>
>My question is: can I choose whatever label names in my program,
>without having all these error messages? Or am I forced to always
>insert a label called "main" in my code?

When you are writing a function that will be called by other code, you can
name it whatever you want to, as long as both sides agree on the names.

In this case, however, there is another player involved.  When you run a
program from a command line, the system loads your program into memory and
starts it by jumping to a special address called the "transfer address".
When you use "gcc" to link your code, "gcc" assumes that you are using the
C run-time library, so it arranges that the "transfer address" points to a
startup function in the C run-time library.  That startup function
initializes a bunch of stuff in the library, and then calls a function
called "main".  Every C program must include a function called "main",
which receives the command-line arguments and does the main processing.
When "main" returns, the program ends.

So, if you want to use "gcc" to link your code (and that's a reasonable
thing to do when you are learning assembly, because it lets you use
convenient functions like printf), then your main entry point will have to
be called "main".

The code you wrote is not really a "program", because it doesn't do
anything with the result of the addition.  It is more of a function that
is
designed to be called by someone else.  If you were calling this from a C
main program, you could call it "prog", and your "main" would be in the C
file.
-- 
Tim Roberts, timr@[EMAIL PROTECTED]
 & Boekelheide, Inc.
 




 8 Posts in Topic:
global start, global main or anything I want?
phoenix <spamtrap@[EM  2008-03-16 10:51:20 
Re: global start, global main or anything I want?
Tim Roberts <spamtrap  2008-03-16 21:25:30 
Re: global start, global main or anything I want?
Terence <spamtrap@[EM  2008-03-16 14:19:02 
Re: global start, global main or anything I want?
Frank Kotler <spamtra  2008-03-16 21:58:37 
Re: global start, global main or anything I want?
Robert Redelmeier <red  2008-03-16 22:32:37 
Re: global start, global main or anything I want?
phoenix <spamtrap@[EM  2008-03-17 03:25:33 
Re: global start, global main or anything I want?
Frank Kotler <spamtra  2008-03-17 21:27:21 
Re: global start, global main or anything I want?
phoenix <spamtrap@[EM  2008-03-19 01:01:59 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 15:01:51 CDT 2008.