Changes

Jump to: navigation, search

Assembler Basics

2,451 bytes added, 00:17, 24 January 2014
Created page with '{{Chris Tyler Draft}}Category:Assembler When you program in assembly language, you're directly programming the "bare metal" hardware. This means that many of th…'
{{Chris Tyler Draft}}[[Category:Assembler]]
When you program in [[Assembler|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:
# Labels - [[Symbol|symbols]] that correspond to memory locations.
# 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.

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

<font color="red">.text</font>
<font color="red">.global</font> <font color="blue">_start</font>

<font color="blue">_start:</font>
movq $<font color="blue">len</font>,%rdx /* message length */
movq $<font color="blue">msg</font>,%rsi /* message location */
movq $1,%rdi /* file descriptor stdout */
movq $1,%rax /* syscall sys_write */
syscall

movq $<font color="orange">0</font>,%rdi /* exit status */
movq $<font color="orange">60</font>,%rax /* syscall sys_exit */
syscall

<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">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="red">directives</font>
* <font color="blue">symbols</font>
* <font color="orange">expressions</font>

Navigation menu