Difference between revisions of "Assembler Basics"

From CDOT Wiki
Jump to: navigation, search
(Format of an Assembly Language program)
(Format of an Assembly Language program)
Line 5: Line 5:
  
 
An assembly-language program consists of:
 
An assembly-language program consists of:
# Labels - [[Symbol|symbols]] that correspond to memory locations.
+
# [[Symbol|symbols]] that correspond to memory locations.
 
# Instructions - Mnemonics for an operation followed by zero or more arguments.
 
# Instructions - Mnemonics for an operation followed by zero or more arguments.
 
# Data - Values used by the program for things such as initial variable values and string or numeric constants.
 
# Data - Values used by the program for things such as initial variable values and string or numeric constants.
Line 29: Line 29:
 
  <font color="red">.data</font>
 
  <font color="red">.data</font>
 
   
 
   
  <font color="blue">msg:</font>    <font color="red">.ascii</font>      <font color="orange">Hello, world!\n"</font>
+
  <font color="blue">msg:</font>    <font color="red">.ascii</font>      <font color="orange">"Hello, world!\n"</font>
 
  <font color="blue">len</font> = <font color="orange">. - msg</font>
 
  <font color="blue">len</font> = <font color="orange">. - msg</font>
  

Revision as of 00:21, 24 January 2014

Important.png
This is a draft only!
It is still under construction and content may change. Do not rely on this information.

When you program in assembly language, you're directly programming the "bare metal" hardware. This means that many of the compile-time and run-time checks, error messages, and diagnostics are not available. The computer will follow your instructions exactly, even if they are completely wrong (like executing data), and when something goes wrong, your program won't terminate until it tries to do something that's not permitted, such as execute an invalid opcode or attempt to access a protected or unmapped region of memory. When that happens, the CPU will signal an exception, and in most cases the operating system will shut down the offending process.

Format of an Assembly Language program

An assembly-language program consists of:

  1. symbols that correspond to memory locations.
  2. Instructions - Mnemonics for an operation followed by zero or more arguments.
  3. Data - Values used by the program for things such as initial variable values and string or numeric constants.

Assembler directives are used to control the assembly of the code, by specifying output file sections (such as .text or .data) and data formats (such as word size for numeric values), and defining macros.

Consider this

.text
.global  _start

_start:
        mov    $len,%rdx                       /* message length */
        mov    $msg,%rsi                       /* message location */
        mov    $1,%rdi                         /* file descriptor stdout */
        mov    $1,%rax                         /* syscall sys_write */
        syscall

        mov    $0,%rdi                         /* exit status */
        mov    $60,%rax                        /* syscall sys_exit */
        syscall

.data

msg:    .ascii      "Hello, world!\n"
len = . - msg

In this program, which was written using GNU Assembler (gas) syntax, text is coloured according to its type:

  • directives
  • symbols
  • expressions