MIPS Functions.

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.
Branches Two branch instructions:
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.
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 Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker.
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.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
MIPS Function Continued
Ch. 8 Functions.
The University of Adelaide, School of Computer Science
Lecture 8: MIPS Instruction Set
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
Procedures I Fall 2005 C functions main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */ int mult (int mcand,
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 */
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Procedures 2a MIPS code examples Making Use of a Stack.
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.
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.
Subroutines, parameters and the stack Bryan Duggan.
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.
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.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
MIPS Coding. Exercise – the bubble sort 7/9/2016week04-3.ppt2.
Computer Architecture & Operations I
MIPS Assembly Language Programming
Switch Statement Pre-requisites: 1D array addressing Updated 7/11/2013.
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Functions and the Stack
Procedures 101: There and Back Again
MIPS Procedures.
MIPS Coding Continued.
MIPS instructions.
MIPS coding.
MIPS Procedures.
MIPS Functions.
MIPS Instructions.
MIPS coding.
MIPS Functions.
MIPS Functions.
MIPS coding.
MIPS functions.
MIPS Procedures.
Review.
MIPS Coding.
MIPS function continued
MIPS Functions.
MIPS Coding.
Review.
MIPS Coding Continued.
MIPS coding.
Where is all the knowledge we lost with information? T. S. Eliot
Program Assembly.
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 R3000 Subroutine Calls and Stack
MIPS instructions.
Conditional Branching (beq)
Presentation transcript:

MIPS Functions

Procedures and Functions 9/16/2019 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 9/16/2019 week04-3.ppt CDA3100 week04-3.ppt

Function A function carries out a well-defined functionality, that can be called and produce the result to be used by the caller.

Functions 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 by j Function And, the function code will do something we need. 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!

Functions The procedure a a a a a a a a . a a a a a a add $t0, $t1, $t0 a a sub $t2, $t1, $t0 a a jal Function a a sll $t0, $t0, 2 . Function: a a add $t0, $t1, $t0 a a sub $t2, $t1, $t0 a a jr $ra

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 blt $s0, $s6, 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.