Changes

Jump to: navigation, search

Assembler Basics

1,806 bytes added, 01:38, 24 January 2014
no edit summary
<font color="blue">msg:</font> <font color="red">.ascii</font> <font color="orange">"Hello, world!\n"</font>
<font color="red">.set</font> <font color="blue">len</font> = <font color="orange">. - msg</font>
In this program, which was written using GNU Assembler (gas) syntax, text is coloured according to its type:
* <font color="blue">symbols</font>
* <font color="orange">expressions</font>
 
A symbol may be set in one of two ways:
# Using a directive (in the example above, len line), or
# As a label (such as _start or msg in the example above). A label is identified by the trailing semi-colon, and is set to the current memory location in the instruction or data sequence. Labels may be used for loading/storing information, or as the target of branches/jumps.
 
In the program above:
* .start is a directive (equivalent to the longer directive ".section .start") which specifies that the following instructions/data should be placed in the ".start" section of the output ELF file.
* .global (or .globl) is a directive which makes the following symbol visible to the linker. Otherwise, symbols are normally lost by link time. In this case, the linker needs to know the value of the special symbol _start in order to know where execution is to begin in the program (which is not always at the start of the .text section).
* .set is a directive which sets a symbol (len) equal to the value of an expression (in this example, ". - msg" meaning the current memory location minus the value of the label "msg").
* _start is a label which is equivalent to the memory location of the first instruction in the program.
* msg is a label which is equivalent to the memory location of the first byte of the string "Hello, World!"
 
Note that symbols are not variables - they are constants that are calculated at compile-time.
 
Note also that the syntax will vary from assembler to assembler and from architecture to architechture.
 
== Instruction Set Architecture Information ==
 
To get started with specific [[Instruction Set Architecture|instruction set architectures]], see:
* [[x86_64 Register and Instruction Quick Start]]
* [[aarch64 Register and Instruction Quick Start]]

Navigation menu