Computer Organization CS224 Fall 2012 Lessons 9 and 10.

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

C OMPUTER O RGANIZATION AND D ESIGN The Hardware/Software Interface 5 th Edition Chapter 2 Instructions: Language of the Computer.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedure Calls Prof. Sirer CS 316 Cornell University.
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.
Chapter 2 Instructions: Language of the Computer CprE 381 Computer Organization and Assembly Level Programming, Fall 2013 Zhao Zhang Iowa State University.
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CSE331 W04.1Irwin&Li 2006 PSU CSE 331 Computer Organization and Design Fall 2006 Week 4 Section 1: Mary Jane Irwin (
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.
The University of Adelaide, School of Computer Science
CS3350B Computer Architecture Winter 2015 Lecture 4
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CS224 Fall 2011 Chapter 2b Computer Organization CS224 Fall 2011 Chapter 2 b With thanks to M.J. Irwin, D. Patterson, and J. Hennessy for some lecture.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Chapter 2 Instructions: Language of the Computer.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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,
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /22/2013 Lecture 12: Character Data Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
Lec 6Systems Architecture1 Systems Architecture Lecture 6: Branching and Procedures in MIPS Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Chapter 2 Instructions: Language of the Computer.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Computer Architecture & Operations I
Prof. Hsien-Hsin Sean Lee
Lecture 6: Assembly Programs
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Chapter 2 Instructions: Language of the Computer part 2
Program and memory layout
Lecture 6: Assembly Programs
Systems Architecture I
CSC3050 – Computer Architecture
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Presentation transcript:

Computer Organization CS224 Fall 2012 Lessons 9 and 10

 MIPS procedure call instruction: jalProcedureAddress#jump and link  Saves PC+4 in register $ra to have a link to the next instruction, for the procedure return; jumps to target addr.  Machine format (J format):  Procedure return copies return address to PC: jr$ra#return to callpoint+4;  Instruction format (R format): Instructions for Accessing Procedures 0x03 26 bit address x08 §2.8 Supporting Procedures in Computer Hardware

Six Steps in Execution of a Procedure 1. Main routine (caller) places parameters in a place where the procedure (callee) can access them $a0 - $a3 : four argument registers 2. Caller transfers control to the callee ( jal Dest) 3. Callee acquires the storage resources needed 4. Callee performs the desired task 5. Callee places the result value in a place where the caller can access it $v0 - $v1 : two value registers for result values 6. Callee returns control to the caller ( jr $ra) $ra : one return address register to return to the point of origin

Register Usage  $a0 – $a3: arguments (reg’s 4 – 7)  $v0, $v1: result values (reg’s 2 and 3)  $t0 – $t9: temporaries (reg’s 8-15, 24-25) l Can be overwritten by callee  $s0 – $s7: saved (reg’s 16-23) l Must be saved/restored by callee !  $gp: global pointer for static data (reg 28)  $sp: stack pointer (reg 29)  $fp: frame pointer (reg 30)  $ra: return address (reg 31)  $gp, $sp, $fp, $ra must be saved/restored by callee !

MIPS Register Convention NameRegister Number UsagePreserve on call? $zero0constant 0 (hardware)n.a. $at1reserved for assemblern.a. $v0 - $v12-3returned valuesno $a0 - $a34-7argumentsno $t0 - $t78-15temporariesno $s0 - $s716-23saved valuesyes $t8 - $t924-25temporariesno $k0 - $k126-27reserved for op systemn.a. $gp28global pointeryes $sp29stack pointeryes $fp30frame pointeryes $ra31return addr (hardware)yes

Stack Usage  Both caller and callee use a stack – a last-in-first-out queue low addr high addr $sp  One of the general registers, $sp ( $29 ), is used to address the stack (which “grows” down, from high address to low address) l add data onto the stack – push $sp = $sp – 4 put data on stack at new $sp l remove data from the stack – pop get data from stack at $sp $sp = $sp + 4 top of stack

Leaf Procedure Example  C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } l Arguments g, …, j in $a0, …, $a3 l f in $s0 (hence, need to save $s0 on stack) l Return result in $v0

Leaf Procedure Example  MIPS code: leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Note: it can be done in 4 lines (2 add, sub, jr) if $s0 is not used Save $s0 on stack Procedure body Restore $s0 Result to$v0 for return Return

Stacking of Subroutine Environments  Procedures are often nested, there are multiple calls and returns  C is a “leaf” procedure, but B is a nested (non-leaf) procedure  The stack grows downward at each new call, shrinks upward at each return A: A CALL B B: A B CALL C A B C C: RET A B A

Non-Leaf Procedures  Procedures that call other procedures  For nested call, caller needs to save on the stack: l Its return address l Any arguments and temporaries it still needs after the call (which are not in saved registers)  Restore from the stack after the call  As with leaf procedures, callee also needs to save on the stack any “saved” registers, & restore them at end

Non-Leaf Procedure Example  C code: int fact(int n) { if (n < 1) return (1); else return n * fact(n - 1); } l Argument n in $a0 l Return result in $v0

Non-Leaf Procedure Example  MIPS code: fact:addi $sp, $sp, -8 # adjust stack for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 addi $v0, $zero, 1 # if so, result is 1 addi $sp, $sp, 8 # pop 2 items from stack jr $ra # and return L1: addi $a0, $a0, -1 # else decrement n jal fact # recursive call lw $a0, 0($sp) # restore original n lw $ra, 4($sp) # and return address addi $sp, $sp, 8 # pop 2 items from stack mul $v0, $a0, $v0 # multiply to get result jr $ra # and return

