LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Blogs > In The Middle Of Nowhere [edK's blog]
User Name
Password

Notices

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 10: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):
Code:
root@router:/opt/src/asm# as -o exit.o exit.s;ld -s -o exit exit.o
root@router:/opt/src/asm# ./exit;echo $?
16
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
	.text
	.global __start

__start:
	li $a0, 16
	li $v0, 4001 # __NR_exit <asm/unistd.h>
	syscall
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:

Code:
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
1
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
	.data
ky:
	.asciiz "Hi World\n"
	
	.text
	.global __start

__start:
	li $a2, 9
	la $a1, ky
	li $a0, 1
	li $v0, 4004 # __NR_write
	syscall
	
	# $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
	syscall
Now, go and take some time reading MIPS assembly documentation, if you feel interested!
Posted in etc, hardware
Views 858 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



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

Main Menu
Advertisement

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