MIPS Function Continued

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

The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
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,
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
CDA 3100 Recitation Week 15. What does the function f1 do:.data A:.word 10,21,45,8,100,15,29,12,3,19 B:.word 2,5,33,5,20,1,53,52,5,5 C:.word 6,8,5,4,5,22,53,12,33,89.text.globl.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
5Z032 Processor Design SPIM, a MIPS simulator Henk Corporaal
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
SPIM and MIPS programming
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
MIPS Assembly Language Programming
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Recitation Material of Engineering an Assembler June 11, 2013.
The University of Adelaide, School of Computer Science
Assembly Language Working with the CPU.
.text fact: addiu $sp, $sp, -32 sw $ra, 20($sp) sw $fp, 16($sp) addiu $fp, $sp, 28 sw $a0, 0($fp) bgtz $a0, L2 li $v0, 1 b suffix L2: addi $a0, $a0,
CS3350B Computer Architecture Winter 2015 Lecture 4
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Intro to Computer Architecture
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Lecture 4: MIPS Instruction Set
Subroutines, parameters and the stack Bryan Duggan.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /22/2013 Lecture 12: Character Data Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Lecture 5: Procedure Calls
Computer Architecture & Operations I
MIPS Assembly Language Programming
MIPS Procedures.
MIPS Coding Continued.
MIPS instructions.
Procedures (Functions)
Procedures (Functions)
MIPS coding.
CSCI206 - Computer Organization & Programming
MIPS Procedures.
MIPS Functions.
MIPS Instructions.
MIPS coding.
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Functions.
MIPS Procedures.
Review.
MIPS Coding.
MIPS function continued
MIPS Functions.
MIPS function continued
MIPS coding.
MIPS Functions.
MIPS function continued
MIPS Functions.
Presentation transcript:

MIPS Function Continued

Character and String Operations 4/11/2017 Character and String Operations Characters are encoded as 0’s and 1’s using ASCII most commonly American Standard Code for Information Interchange Each character is represented using 8 bits (or a byte) MIPS provides instructions to move bytes Load byte (lb) loads a byte to the rightmost 8 bits of a register Store byte (sb) write the rightmost 8 bits of a register to memory week04-3.ppt 4/11/2017 CDA3100 week04-3.ppt

SPIM syscalls li $v0,1 # print an integer in $a0 li $a0,100 syscall li $v0,5 # read an integer into $v0 li $v0,4 # print an ASCIIZ string at $a0 la $a0,msg_hello li $v0,10 #exit

String Copy Procedure week04-3.ppt 4/11/2017 4/11/2017 CDA3100 week04-3.ppt

.asciiz "Hello! This is CDA3100!\n" msg_empty: .space 400 .text .data msg_hello: .asciiz "Hello! This is CDA3100!\n" msg_empty: .space 400 .text .globl main main: li $v0,4 la $a0,msg_hello syscall la $a0,msg_empty la $a0,msg_empty #dst la $a1,msg_hello #src jal strcpy li $v0,10 #exit strcpy: ori $t0, $a0, 0 ori $t1, $a1, 0 strcpyloop: lb $t3, 0($t1) sb $t3, 0($t0) beq $t3, $0, strcpydone addi $t0, $t0, 1 addi $t1, $t1, 1 j strcpyloop strcpydone: jr $ra

Stack Key things to keep in mind: Stack is a software concept – last in first out, that’s it. In MIPS, you implement the stack by yourself by keeping $sp always pointing to the top element on the stack Stack can be used in functions to save register values, and is the standard approach to save register values. But You can also use stack for other purposes This is not the only way to save register values.

msg: .asciiz "hello world" endl: .asciiz "\n" .text .globl main main: .data msg: .asciiz "hello world" endl: .asciiz "\n" .text .globl main main: addi $sp,$sp,-1 sb $0,($sp) la $t1, msg L0: lb $t0,($t1) beq $t0,$0, L1 sub $sp,$sp,1 sb $t0,($sp) add $t1,1 j L0 L1: la $t1,msg L2: lb $t0,($sp) add $sp,$sp,1 sb $t0,($t1) beq $t0, $0, L3 add $t1,1 j L2 L3: la $a0,msg li $v0,4 syscall la $a0,endl li $v0,10 #exit