Local Data on the Stack  Local data allocated by callee l e.g., C automatic variables  Procedure frame (aka activation record) l Used by some compilers to manage stack storage  The frame pointer ( $fp ) points to the first word of the frame of a procedure – providing a stable “base” register for the procedure

Using Registers to Implement Procedures  Caller l Save caller-saved registers $a0-$a3, $t0-$t9 (if their values need preserving) l Load arguments in $a0-$a3 (rest on stack above $fp) l Execute jal instruction  Callee Setup l Step 1: Allocate memory in frame ($sp = $sp - frame) l Step 2: Save callee-saved registers $s0-$s7, $fp, $ra (if their values will be changed) l Step 3: Create frame ($fp = $sp + frame size - 4)  Callee Return l Place return value in $v0 and $v1 l Restore any callee-saved registers ($fp, $ra,$s0-$s7…) l Pop stack ($sp = $sp + frame size) l Return using jr $ra

Calling Convention: Steps FP SP ra old FP $s0-$s7 FP SP First four arguments passed in registers Before call: Callee setup; step 1 FP SP Callee setup; step 2 ra old FP $s0-$s7 FP SP Callee setup; step 3 Adjust SP Save registers as needed Adjust FP

Memory Layout  Text: program code  Static data: global variables l e.g., static variables in C, constant arrays and strings l $gp is initialized to address , allowing +/- offsets into this segment  Dynamic data: heap l For structures that grow and shrink, e.g. linked lists, trees, etc l Allocate using malloc in C, (new in Java); free it with free in C (automatic garbage collection in Java)  Stack: storage of automatic variables (local to a procedure)

Character Data  Byte-encoded character sets l ASCII: 128 characters (= 7-bit) -95 graphic (i.e printable), 33 control l Latin-1: 256 characters (= 8-bit) -ASCII, +96 more graphic characters  Unicode: universal character set l Used in Java, C++ wide characters, … l Most of the world’s alphabets, plus symbols l UTF-16 is the default (16-bit encoding for each character) l UTF-32 is 32-bits per character l UTF-8 is variable length: 8-bits to handle ASCII, plus bits for other characters §2.9 Communicating with People

Byte/Halfword Operations  Could use bitwise operations—so why not? l String processing is a common case  MIPS byte/halfword load/store operations --lb rt, offset(rs) lh rt, offset(rs) l Sign extend to 32 bits in rt --lbu rt, offset(rs) lhu rt, offset(rs) l Zero extend to 32 bits in rt --sb rt, offset(rs) sh rt, offset(rs) l Store just rightmost byte/halfword

String Copy Example  C code (naive): l Null-terminated ASCII string void strcpy (char x[], char y[]) { int i; i = 0; while ((x[i]=y[i])!='\0') # copy x  y until null i += 1; } l Addresses of x, y in $a0, $a1 l i in $s0

String Copy Example  MIPS code: strcpy: addi $sp, $sp, -4 # push $s0 onto stack sw $s0, 0($sp) # add $s0, $zero, $zero # i = 0 L1: add $t1, $s0, $a1 # addr of y[i] in $t1 lbu $t2, 0($t1) # $t2 = y[i] add $t3, $s0, $a0 # addr of x[i] in $t3 sb $t2, 0($t3) # x[i] = y[i] beq $t2, $zero, L2 # exit loop if y[i] == 0 addi $s0, $s0, 1 # i = i + 1 j L1 # next iteration of loop L2: lw $s0, 0($sp) # pop $s0 from stack addi $sp, $sp, 4 # jr $ra # and return [Bottom testing & pointer addressing saves 2 lines from the loop. Not using s0 saves 4 other lines.]

 We'd like to be able to load a 32-bit constant into a register, for this we must use two instructions  a new "load upper immediate" instruction lui $t0,  Then must get the lower order bits right, use ori $t0, $t0, bit Constants §2.10 MIPS Addressing for 32-Bit Immediates and Addresses

Branch Addressing  Branch instructions specify l Opcode, two registers, target address l beq $t0, $s3, LoopEnd  Most branch targets are near branch l Forward or backward (max is ±2 15 ) oprsrtconstant or address 6 bits5 bits 16 bits  PC-relative addressing l Target address = PC + offset × 4 l PC already incremented by 4 by this time

 MIPS also has an unconditional branch instruction or jump instruction: j label#go to label Other Control Flow Instructions  Instruction Format (J Format): 0x02 26-bit address PC from the low order 26 bits of the jump instruction

Jump Addressing  Jump ( j and jal ) targets could be anywhere in text segment l Encode full address in instruction opaddress 6 bits 26 bits  Pseudo-Direct jump addressing l Target address = PC 31…28 : (address × 4)

Target Addressing Example  Loop code from earlier example l Assume Loop at location Loop: sll $t1, $s3, add $t1, $t1, $s lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, j Loop Exit: … 80024

Branching Far Away  What if the branch destination is further away than can be captured in 16 bits?  The assembler comes to the rescue – it inserts an unconditional jump to the branch target and inverts the condition beq$s0, $s1, L1_far becomes bne$s0, $s1, L2 jL1_far L2:

Addressing Mode Summary