100% found this document useful (1 vote)
2K views5 pages

MIPS Instruction Comments and Examples

The document contains MIPS instructions and their descriptions. It provides examples of arithmetic, logical, branching, memory access, and immediate instructions. It also includes examples of converting between MIPS assembly code and machine code.

Uploaded by

Rajeen Venura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views5 pages

MIPS Instruction Comments and Examples

The document contains MIPS instructions and their descriptions. It provides examples of arithmetic, logical, branching, memory access, and immediate instructions. It also includes examples of converting between MIPS assembly code and machine code.

Uploaded by

Rajeen Venura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

For each of the following MIPS instructions, write comments that explain what the

instruction does. The first line is completed for you as an example:

add $v0, $t0, $t1 #$v0 = $t0 + $t1


addi $s0, $t3, -5 #$s0 = $t3 + (-5) [interprets immediate value as signed number]
addiu $s1, $s0, 5 #$s1 = $s0 + 5 [interprets immediate value as an unsigned number]
and $t0, $t1, $t2 #$t0 = $t1 & $t2 [bitwise AND operation]
andi $v1, $a0, 0xFF0F #$v1 = ($a0 & 0xFF0F) [bitwise AND with immediate value]
beq $t5, $t4, loop #if $t5 == $t4, then branch to the label ‘loop’
bne $s5, $s4, loop #if $s5 != $s4, then branch to the label ‘loop’
j 0x80005010 #unconditional jump to the instruction at mem. addr. 0x80005010
jal fact #jump and link to the label ‘fact’ [stores return addr in $ra]
jr $t0 #jump to the instruction at mem. addr. pointed to by $t0
lbu $s0, 0($a0) #$s0 = {24 zeros, least significant byte of mem[0 + $a0]}
lui $v2, 0x1234 #$v2 = {0x1234, 16 zeros}
lw $t6, 0($s0) #$t6 = mem[0 + $s0]
nor $t0, $t1, $t2 #$t0 = $t1 NOR $t2 [ same as $t0 = ~($t1 | $t2) ]
or $s0, $s1, $s2 #$s0 = $s1 | $s2 [ the ‘|’ is the symbol for bitwise OR ]
ori $s4, $s4, 5 #$s4 = $s4 | 5
slt $t0, $s5, $s4 #if ($s5 < $s4) $t0 = 1, else $t0 = 0
slti $t0, $s0, 17 #if ($s0 < 17) $t0 = 1, else $t0 = 0
sltiu $s0, $t5, 4 #if ($t5 < 4) $s0 = 1, else $s0 = 0 [immediate interpreted as unsigned]
sltu $s0, $s1, $s2 #if ($s1 < $s2) $s0 = 1, else $s0 = 0 [$s1 & $s2 interpreted as unsigned]
sll $t3, $t2, 5 #t3 = $t2 << 5 [zeros are shifted into the least significant side]
srl $t0, $t1, 5 #t0 = $t1 >> 5 [zeros are shifted into the most significant side]
sra $t0, $t1, 5 #t0 = $t1 >>> 5 [the sign bit is shifted into the most significant side]
srlv $t3, $t2, $t4 #t3 = $t2 >> $t4 [number of places to shift stored in $t4]
sllv $t4, $t5, $t6 #t4 = $t5 << $t6 [number of places to shift stored in $t6]
srav $s0, $t0, $a0 #s0 = $t0 >>> $a0 [number of places to shift stored in $a0]
sb $s7, 0($t0) #mem[0 + $t0] = least significant byte of $s7
sw $s7, 0($t0) #the 4 bytes starting at mem[0 + $t0] = $s7
sub $t5, $t3, $t4 #$t5 = $t3 - $t4
blt $t0, $t1, exit #if $t0 < $t1, branch to the label ‘exit’
bgt $t0, $t1, exit #if $t0 > $t1, branch to the label ‘exit’
li $t0, 0x12345678 #$t0 = 0x12345678
Convert the following MIPS instructions to machine code:

1) addi $sp, $sp, -8

00100011101111011111111111111000

2) sw $t0, 4($a0) ($a0 = 0x5050)

10101100100010000000000000000100

3) slti $t2, $a1, 2

00101000101010100000000000000010

4) beq $s0, $t2, -1

00010010000010101111111111111111

5) j 0x81234FE0 (PC = 0x80000000) [NOTE THERE WAS AN ERROR IN THE QS]

00001000010010001101001111111000

6) nop

00000000000000000000000000000000

7) lw $s0, 0($t3) ($t3 = 0x80001000)

10001101011100000000000000000000

8) sub $t0, $t1, $t2

00000001001010100100000000100010
Convert the following machine code to MIPS instructions:

1) 0 0 1 1 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 1

ori $s0, $s1, 0x2FF

2) 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0

and $t1, $s1, $a1

3) 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

addi $v0, $s5, -1

4) 0 0 0 0 1 0 0 1 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 1 0 1 0 1 0 1 1

Note: Question should have specified value of Program Counter (PC). (Assume PC =
0x00001000)

j 0x050C86AC

5) 0x3C081234

00111100000010000001001000110100
lui $t0, 0x1234

6) 0x000A4143

00000000000010100100000101000011
sra $t0, $t2, 5

7) 0x00851026

00000000100001010001000000100110
xor $v0, $a0, $a1

8) 0x01AE6020

00000001101011100110000000100000
add $t4, $t5, $t6
Convert the following C code to MIPS assembly code:

1) (Assume base address of array A is stored in $a0)

int N = 64;
for(int i=0; i<N; i++)
{
A[i] = i;
}

li $t0, 64 #int N = 64
li $t1, 0 #int i = 0

for: beq $t1, $t0, exit #exit loop if (i == N)


sll $t2, $t1, 2 #$t2 = i * 4
add $t2, $a0, $t2 #$t2 = $a0 + (i*4)
sw $t1, 0($t2) #A[i] = i
addi $t1, $t1, 1 #i++
j for #jump to label ‘for’

exit: (other code)

2) (Assume $t0 = i, $t1 = j, $t2 = k)

if(i == j && i == k)
i++;
else
j--;

j = i + k;

bne $t0, $t1, else #goto ‘else’ if (i != j)


bne $t0, $t2, else #goto ‘else’ if (i != k)
addi $t0, $t0, 1 #i++
j sum #goto ‘sum’

else: addi $t2, $t2, -1 #j--

sum: add $t1, $t0, $t2 #j = i + k


3)(Assume $a0 = base address of array v, and $a1 = k)

void swap(int v[], int k)


{
int temp;
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
}

swap: sll $t0, $a1, 2 #$t0 = k * 4


add $t0, $a0, $t0 #$t0 = address of v[k]
addi $t2, $t0, 4 #$t2 = address of v[k+1]
lw $t1, 0($t0) #temp = v[k]
lw $t3, 0($t2) #$t3 = v[k+1]
sw $t3, 0($t0) #v[k] = v[k+1]
sw $t1, 0($t2) #v[k+1] = temp

You might also like