My Blog

Cross-compile GNU assembler for MIPS

PDF ·

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:

_start:
	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

To:

0x2042007b
0x20630141
0x00432020
0x20a50001
0x20c60002
0x00a63825
0x2108000f
0x21290003
0x01095024

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

wget ftp://sourceware.org/pub/binutils/snapshots/binutils-2.22.52.tar.bz2
tar xjf binutils-2.22.52.tar.bz2
cd binutils-2.22.52
mkdir build
../configure --target=mips --prefix=${HOME}/bin/mips
make
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.

#software engineering #tools