MIPS Assembly Language Programming

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Lecture 5: MIPS Instruction Set
MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
The University of Adelaide, School of Computer Science
CS 136 Lab 2 MIPS ISA SPIM. Prob 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)
CDA 3100 Recitation Week 8. Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A li $a1, 17 li $a2, 10 jal funct li $v0,
Lecture 9: MIPS Instruction Set
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Solution 2nd Exam.
1 CDA 3101 Discussion Section 03.  Problem The following problems deal with translating from C to MIPS. Assume that the variables f, g, h, i and.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Computer Architecture CSCE 350
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Assembly Language Working with the CPU.
The University of Adelaide, School of Computer Science
Lecture 8: MIPS Instruction Set
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
MIPS Instruction Set Advantages
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
1 MIPS Assembly Language Programming CDA 3101 Discussion Section 03.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CPEG323 Homework Review I Long Chen October, 17 th, 2005.
MIPS Assembly Language Programming
Switch Statement Pre-requisites: 1D array addressing Updated 7/11/2013.
Lecture 5: Procedure Calls
MIPS Instruction Set Advantages
CS2100 Computer Organisation
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
Computer Architecture & Operations I
MIPS instructions.
Decision Making.
Procedures (Functions)
The University of Adelaide, School of Computer Science
MIPS coding.
MIPS Procedures.
MIPS Instruction Encoding
MIPS coding.
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Instruction Encoding
Instruction encoding The ISA defines Format = Encoding
Lecture 5: Procedure Calls
MIPS coding.
MIPS Coding.
MIPS Functions.
3.
MIPS Coding.
Review.
MIPS Coding Continued.
MIPS coding.
MIPS assembly.
9/27: Lecture Topics Memory Data transfer instructions
MIPS Functions.
MIPS instructions.
Conditional Branching (beq)
Presentation transcript:

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

Outline MIPS simulator – PCSpim Problems from textbook 2.29, 2.30

PCSpim Installation From the textbook CD From the internet http://www.cs.wisc.edu/~larus/spim.html

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; }

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; }

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; }

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

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.

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.

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.

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.

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.

PC Spim

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

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.

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.

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

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.

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.

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?

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

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 ++; }

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

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