MIPS Procedures.

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

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
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,
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10/6: Lecture Topics Procedure call Calling conventions The stack
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
MIPS Function Continued
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
Intro to Computer Architecture
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
Procedures 2a MIPS code examples Making Use of a Stack.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
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.
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
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Architecture & Operations I
Computer Science 210 Computer Organization
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Procedures 101: There and Back Again
CSCI206 - Computer Organization & Programming
MIPS Procedures.
MIPS Coding Continued.
MIPS instructions.
Procedures (Functions)
Functions and Procedures
MIPS coding.
CSCI206 - Computer Organization & Programming
Solutions Chapter 2.
MIPS Instructions.
MIPS coding.
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Functions.
Lecture 5: Procedure Calls
MIPS coding.
MIPS functions.
MIPS Procedures.
Review.
MIPS Coding.
MIPS function continued
MIPS Functions.
MIPS Coding.
Lecture 6: Assembly Programs
MIPS Coding Continued.
MIPS coding.
Where is all the knowledge we lost with information? T. S. Eliot
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
MIPS Functions.
MIPS function continued
MIPS Functions.
Presentation transcript:

MIPS Procedures

.data A: .word 0,1,2,3,4,5,6,7,8,9, .text .globl main main: la $s0, A li $s1, 10 li $s2, 0 # the loop counter li $s3, 0 # the result loop: sll $t0, $s2, 2 add $t0, $t0, $s0 lw $t0, 0($t0) andi $t1, $s2, 1 bne $t1, $zero, loop_L1 add $s3, $s3, $t0 j loop_L2 loop_L1: sub $s3, $s3, $t0 loop_L2: addi $s2, $s2, 1 beq $s2, $s1, done j loop done: li $v0,10 syscall

Procedures and Functions 11/17/2018 Procedures and Functions We programmers use procedures and functions to structure and organize programs To make them easier to understand To allow code to be reused 11/17/2018 week04-3.ppt CDA3100 week04-3.ppt

Functions A function carries out a well-defined functionality, that can be called and produce the result A function is a consecutive piece of code stored in the memory To invoke (or call) a function, we must go to that piece of code. Then it does certain things, and get the result we need. What do we know about going to a piece of code?

Functions So, we can call a function j Function. And, the function code will do something we need, and store the result somewhere that is well-known for the caller to pick up. Problem: how to come back to the caller?

Two Interesting Instructions and One Interesting Register jal: jump and link jal L1: does TWO things Goto L1. (the next instruction to be executed is at address L1) Save the address of the next instruction in $ra. $ra is the interesting register that stores the return address jr $ra Does ONE thing. Goto the instruction whose address is the value stored in $ra. This is ALL we need to support function calls in MIPS!

A simple function

.data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 .text .globl main main: la $s7, A li $s0, 0 #i li $s1, 0 #res li $s6, 9 loop: sll $t0, $s0, 2 add $t0, $t0, $s7 lw $a0, 0($t0) lw $a1, 4($t0) jal addfun add $s1, $s1, $v0 addi $s0, $s0, 2 bgt $s0, $s6, done j loop done: li $v0,10 syscall addfun: add $v0, $a0, $a1 jr $ra

Key things to keep in mind A function is just a segment of code stored sequentially in the memory. To call a function is just to go there. The name of a function in MIPS is JUST A LABEL or JUST AN ADDRESS. We cannot simply use “j addfun” to go to addfun, because we do not know where come back. Therefore, we need to store the address of the instruction that should be executed after going to the function somewhere, and in MIPS, it is $ra. At the end of a function, we write “jr $ra”, to go back.

MIPS Calling Conventions 11/17/2018 MIPS Calling Conventions MIPS assembly follows the following convention in using registers $a0 - $a3: four argument registers in which to pass parameters $v0 - $v1: two value registers in which to return values $ra: one return address register to return to the point of origin 11/17/2018 week04-3.ppt CDA3100 week04-3.ppt

MIPS Conventions Quite often, our function needs to use some registers to do dome calculation. So we will modify the values of them. We can use $t0-$t9 freely inside a function, because the caller does not expect the values inside $t0-$t9 to stay the same after the function call. But, the caller do expect the values in $s0 to $s7 to be the same after a function call.

MIPS Conventions So, just try to avoid using $s0 and $s7 inside a function whenever possible. But what if do need it? Such occasions will arise…

Stack So, if we do have to use $s0 - $s7, we MUST save it somewhere before entering the main part of the function, and restore it before you return (before we execute “jr $ra”). In MIPS, we save them in the stack. Stack is a part in the memory allocated for functions. It starts at 0x7ffffffc and grows down as we add more stuffs to it. Stack is “first in last out.”

$sp The top address of the stack, the address of the first word that is storing value, is (should be) always be stored in $sp. So, adding a word into the stack (pushing a word onto the stack) is a two-step thing, because you have to maintain the correctness of $sp: addi $sp, $sp, -4 sw $s0, 0($sp)

Suppose we want to

Stack and $sp Suppose we want to store a/2 in $s0. How do we get a/2? At the beginning, we do addi $sp, $sp, -4 sw $s0, 0($sp) At the end, we do lw $s0, 0($sp) addi $sp, $sp, 4

.data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 .text .globl main main: la $s7, A li $s0, 0 #i li $s1, 0 #res li $s6, 9 loop: sll $t0, $s0, 2 add $t0, $t0, $s7 lw $a0, 0($t0) lw $a1, 4($t0) jal weirdfun add $s1, $s1, $v0 addi $s0, $s0, 2 bgt $s0, $s6, done j loop done: li $v0,10 syscall weirdfun: addi $sp, $sp, -4 sw $s0, 0($sp) srl $s0, $a0, 1 add $t0, $a0, $a0 add $t0, $t0, $a1 sub $t0, $t0, $s0 ori $v0, $t0, 0 lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra

Function calls inside a function What if we need to call another function inside a function? Will this work? twofun: addi $sp, $sp, -4 sw $s0, 0($sp) jal addfun srl $s0, $a0, 1 add $v0, $v0, $s0, lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra

Function calls inside a function The problem is that the value of $ra is changed whenever you use jal somelabel. How to deal with it? twofun: addi $sp, $sp, -4 sw $s0, 0($sp) jal addfun srl $s0, $a0, 1 add $v0, $v0, $s0, lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra

The working versions twofun2: addi $sp, $sp, -8 sw $s0, 4($sp) twofun1: addi $sp, $sp, -4 sw $s0, 0($sp) sw $ra, 0($sp) jal addfun srl $s0, $a0, 1 add $v0, $v0, $s0, lw $ra, 0($sp) addi $sp, $sp, 4 lw $s0, 0($sp) jr $ra twofun2: addi $sp, $sp, -8 sw $s0, 4($sp) sw $ra, 0($sp) jal addfun srl $s0, $a0, 1 add $v0, $v0, $s0, lw $ra, 0($sp) lw $s0, 4($sp) addi $sp, $sp, 8 jr $ra

Saving registers In case of nested function calls, before calling a function, to be safe, the caller should save $t0-$t9 save $a0-$a3 If such registers are needed later. and save $ra Because $ra is going to be needed later and it will be changed The callee should save $s0-s7