The whole program is located at output.s in the samples folder. Here is a line-by-line break down of this program.
.data
This begins the definition of the initialized data of the program.
myMessage: .ascii "This is cool!\n"
This line has two elements. First, the label definition
myMessage: (note the colon) defines a symbolic name for the
``current'' location. What is the current location? It is the first
byte of the ASCII string (not null-terminated) "This is cool!\n".
14 bytes are allocated in this case, one byte for each character.
Note that \n is the notation for the newline character, which
is a single character.
myMessageLen = . - myMessage
This line defines another symbolic name, myMessageLen. This
symbol, unlike myMessage, is not a memory location label. Instead,
it is just a symbolic name defined to be the value of
. - myMessage.
The dot (.) represents ``the current address''. At this point,
the current address is that of the location immediately after
the last byte of the ASCII string. In other words, the location right
after the newline character.
As a result, the difference . - myMessage is actually the length
of the ASCII string. This is a nifty definition because we don't have
to manually count the number of characters in the message.
.text
This is telling the assembler that we are about to specify code.
.global _start
This is our usual line to tell the assembler to ``export'' the name
and definition of _start.
_start:
This defines the label _start at the beginning of our program.
movl $myMessageLen, %edx
We need to specify the number of characters to output in %edx.
movl $myMessage, %ecx
This specifies the address of the first byte to output. In this
program, we use myMessage because we have an ASCII string
defined at that location.
movl $1, %ebx
%ebx specifies the handle of the output file. 1 is the value for
the standard output file (stdout).
movl $4, %eax
%eax specifies the system service. 4 means we want the operating
system to output a number of characters (%edx), starting at a
particular location (%ecx) to a particular file (%ebx).
int $0x80
Once we set up parameters, we use the software interrupt instruction
int to request service. Note that all kernel services
are requested via software interrupt 128 (hexadecimal 0x80).
movl $1, %eax
Set up %eax for the program exit request.
movl $0, %ebx
Specifies an exit code of 0 (normal).
int $0x80
Request the operating system to terminate the program
Copyright © 2009-04-16 by Tak Auyeung