readbin

Download readbin.zip.

These programs shows how to read a binary file produced by the Nios II objcopy utility.

readbin.cpp is a C++ example. readbin.java is a Java example.

mifwrite.java produces an Altera .mif (memory intialization file).

Producing a Binary File

Consider the following example program.

int main()
{
  register int a, b, c;
  int d, e, f;
  a = 70000;
  b = 69995;
  c = a - b;
  d = 14;
  e = 11;
  f = d + e;
  return (f>c? c: f);
}

We can compile it in the Nios II IDE environment producing an .elf file. This file can then be disassembled from the Nios II command shell via:

   nios2-elf-objdump -dS blank_project_1.elf > proj1_disassemble.txt

An excerpt of the resulting file is shown below

0000020c <main>:
int main()
{
     20c:	defffa04 	addi	sp,sp,-24
     210:	df000515 	stw	fp,20(sp)
     214:	d839883a 	mov	fp,sp
  register int a, b, c;
  int d, e, f;
  a = 70000;
     218:	00c00074 	movhi	r3,1
     21c:	18c45c04 	addi	r3,r3,4464
  b = 69995;
     220:	00800074 	movhi	r2,1
     224:	10845ac4 	addi	r2,r2,4459
  c = a - b;
     228:	1887c83a 	sub	r3,r3,r2
     22c:	e0c00315 	stw	r3,12(fp)
  d = 14;
     230:	00800384 	movi	r2,14

The following shell command will generate a binary version of the executable:

   nios2-elf-objcopy -S -O binary blank_project_1.elf proj1.bin

C++ Program

readbin.cpp is a C++ program that reads binary files. The left column below is from readbin. The right column is the disassembler output.

readbin outputdisassembler output
0000020c : defffa04
00000210 : df000515
00000214 : d839883a
00000218 : 00c00074
0000021c : 18c45c04
00000220 : 00800074
00000224 : 10845ac4
00000228 : 1887c83a
0000022c : e0c00315
00000230 : 00800384
00000234 : e0800015
00000238 : 008002c4
0000020c 
: int main() { 20c: defffa04 210: df000515 214: d839883a register int a, b, c; int d, e, f; a = 70000; 218: 00c00074 21c: 18c45c0 b = 69995; 220: 00800074 224: 10845ac4 c = a - b; 228: 1887c83a 22c: e0c00315 d = 14; 230: 00800384

Java program

readbin.java is a Java example.

The first argument is the input binary file. The second argument is the hex offset into the file where output is to be generated. Compare to the disassembler output above.

>java readbin proj1.bin 20c
0000020c : defffa04
00000210 : df000515
00000214 : d839883a
00000218 : 00c00074
0000021c : 18c45c04
00000220 : 00800074
00000224 : 10845ac4
00000228 : 1887c83a
0000022c : e0c00315
00000230 : 00800384
00000234 : e0800015
00000238 : 008002c4
0000023c : e0800115
00000240 : e0c00017
00000244 : e0800117
00000248 : 1885883a
0000024c : e0800215

Writing MIF Files (Java program)

mifwrite.java produces an Altera .mif (memory intialization file).


Maintained by John Loomis, last updated 3 February 2007