Cross-compile GNU assembler for MIPS

by Wojciech Adam Koszek   ⋅   Jun 10, 2012   ⋅   East Palo Alto, CA

Technical post which you'll find useful if you study the details of the MIPS architecture, maybe from Patterson and Hennessy books.

Just a short note, not to have to walk through the Web each time I want to get some strange opcodes generated.

Today “strange” means “MIPS”. “Nobody uses MIPS”, you say? Well – I still believe MIPS is the cleanest RISC architecture available today. So yes, I want to have something translate:

	addi	$2, $2, 123
	addi	$3, $3, 321
	add	$4, $2, $3

	addi	$5, $5, 1
	addi	$6, $6, 2
	or	$7, $5, $6

	addi	$8, $8, 0xf
	addi	$9, $9, 0x3
	and	$10, $8, $9



For me. So first let me present steps for building the GNU assembler for MIPS. Steps:

tar xjf binutils-2.22.52.tar.bz2
cd binutils-2.22.52
mkdir build
../configure --target=mips --prefix=${HOME}/bin/mips
make install
set path=($path ${HOME}/bin/mips/bin)

Now that I have MIPS assembler, I have created a simple makefile for myself, which translates opcodes from generated object file to hexadecimal opcodes:

imem.hex: imem.s
	mips-as -o imem.o imem.s
	mips-objcopy -O binary imem.o imem.bin
	mips-objdump -d imem.o > imem.dump
	sed '1,7d' imem.dump | awk '{ print "0x"$$2 }' > imem.hex

Thanks to these several steps, I have a simple infrastructure to play with MIPS opcodes.

Subscribe for updates

Once a month I send updates on the new content and hints for software engineers.

Liked it? Share it!

About the author: I'm Wojciech Adam Koszek. I like software, business and design. Poland native. In Bay Area since 2010.   More about me