MIPS Programming Manual for COA (15B11CI313)
by
Dr. Hema N
Kindly download the Mar4.5 simulator form
[Link]
In mars to enable pseudo code, following options to be selected from settings
MIPS Manual 5th Sem CSE/IT
Example1: ADD
li $t0, 1 #This is to load the immediate value of 1 into the temporary register $t0
li $t1, 2 #This is to load the immediate value of 2 into the temporary register $t1
li $t6, 0 #This is to load the immediate value of 0 into the temporary register $t6
add $t6,$t0,$t1 #this adds the values stored in $t0 and $t1 and assigns them to t6
Example 2: SUB
li $t0, 4 #This is to load the immediate value of 4 into the temporary register $t0
li $t1, 2 #This is to load the immediate value of 2 into the temporary register $t1
li $t6, 0 #This is to load the immediate value of 0 into the temporary register $t6
sub $t6,$t0,$t1 #this sub the values stored in $t0 and $t1 and assigns them to t6
Example 3: Program in MIPS to add two numbers and print on the screen
.text
li $s0,0x16
li $s1,0x17
addu $s2,$s0,$s1
add $a0,$0,$s2. #load the sum
li $v0,1
syscall #print on the screen
li $v0,0x10
syscall #Terminate the program
ODD SEM 2020 Dr. Hema N
MIPS Manual 5th Sem CSE/IT
Example 4: Program to read word (of size 32-bit) from the memory and print on the screen
.data
num1: .word 12345678
.text
lw $a0,num1 # number load into $a0
li $v0,1
syscall #print on the screen
Example 5: Program to read word and string from the memory and print on the screen
.data
num1: .word 12345678
num2: .asciiz "\n hello world \n"
.text
lw $a0,num1
li $v0,1
syscall #print word on screen
la $a0,num2
li $v0,4
syscall #print string on screen
ODD SEM 2020 Dr. Hema N
MIPS Manual 5th Sem CSE/IT
li $a0,10
syscall
Example 6: To read an integer from the user and add with 50H and print the sum on the
screen.
.text
li $v0,5
syscall # read number and store at V0
addi $a0,$v0,0x50 # number to printed to be in $a0
li $v0,1
syscall
ODD SEM 2020 Dr. Hema N
MIPS Manual 5th Sem CSE/IT
ODD SEM 2020 Dr. Hema N
MIPS Manual 5th Sem CSE/IT
Example 7: Add the number 0 to 9,
// addthenumbersfrom 0 to9
intsum= 0;
inti;
for(i = 0; i != 10; i = i+1) {
sum= sum+ i;
}
.data
null: .asciiz "\n"
.text
# $s0 = i, $s1 = sum
addi $s1, $0, 0
add $s0, $0, $0
addi $t0, $0, 10
for: beq $s0, $t0, done
add $s1, $s1, $s0
add $a0,$0,$s1 # print i
li $v0,1
syscall
ODD SEM 2020 Dr. Hema N
MIPS Manual 5th Sem CSE/IT
la $a0,null
li $v0,4
syscall
addi $s0, $s0,1
add $a0,$0,$s0 # print sum
li $v0,1
syscall
la $a0,null
li $v0,4
syscall
j for
done:
Reference:
[Link]
[Link]
[Link]
[Link]
ODD SEM 2020 Dr. Hema N