hello world.s
.data
msg: .asciiz "Hello World\n"
.text
.globl main
main:
la $a0, msg #load message
li $v0, 4 #load immediate
syscall #print it
li $v0, 10
syscall #exit
Example: Recursive Factorial in MIPS
# int fact(int n): return n <= 0 ? 1 : n * fact(n-1);
fact:
addi $sp, $sp, -8 # space for two words
sw $ra, 4($sp) # save return address
sw $a0, 0($sp) # temporary variable to
hold n
li $v0, 1 #($v0)=1
ble $a0, $zero, fact_return
addi $a0, $a0, -1
jal fact
lw $a0, 0($sp) # retrieve original n
mul $v0, $v0, $a0 # n * fact(n - 1)
fact_return:
lw $ra 4($sp) # restore $ra
addi $sp, $sp, 8 # restore $sp
jr $ra # back to caller