Help answer threads with 0 replies.
Go Back > Blogs > In The Middle Of Nowhere [edK's blog]
User Name


In The Middle Of Nowhere

Life, universe and everything...
Blog title (C) Orbital
Rate this Entry

More fun around MIPS

Posted 01-29-2011 at 09:54 AM by Web31337

I thought I finally have to try this myself at last, so I logged in to my Netgear router and started experiments around MIPS assembly.

It isn't much, though, I think I love this architecture It's really easy comparing to x86.

I guess I started at the wrong place. My first step was googling for "MIPS linux syscall", I was trying to find some easy sample code in order to understand how it works and create "hello world" myself. But MIPS is so easy, you can start right from reading it's docs. It took me about 20 minutes to get into the basics and write own version of exit syscall. Don't be surprised when you see how disassembly looks like: yes, every instruction is four-bytes.
Well, this is how I made simple exit(16):
root@router:/opt/src/asm# as -o exit.o exit.s;ld -s -o exit exit.o
root@router:/opt/src/asm# ./exit;echo $?
root@router:/opt/src/asm# ls -la exit
-rwxr-xr-x    1 root     root           348 Jan 29 19:43 exit
root@router:/opt/src/asm# objdump -d exit

exit:     file format elf32-tradlittlemips

Disassembly of section .text:

00400090 <.text>:
  400090:	24040010 	li	a0,16
  400094:	24020fa1 	li	v0,4001
  400098:	0000000c 	syscall
  40009c:	00000000 	nop
root@router:/opt/src/asm# cat exit.s
	.global __start

	li $a0, 16
	li $v0, 4001 # __NR_exit <asm/unistd.h>
It's that simple. syscall is an actual instruction. That long name confused me a bit when I just started looking for code, I thought that has something to do with imported function from stdlib
And next, the simple "Hi world" application:

root@router:/opt/src/asm# as -o hello.o hello.s;ld -s -o hello hello.o
root@router:/opt/src/asm# ./hello;echo $?
Hi World
root@router:/opt/src/asm# ls -l hello
-rwxr-xr-x    1 root     root           456 Jan 29 20:16 hello
root@router:/opt/src/asm# objdump -d hello

hello:     file format elf32-tradlittlemips

Disassembly of section .text:

004000b0 <.text>:
  4000b0:	24060009 	li	a2,9
  4000b4:	3c050041 	lui	a1,0x41
  4000b8:	24a500d0 	addiu	a1,a1,208
  4000bc:	24040001 	li	a0,1
  4000c0:	24020fa4 	li	v0,4004
  4000c4:	0000000c 	syscall
  4000c8:	24020fa1 	li	v0,4001
  4000cc:	0000000c 	syscall
root@router:/opt/src/asm# cat hello.s
	.asciiz "Hi World\n"
	.global __start

	li $a2, 9
	la $a1, ky
	li $a0, 1
	li $v0, 4004 # __NR_write
	# $a0 is not reset after syscall, so that results in exiting with 1
	# but if you like, you can uncomment the following line, so it will exit with 9, the number of written bytes, return value from previous syscall
	#move $a0, $v0
	li $v0, 4001 # __NR_exit
Now, go and take some time reading MIPS assembly documentation, if you feel interested!
Posted in etc, hardware
Views 1512 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 12:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration