This assembly program includes examples of arithmetic (add, sub) and logic statements. Values are set using immediate operands.

This shell script ( assembles the source to machine language, runs the disassembler, runs the instruction-set simulator, and converts the output (a.out) to binary (isa1.bin). The C++ program (mifwrite.exe) generates a (.mif) of the result.

The techniques described here are an alternative to using the Nios II IDE to run programs that explore features of the Nios II architecture. We show how to run the compiler and the instruction set simulator (ISS) directly from the Nios II Command Shell. We also show how to generate a Memory Initialization File (MIF) for transfer to the Altera Cyclone II trip.


Shell script
Disassembler output
Size summary


To run this program from the Nios II IDE, we would change the .include directive to

.include "../nios_macros.m"
because the IDE runs scripts one directory below the source directory.

To run this program from the command shell, wc can type

nios2-elf-gcc isa1.s
This calls the GNU C-compiler directly, using a default cpu configuration and creating an output ELF file called a.out. Because this command is usually run from the source directory, we should change the .include directive to
.include "./nios_macros.m"
We recommend running the instruction set simulator with the following command
nios2-iss -td:isa1.sim.txt --trace-from=main -f a.out
output shows each instruction executed, the destination value, and the destination register. This is usually all the information needed to verify the operation of a Nios II cpu development system.

Other options for the instruction set simulator can be listed by entering nios-iss -h on the command line.

MIF Output

To generate an Altera memory initialization file (mif), we copy the ELF a.out to a binary format (bin) and then convert that to mif format.

The following command will generate a binary file:

nios2-elf-objcopy -S -O binary a.out isa1.bin
which generates the binary output file isa1.bin. Then the command
mifwrite.exe isa1.bin isa1.mif 30
will generate a memory initialization file called isa1.mif. Generally the main program starts at an offset of 0x30.

The mif conversion program (mifwrite.exe) should be written to a directory (like c:\bin) that has been placed in the default search path. If you do not wish to createa a seperate directory, you can copy the file to C:\WINDOWS, which is generally in the path. You may also copy the file to the current directory, in which case it can be executed by typing

./mifwrite.exe isa1.bin isa1.mif 30
where isa1.mif is the designated output file.

The easiest way to cycle through a set of commnads is to use a command script, such as, which can be run by entering


Source: isa1.s

.include "./nios_macros.m"

.global main
.type main, @function

    movi    r2,10        # a = 10
    movi    r3,14        # b = 14
    sub    r4,r3,r2    # c = b - a
    movi    r6,11        # e = 11
    movi    r7,6        # f = 6
        /* g = (b + c) - (e + f); */
    add    r8,r3,r4    # g = (b+c)
    add    r9,r7,r8    # tmp = (e+f)
    sub    r8,r8,r9    # g = g - tmp
    addi    r9,r8,3        # h = g + 3
        /* do some logic operations */
    and    r4,r2,r3    # c = a & b;
    or    r5,r2,r3    # d = a | b;
    nor    r6,r2,r3    # e = ~(a | b);
    xor    r7,r2,r3    # f = a ^ b;
    andi    r8,r2,0x3f    # g = a & 0x3f;
    br    end        /* wait here once the program has completed */

Shell script

nios2-elf-gcc ${name}.s
nios2-elf-size a.out > ${name}.size.txt
nios2-elf-objdump -dS a.out > ${name}.disassemble.txt
nios2-elf-objdump -h a.out > ${name}.headers.txt
nios2-elf-nm a.out > ${name}.symbols.txt
nios2-elf-objcopy -S -O binary a.out ${name}.bin
nios2-iss -td:${name}.sim.txt --trace-from=main -f a.out
mifwrite ${name}.bin ${name}.mif 30

Disassembler output

a.out:     file format elf32-littlenios2

Disassembly of section .text:

00000000 <_start>:
   0:    06c20034     movhi    sp,2048
   4:    deffc004     addi    sp,sp,-256
   8:    def6303a     nor    sp,sp,sp
   c:    dec001d4     ori    sp,sp,7
  10:    def6303a     nor    sp,sp,sp
  14:    06800074     movhi    gp,1
  18:    d6a17804     addi    gp,gp,-31264
  1c:    06000034     movhi    et,0
  20:    c6017c04     addi    et,et,1520
  24:    00800034     movhi    r2,0
  28:    10801c04     addi    r2,r2,112
  2c:    1000683a     jmp    r2

00000030 <main>:
  30:    00800284     movi    r2,10
  34:    00c00384     movi    r3,14
  38:    1889c83a     sub    r4,r3,r2
  3c:    018002c4     movi    r6,11
  40:    01c00184     movi    r7,6
  44:    1911883a     add    r8,r3,r4
  48:    3a13883a     add    r9,r7,r8
  4c:    4251c83a     sub    r8,r8,r9
  50:    424000c4     addi    r9,r8,3
  54:    10c8703a     and    r4,r2,r3
  58:    10cab03a     or    r5,r2,r3
  5c:    10cc303a     nor    r6,r2,r3
  60:    10cef03a     xor    r7,r2,r3
  64:    12000fcc     andi    r8,r2,63

00000068 <end>:
  68:    003fff06     br    68 <end>

0000006c <__fake_init>:
  6c:    f800283a     ret

00000070 <__start_2>:
  70:    01000034     movhi    r4,0
  74:    21017c04     addi    r4,r4,1520
  78:    01800034     movhi    r6,0
  7c:    31817c04     addi    r6,r6,1520
  80:    deffff04     addi    sp,sp,-4
  84:    310dc83a     sub    r6,r6,r4
  88:    000b883a     mov    r5,zero
  8c:    dfc00015     stw    ra,0(sp)
  90:    00000b00     call    b0 <_zero_bss>
  94:    000006c0     call    6c <__fake_init>
  98:    0009883a     mov    r4,zero
  9c:    000b883a     mov    r5,zero
  a0:    000d883a     mov    r6,zero
  a4:    00000300     call    30 <main>
  a8:    1009883a     mov    r4,r2
  ac:    00000bc0     call    bc <exit>

000000b0 <_zero_bss>:
  b0:    02000034     movhi    r8,0
  b4:    42006804     addi    r8,r8,416
  b8:    4000683a     jmp    r8

000000bc <exit>:
  bc:    d0a00317     ldw    r2,-32756(gp)
  c0:    defff804     addi    sp,sp,-32
  c4:    dd800015     stw    r22,0(sp)
  c8:    dfc00715     stw    ra,28(sp)
  cc:    dc000615     stw    r16,24(sp)
  d0:    dc400515     stw    r17,20(sp)
  d4:    dc800415     stw    r18,16(sp)
  d8:    dcc00315     stw    r19,12(sp)
  dc:    dd000215     stw    r20,8(sp)
  e0:    dd400115     stw    r21,4(sp)
  e4:    15005217     ldw    r20,328(r2)
  e8:    202d883a     mov    r22,r4
  ec:    a0002526     beq    r20,zero,184 <exit+0xc8>
  f0:    a0800117     ldw    r2,4(r20)
  f4:    a5402204     addi    r21,r20,136
  f8:    0027883a     mov    r19,zero
  fc:    143fffc4     addi    r16,r2,-1
 100:    8006803a     cmplt    r3,r16,zero
 104:    1800021e     bne    r3,zero,110 <exit+0x54>
 108:    00800044     movi    r2,1
 10c:    1426983a     sll    r19,r2,r16
 110:    1800191e     bne    r3,zero,178 <exit+0xbc>
 114:    800490ba     slli    r2,r16,2
 118:    1025883a     mov    r18,r2
 11c:    1505883a     add    r2,r2,r20
 120:    14400204     addi    r17,r2,8
 124:    00000806     br    148 <exit+0x8c>
 128:    88800017     ldw    r2,0(r17)
 12c:    29400017     ldw    r5,0(r5)
 130:    843fffc4     addi    r16,r16,-1
 134:    94bfff04     addi    r18,r18,-4
 138:    103ee83a     callr    r2
 13c:    8c7fff04     addi    r17,r17,-4
 140:    9827d07a     srai    r19,r19,1
 144:    80000c16     blt    r16,zero,178 <exit+0xbc>
 148:    a8802017     ldw    r2,128(r21)
 14c:    954b883a     add    r5,r18,r21
 150:    b009883a     mov    r4,r22
 154:    14c4703a     and    r2,r2,r19
 158:    103ff31e     bne    r2,zero,128 <exit+0x6c>
 15c:    88800017     ldw    r2,0(r17)
 160:    843fffc4     addi    r16,r16,-1
 164:    94bfff04     addi    r18,r18,-4
 168:    103ee83a     callr    r2
 16c:    8c7fff04     addi    r17,r17,-4
 170:    9827d07a     srai    r19,r19,1
 174:    803ff40e     bge    r16,zero,148 <exit+0x8c>
 178:    a5000017     ldw    r20,0(r20)
 17c:    a03fdc1e     bne    r20,zero,f0 <exit+0x34>
 180:    d0a00317     ldw    r2,-32756(gp)
 184:    10c00f17     ldw    r3,60(r2)
 188:    1800021e     bne    r3,zero,194 <exit+0xd8>
 18c:    b009883a     mov    r4,r22
 190:    00002400     call    240 <_exit>
 194:    1009883a     mov    r4,r2
 198:    183ee83a     callr    r3
 19c:    003ffb06     br    18c <exit+0xd0>

000001a0 <memset>:
 1a0:    008000c4     movi    r2,3
 1a4:    29403fcc     andi    r5,r5,255
 1a8:    2007883a     mov    r3,r4
 1ac:    11801a2e     bgeu    r2,r6,218 <memset+0x78>
 1b0:    2084703a     and    r2,r4,r2
 1b4:    1000181e     bne    r2,zero,218 <memset+0x78>
 1b8:    2806923a     slli    r3,r5,8
 1bc:    200f883a     mov    r7,r4
 1c0:    1946b03a     or    r3,r3,r5
 1c4:    1804943a     slli    r2,r3,16
 1c8:    1886b03a     or    r3,r3,r2
 1cc:    008003c4     movi    r2,15
 1d0:    11800a2e     bgeu    r2,r6,1fc <memset+0x5c>
 1d4:    38c00015     stw    r3,0(r7)
 1d8:    39c00104     addi    r7,r7,4
 1dc:    38c00015     stw    r3,0(r7)
 1e0:    39c00104     addi    r7,r7,4
 1e4:    38c00015     stw    r3,0(r7)
 1e8:    39c00104     addi    r7,r7,4
 1ec:    38c00015     stw    r3,0(r7)
 1f0:    31bffc04     addi    r6,r6,-16
 1f4:    39c00104     addi    r7,r7,4
 1f8:    11bff636     bltu    r2,r6,1d4 <memset+0x34>
 1fc:    008000c4     movi    r2,3
 200:    1180042e     bgeu    r2,r6,214 <memset+0x74>
 204:    38c00015     stw    r3,0(r7)
 208:    31bfff04     addi    r6,r6,-4
 20c:    39c00104     addi    r7,r7,4
 210:    11bffc36     bltu    r2,r6,204 <memset+0x64>
 214:    3807883a     mov    r3,r7
 218:    31bfffc4     addi    r6,r6,-1
 21c:    00bfffc4     movi    r2,-1
 220:    30800426     beq    r6,r2,234 <memset+0x94>
 224:    19400005     stb    r5,0(r3)
 228:    31bfffc4     addi    r6,r6,-1
 22c:    18c00044     addi    r3,r3,1
 230:    30bffc1e     bne    r6,r2,224 <memset+0x84>
 234:    2005883a     mov    r2,r4
 238:    f800283a     ret

0000023c <__fake_fini>:
 23c:    f800283a     ret

00000240 <_exit>:
 240:    defffe04     addi    sp,sp,-8
 244:    dc400015     stw    r17,0(sp)
 248:    dfc00115     stw    ra,4(sp)
 24c:    2023883a     mov    r17,r4
 250:    000023c0     call    23c <__fake_fini>
 254:    8805883a     mov    r2,r17
 258:    003fff06     br    258 <_exit+0x18>
 25c:    dfc00117     ldw    ra,4(sp)
 260:    dc400017     ldw    r17,0(sp)
 264:    dec00204     addi    sp,sp,8
 268:    f800283a     ret


   text       data        bss        dec        hex    filename
    622        896          0       1518        5ee    a.out


a.out:     file format elf32-littlenios2

Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         0000026c  00000000  00000000  00000074  2**3
  1 .rodata       00000002  0000026c  0000026c  000002e0  2**2
  2 .data         0000037c  00000270  00000270  000002e2  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  3 .sdata        00000004  000005ec  000005ec  0000065e  2**2
  4 .sbss         00000000  000005f0  000005f0  00000662  2**0
                  CONTENTS, SMALL_DATA
  5 .bss          00000000  000005f0  000005f0  00000662  2**0
  6 .comment      00000108  00000000  00000000  00000662  2**0
                  CONTENTS, READONLY
  7 .debug_aranges 000000a0  00000000  00000000  0000076a  2**0
  8 .debug_pubnames 000000d6  00000000  00000000  0000080a  2**0
  9 .debug_info   00001499  00000000  00000000  000008e0  2**0
 10 .debug_abbrev 000004c2  00000000  00000000  00001d79  2**0
 11 .debug_line   00000a02  00000000  00000000  0000223b  2**0
 12 .debug_frame  000000e4  00000000  00000000  00002c40  2**2
 13 .debug_str    0000068a  00000000  00000000  00002d24  2**0


000005f0 A __bss_start
0000023c T __fake_fini
0000006c T __fake_init
00000070 T __start_2
000005f0 A _edata
000005f0 A _end
00000240 T _exit
0000023c W _fini
000085e0 A _gp
000005ec G _impure_ptr
0000006c W _init
00000001 a _nios2_macros_
00000000 T _start
000000b0 T _zero_bss
00000068 t end
000000bc T exit
00000270 d impure_data
00000030 T main
000001a0 T memset
07ffff00 A nasys_stack_top

See key for the meaning of the symbol type characters.


0x00000030 <main>:         0x00800284  movi r2, 10 [dstData=0xa dstReg=r2]
0x00000034 <main+0x4>:     0x00c00384  movi r3, 14 [dstData=0xe dstReg=r3]
0x00000038 <main+0x8>:     0x1889c83a  sub r4, r3, r2 [dstData=0x4 dstReg=r4]
0x0000003c <main+0xc>:     0x018002c4  movi r6, 11 [dstData=0xb dstReg=r6]
0x00000040 <main+0x10>:    0x01c00184  movi r7, 6 [dstData=0x6 dstReg=r7]
0x00000044 <main+0x14>:    0x1911883a  add r8, r3, r4 [dstData=0x12 dstReg=r8]
0x00000048 <main+0x18>:    0x3a13883a  add r9, r7, r8 [dstData=0x18 dstReg=r9]
0x0000004c <main+0x1c>:    0x4251c83a  sub r8, r8, r9 [dstData=0xfffffffa dstReg=r8]
0x00000050 <main+0x20>:    0x424000c4  addi r9, r8, 3 [dstData=0xfffffffd dstReg=r9]
0x00000054 <main+0x24>:    0x10c8703a  and r4, r2, r3 [dstData=0xa dstReg=r4]
0x00000058 <main+0x28>:    0x10cab03a  or r5, r2, r3 [dstData=0xe dstReg=r5]
0x0000005c <main+0x2c>:    0x10cc303a  nor r6, r2, r3 [dstData=0xfffffff1 dstReg=r6]
0x00000060 <main+0x30>:    0x10cef03a  xor r7, r2, r3 [dstData=0x4 dstReg=r7]
0x00000064 <main+0x34>:    0x12000fcc  andi r8, r2, 63 [dstData=0xa dstReg=r8]
0x00000068 <end>:          0x003fff06  br 0x68 <end>

Maintained by John Loomis, last updated Tue Feb 12 19:19:51 2008