Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/2014 1.

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.
Lecture 5: MIPS Instruction Set
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.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
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
MIPS Function Continued
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.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
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
Lecture 8: MIPS Instruction Set
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Intro to Computer Architecture
1 Lecture 5: MIPS Examples Today’s topics:  the compilation process  full example – sort in C Reminder: 2 nd assignment will be posted later today.
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.
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
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.
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.
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.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
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.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
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.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Computer Architecture & Operations I
Lecture 6: Assembly Programs
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Chapter 2 Instructions: Language of the Computer part 2
10/4: Lecture Topics Overflow and underflow Logical operations
MIPS function continued
Lecture 6: Assembly Programs
Systems Architecture I
Computer Architecture
Presentation transcript:

Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/2014 1

Recap: Procedure Calling Steps required 1.Place parameters in registers 2.Transfer control to procedure 3.Acquire storage for procedure 4.Perform procedure’s operations 5.Place result in register for caller 6.Return to place of call 2

3 Recap The jal instruction is used to jump to the procedure and save the current PC (+4) into the return address register Arguments are passed in $a0-$a3; return values in $v0-$v1 Since the callee may over-write the caller’s registers, relevant values may have to be copied into memory Each procedure may also require memory space for local variables – a stack is used to organize the memory needs for each procedure

4 The Stack The register scratchpad for a procedure seems volatile – it seems to disappear every time we switch procedures – a procedure’s values are therefore backed up in memory on a stack Proc A’s values Proc B’s values Proc C’s values … High address Low address Stack grows this way Proc A call Proc B … call Proc C … return

5 Example 1 int leaf_example (int g, int h, int i, int j) { int f ; f = (g + h) – (i + j); return f; }

6 Example 1 int leaf_example (int g, int h, int i, int j) { int f ; f = (g + h) – (i + j); return f; } leaf_example: addi $sp, $sp, -12 sw $t1, 8($sp) sw $t0, 4($sp) sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) lw $t0, 4($sp) lw $t1, 8($sp) addi $sp, $sp, 12 jr $ra Notes: In this example, the procedure’s stack space was used for the caller’s variables, not the callee’s – the compiler decided that was better. The caller took care of saving its $ra and $a0-$a3.

Non-Leaf Procedures Procedures that call other procedures For nested call, caller needs to save on the stack: –Its return address –Any arguments and temporaries needed after the call Restore from the stack after the call 7

Non-Leaf Procedure Example C code: int fact (int n) { if (n < 1) return f; else return n * fact(n - 1); } –Argument n in $a0 –Result in $v0 8

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 9

Local Data on the Stack Local data allocated by callee –e.g., C automatic variables Procedure frame (activation record) –Used by some compilers to manage stack storage 10

Memory Layout Text: program code Static data: global variables –e.g., static variables in C, constant arrays and strings –$gp initialized to address allowing ±offsets into this segment Dynamic data: heap –E.g., malloc in C, new in Java Stack: automatic storage 11

12 Dealing with Characters Instructions are also provided to deal with byte-sized and half-word quantities: lb (load-byte), sb, lh, sh These data types are most useful when dealing with characters, pixel values, etc. C employs ASCII formats to represent characters – each character is represented with 8 bits and a string ends in the null character (corresponding to the 8-bit number 0)

13 Example Convert to assembly: void strcpy (char x[], char y[]) { int i; i=0; while ((x[i] = y[i]) != `\0’) i += 1; }

14 Example Convert to assembly: void strcpy (char x[], char y[]) { int i; i=0; while ((x[i] = y[i]) != `\0’) i += 1; } strcpy: addi $sp, $sp, -4 sw $s0, 0($sp) add $s0, $zero, $zero L1: add $t1, $s0, $a1 lb $t2, 0($t1) add $t3, $s0, $a0 sb $t2, 0($t3) beq $t2, $zero, L2 addi $s0, $s0, 1 j L1 L2: lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra

String Copy Example MIPS code: strcpy: addi $sp, $sp, -4 # adjust stack for 1 item sw $s0, 0($sp) # save $s0 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) # restore saved $s0 addi $sp, $sp, 4 # pop 1 item from stack jr $ra # and return 15

bit Constants Most constants are small –16-bit immediate is sufficient For the occasional 32-bit constant lui rt, constant –Copies 16-bit constant to left 16 bits of rt –Clears right 16 bits of rt to 0 lui $s0, ori $s0, $s0,

Branch Addressing Branch instructions specify –Opcode, two registers, target address Most branch targets are near branch –Forward or backward oprsrtconstant or address 6 bits5 bits 16 bits PC-relative addressing Target address = PC + offset × 4 PC already incremented by 4 by this time 17

Jump Addressing Jump ( j and jal ) targets could be anywhere in text segment –Encode full address in instruction opaddress 6 bits 26 bits 18

Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites the code Example beq $s0,$s1, L1 ↓ bne $s0,$s1, L2 j L1 L2:… 19

Addressing Mode Summary 20

21 Large Constants Immediate instructions can only specify 16-bit constants The lui instruction is used to store a 16-bit constant into the upper 16 bits of a register… thus, two immediate instructions are used to specify a 32-bit constant The destination PC-address in a conditional branch is specified as a 16-bit constant, relative to the current PC A jump (j) instruction can specify a 26-bit constant; if more bits are required, the jump-register (jr) instruction is used