Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIPS Assembly Language Programming

Similar presentations


Presentation on theme: "MIPS Assembly Language Programming"— Presentation transcript:

1 MIPS Assembly Language Programming
CDA 3101 Discussion Section 03 MIPS Assembly Language Programming

2 Outline MIPS simulator – PCSpim Problems from textbook 2.29, 2.30

3 PCSpim Installation From the textbook CD From the internet

4 Writing A Basic Program
Let’s start by writing a program that sums all the numbers between 1 and 10. int main() { int i = 10; int sum = 0; do sum = sum + i; i = i - 1; } while( i != 0 ); return sum; }

5 Writing A Basic Program
Summing numbers between 1 and 10. Something… like this: int main() { int i = 10; int sum = 0; do sum = sum + i; i = i - 1; } while( i != 0 ); return sum; }

6 Writing A Basic Program
How can we translate this into MIPS? int main() { int i = 10; int sum = 0; do sum = sum + i; i = i - 1; } while( i != 0 ); return sum; }

7 Writing A Basic Program
How can we translate this into MIPS? int main() { int i = 10; int sum = 0; do sum = sum + i; i = i - 1; } while( i != 0 ); return sum; } .text #Tells us this is the code section. .globl main #Tells compiler that this is a #public location (function) main: … #The start of the function

8 Writing A Basic Program
How can we translate this into MIPS? int main() { int i = 10; int sum = 0; do sum = sum + i; i = i - 1; } while( i != 0 ); return sum; } main: addi $t0, $0, 10 #int i = 10; or $v0, $0, $0 #int sum = 0; So we will be using $t0 as i, and $v0 as sum, since $v0 is a return register.

9 Writing A Basic Program
How can we translate this into MIPS? int main() { int i = 10; int sum = 0; do sum = sum + i; i = i - 1; } while( i != 0 ); return sum; } main: addi $t0, $0, 10 #int i = 10; or $v0, $0, $0 #int sum = 0; #do loop: … bnez $t0, loop #while(i != 0); To make a do … while loop, we will need a label to jump to. It’s really the old goto function.

10 Writing A Basic Program
How can we translate this into MIPS? int main() { int i = 10; int sum = 0; do sum = sum + i; i = i - 1; } while( i != 0 ); return sum; } main: addi $t0, $0, 10 #int i = 10; or $v0, $0, $0 #int sum = 0; #do loop: add $v0, $v0, $t0 #sum = sum + i; addi $t0, $t0, -1 #i = i – 1; bnez $t0, loop #while(i != 0); Now we can perform the real work.

11 Writing A Basic Program
How can we translate this into MIPS? int main() { int i = 10; int sum = 0; do sum = sum + i; i = i - 1; } while( i != 0 ); return sum; } main: addi $t0, $0, 10 #int i = 10; or $v0, $0, $0 #int sum = 0; #do loop: add $v0, $v0, $t0 #sum = sum + i; addi $t0, $t0, -1 #i = i – 1; bnez $t0, loop #while(i != 0); jr $ra #return sum; We use jr $ra to jump back to the function’s caller. Note that $v0 was already set to be the return value.

12 Writing A Basic Program
How can we translate this into MIPS? int main() { int i = 10; int sum = 0; do sum = sum + i; i = i - 1; } while( i != 0 ); return sum; } .text #Tells us this is the code section. .globl main #Tells compiler that this is a #public location (function) main: addi $t0, $0, 10 #int i = 10; or $v0, $0, $0 #int sum = 0; #do loop: add $v0, $v0, $t0 #sum = sum + i; addi $t0, $t0, -1 #i = i – 1; bnez $t0, loop #while(i != 0); jr $ra #return sum; And our program is complete! Let’s test it.

13 PC Spim

14 PC Spim Note the top window – it contains the state of all registers.

15 PC Spim The button on the top right runs the program to its end, after you click “OK” on the dialog box. Note that you won’t see the register changes until the program ends.

16 PC Spim Click this menu item to reinitialize PC Spim – it’s like rebooting your computer. It’s often necessary to click Reload to run your program again.

17 PC Spim Click this menu item to change settings for the emulator.

18 PC Spim Click this menu item to change settings for the emulator.
Sometimes it’s helpful to uncheck “General registers in hexadecimal” so that you can read values as regular numbers.

19 PC Spim Click the button that looks like a hand to set breakpoints. The program will stop running at positions you indicate, and wait for your authorization to continue upon reaching said point. You will also see the register values updated.

20 Understanding MIPS code
Problem 2.30 Add comments to the following MIPS code and describe in one sentence what it computes. The code processes 2 arrays and produces a value in $v0. Each array consists of 2500 words indexed 0 through 2499, the base addresses of the arrays are stored in $a0, $a1, and their sizes are stored in $a2, $a3. What will be returned in $v0?

21 Understanding MIPS code
Problem 2.30 sll $a2, $a2, 2 sll $a3, $a3, 2 add $v0, $zero, $zero add $t0, $zero, $zero outer: add $t4, $a0, $t0 lw $t4, 0($t4) add $t1, $zero, $zero inner: add $t3, $a1, $t1 lw $t3, 0($t3) bne $t3, $t4, skip addi $v0, $v0, 1 skip: addi $t1, $t1, 4 bne $t1, $a3, inner addi $t0, $t0, 4 bne $t0, $a2, outer #a2=a2*4, size in byte; #a3=a3*4, # $v0=0, counter; # $t0=0, index of $a0 (x) i; # $t4 = x[i]; start of outer loop; # $t1=0, index of $a1 (y) j; # $t3 = y[j], inner loop # if x[i]==y[j], counter increment; # else compare x[i] with y[j+1], until j reach the end of y # go back to start point of outer loop. Compare x[i+1] with y, and count the number

22 Understanding MIPS code
Problem 2.30 sll $a2, $a2, 2 sll $a3, $a3, 2 add $v0, $zero, $zero add $t0, $zero, $zero outer: add $t4, $a0, $t0 lw $t4, 0($t4) add $t1, $zero, $zero inner: add $t3, $a1, $t1 lw $t3, 0($t3) bne $t3, $t4, skip addi $v0, $v0, 1 skip: addi $t1, $t1, 4 bne $t1, $a3, inner addi $t0, $t0, 4 bne $t0, $a2, outer Assume $a0->x, $a1->y, it equals to a nested loop in C code: count = 0; for(i=0; i<2500; i++) { for(j=0; j<2500; j++) { if(x[i] == y[j]) count ++; }

23 Understanding MIPS code
Problem 2.29 Add comments to the following MIPS code and describe in one sentence what it computes. Assume $a0, $a1 are input, initial value a, b; $v0 is output. $a0: input a; $a1: input b; $v0: output; add $t0, $zero, $zero loop: beq $a1, $zero, finish add $t0, $t0, $a0 sub $a1, $a1, 1 j loop finish: addi $t0, $t0, 100 add $v0, $t0, $zero # t=0; # if(b==0) go to finish # t = t+a, increase t by a # b = b-1, # go to loop # t = t+100 # $v0 = t, output

24 Understanding MIPS code
Problem 2.29 Add comments to the following MIPS code and describe in one sentence what it computes. Assume $a0, $a1 are input, initial value a, b; $v0 is output. add $t0, $zero, $zero loop: beq $a1, $zero, finish add $t0, $t0, $a0 sub $a1, $a1, 1 j loop finish: addi $t0, $t0, 100 add $v0, $t0, $zero t=0; while(b!=0) { t = t + a; b = b – 1; } t = t + 100; v = t; So, v = a*b+100


Download ppt "MIPS Assembly Language Programming"

Similar presentations


Ads by Google