The GNU Assembler

Nios II uses the GNU Assembler for translating machine instructions written in text to binary files. Below is an example program, study its syntax and comments very carefully.

You may find the full GNU Assembler Manual useful as a reference. Specifically there's the section on Assembler Directives, as well as a section on Syntax.

Excessively-Commented Example Program

/* This is a comment */

.equ LEDS_RED, 0x10000000  /* Declare a symbol for the address of the red LEDs */

/***************************** Program Code ***********************************/

.text                       /* Put everything below in the .text section */
                            /* You don't actually need this, .text is assumed */
                            /* Program code goes in .text, data goes in .data */

.global _start              /* Make the "_start" label visible to other files */

_start:                     /* All programs start at "_start", you MUST have it */

  movia r4,myshort          /* Get the address of myshort */
  ldh   r3,(r4)             /* Load the value at myshort (0x0123) */
  movia r2,LEDS_RED         /* Get the address of the red LEDs */
  sthio r3,0(r2)            /* Write the value of myint to the red LEDs */
  ret                       /* Return - end _start function */

/********************************* Data ***************************************/

.data                       /* Put everything below in .data section */

/***
 * VARIABLES
 * =========
 *
 * Declare three variables and initialize their contents.  Also make sure they
 * are aligned properly (half-words should start at addresses that are
 * multiples of 2, words should start at multiples of 4).
 ***/

mychar:
.byte  0x01                 /* An 8-bit variable named mychar */

.align 1                    /* Align to a 2-byte (2^1) boundary */
myshort:
.hword 0x0123               /* A 16-bit variable named myshort */

.align 2                    /* Align to a 4-byte (2^2) boundary */
myint:
.word  0x01234567           /* A 32-bit variable named myint */

/***
 * STRINGS
 * =======
 *
 * Declare a label called "hellostring" which points to the zero-terminated
 * string "Hello World"
 ***/

hellostring:
.string "Hello World"

/***
 * ARRAYS
 * =======
 *
 * Declare "array_initialized" with 10 32-bit words, also declare an 
 * uninitialized array called "array_uninitialized" with space for 10
 * 32-bit words (40 bytes).
 ***/

.align 2
array_initialized:
.word 5,3,6,7,2,8,7,1,9,5
array_uninitialized:
.skip 40