Implementing a Recursive Function Suppose we want to implement this in MIPS: It is a recursive function – a function that calls itself. It will keep on calling itself, with different parameters, until a terminating condition is met.

The Recursive Function What happens if we call fact(4)? First time call fact, compare 4 with 1, no less than 1, call fact again – fact(3). Second time call fact, compare 3 with 1, no less than 1, call fact again – fact(2). Third time call fact, compare 2 with 1, no less than 1, call fact again – fact(1). Fourth time call fact, compare 1 with 1, no less than 1, call fact again – fact(0). Fifth time call fact, compare 1 with 1, less than 1, return 1. Return to the time when fact(0) was called (when calling fact(1)). Multiply 1 with 1, return 1. Return to the time when fact(1) was called (when calling fact(2)). Multiply 2 with 1, return 2. Return to the time when fact(2) was called (when calling fact(3)). Multiply 3 with 2, return 6. Return to the time when fact(3) was called (when calling fact(4)). Multiply 4 with 6, return 24.

The Recursive Function In MIPS, we say calling a function as going to the function. So we go to the function over and over again, until the terminating condition is met. Here, the function is called “fact,” so we will have a line of code inside the fact function: jal fact

The Recursive Function The parameter should be passed in $a0. In the C function, every time we call fact, we call with n-1. So, in the MIPS function, before we do “jal fact”, we should have “addi $a0, $a0,-1.” addi $a0, $a0, -1 jal fact

The Recursive Function After calling fact, we multiply the return result with n, so addi $a0, $a0, -1 jal fact mul $v0, $v0, $a0

The Recursive Function After multiplying, we return, so addi $a0, $a0, -1 jal fact mul $v0, $v0, $a0 jr $ra

The Recursive Function So, one if else branch is done. The other branch is slti $t0, $a0, 1 beq $t0, $0, L1 ori $v0, $0, 1 jr $ra where L1 is where we should go to if n >= 1.

The Recursive Function fact: slti $t0, $a0, 1 beq $t0, $zero, L1 ori $v0, $0, 1 jr $ra L1: addi $a0, $a0, -1 jal fact mul $v0, $v0, $a0 Any problems?

The Recursive Function fact: addi $sp, $sp, -4 sw $ra, 0($sp) slti $t0, $a0, 1 beq $t0, $zero, L1 ori $v0, $0, 1 lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra L1: addi $a0, $a0, -1 jal fact mul $v0, $v0, $a0 The problem is that the function will call itself, as we have expected, but it will not return correctly! You need to save $ra, because you made another function call inside the function. You should always do so. Is this enough?

The Recursive Function So now you can return to the main function, but the return result is 0, why? Because you did not return to the correct time. Time, here, means what was the parameter you called fact with. So, should also save $a0!

The Recursive Function 4/11/2017 The Recursive Function week04-3.ppt 4/11/2017 CDA3100 week04-3.ppt

The Stack During Recursion 4/11/2017 The Stack During Recursion week04-3.ppt 4/11/2017 CDA3100 week04-3.ppt

.data .text .globl main main: li $a0, 4 jal fact done: li $v0,10 syscall fact: addi $sp, $sp, -8 sw $ra, 4($sp) sw $a0, 0($sp) slti $t0, $a0, 1 beq $t0, $zero, L1 ori $v0, $0, 1 addi $sp, $sp, 8 jr $ra L1: addi $a0, $a0, -1 lw $ra, 4($sp) lw $a0, 0($sp) mul $v0, $v0, $a0

Two other MIPS pointers $fp: When you call a C function, the function may declare an array of size 100 like int A[100]. It is on the stack. You would want to access it, but the stack pointer may keep changing, so you need a fixed reference. $fp is the “frame pointer,” which should always point to the first word that is used by this function. $gp: the “global pointer.” A reference to access the static data.