Lec 6Systems Architecture1 Systems Architecture Lecture 6: Branching and Procedures in MIPS Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.

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
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
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.
The University of Adelaide, School of Computer Science
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
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.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 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
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
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/
CHAPTER 2 ISA Instructions (logical + procedure call)
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.
Lecture 4. Miscellaneous Addressing Mode, Memory Map, Pointer, and ASCII Prof. Taeweon Suh Computer Science Education Korea University ECM534 Advanced.
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.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
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.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
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.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
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.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 14, 15 Addressing Mode.
Chapter 2 Instructions: Language of the Computer.
Arithmetic Operations Add and subtract, three operands Two sources and one destination add a, b, c # a gets b + c All arithmetic operations have this form.
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Computer Architecture & Operations I
Prof. Hsien-Hsin Sean Lee
CS2100 Computer Organisation
The University of Adelaide, School of Computer Science
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Procedures (Functions)
The University of Adelaide, School of Computer Science
What's wrong with this procedure?
Instructions - Type and Format
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Logical and Decision Operations
Chapter 2 Instructions: Language of the Computer part 2
The University of Adelaide, School of Computer Science
Lecture 6: Assembly Programs
Systems Architecture I
Computer Architecture
Presentation transcript:

Lec 6Systems Architecture1 Systems Architecture Lecture 6: Branching and Procedures in MIPS Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some or all figures from Computer Organization and Design: The Hardware/Software Approach, Third Edition, by David Patterson and John Hennessy, are copyrighted material (COPYRIGHT 2004 MORGAN KAUFMANN PUBLISHERS, INC. ALL RIGHTS RESERVED).

Lec 6Systems Architecture2 Introduction Objective: To illustrate how programming constructs such as conditionals, loops and procedures can be translated into MIPS instructions.

8 March 2016Chapter 2 — Instructions: Language of the Computer 3 Conditional Operations Branch to a labeled instruction if a condition is true –Otherwise, continue sequentially beq rs, rt, L1 –if (rs == rt) branch to instruction labeled L1; bne rs, rt, L1 –if (rs != rt) branch to instruction labeled L1; j L1 –unconditional jump to instruction labeled L1 §2.7 Instructions for Making Decisions

Lec 6Systems Architecture4 Decision making instructions –Alter the control flow – change the "next" instruction to be executed MIPS conditional branch instructions: bne $t0, $t1, Label beq $t0, $t1, Label Example: if (i==j) h = i + j; bne $s0, $s1, Label add $s3, $s0, $s1 Label:.... Control

Lec 6Systems Architecture5 MIPS unconditional branch instructions: j label Example: if (i!=j) beq $s4, $s5, Equal h=i+j;add $s3, $s4, $s5 else j GoOn h=i-j; Equal: sub $s3, $s4, $s5 GoOn:... If-Then Structure

8 March 2016Chapter 2 — Instructions: Language of the Computer 6 Compiling If Statements C code: if (i==j) f = g+h; else f = g-h; –f, g, … in $s0, $s1, … Compiled MIPS code: bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: … Assembler calculates addresses

8 March 2016Chapter 2 — Instructions: Language of the Computer 7 Compiling Loop Statements C code: while (save[i] == k) i += 1; –i in $s3, k in $s5, address of save in $s6 Compiled MIPS code: Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: …

8 March 2016Chapter 2 — Instructions: Language of the Computer 8 Branch Instruction Design Why not blt, bge, etc?

8 March 2016Chapter 2 — Instructions: Language of the Computer 9 Branch Instruction Design Why not blt, bge, etc? Hardware for <, ≥, … slower than =, ≠ –Combining with branch involves more work per instruction, requiring a slower clock –All instructions penalized! beq and bne are the common case This is a good design compromise

Lec 6Systems Architecture10 What about Branch-if-less-than? New instruction: if $s1 < $s2 then $t0 = 1 slt $t0, $s1, $s2 else $t0 = 0 MIPS does not include blt, bgt, ble, bge, bgz, etc. instructions because it is considered too complicated. Assembler pseudoinstruction: " blt $s1, $s2, Label " Note that the assembler needs a register to do this, — there are important conventions for registers use Less Than Test

8 March 2016Chapter 2 — Instructions: Language of the Computer 11 More Conditional Operations Set result to 1 if a condition is true –Otherwise, set to 0 slt rd, rs, rt –if (rs < rt) rd = 1; else rd = 0; slti rt, rs, constant –if (rs < constant) rt = 1; else rt = 0; Use in combination with beq, bne slt $t0, $s1, $s2 # if ($s1 < $s2) bne $t0, $zero, L # branch to L

Lec 6Systems Architecture12 Quick review InstructionMeaning add $s1,$s2,$s3$s1 = $s2 + $s3 sub $s1,$s2,$s3$s1 = $s2 – $s3 lw $s1,100($s2)$s1 = Memory[$s2+100] sw $s1,100($s2)Memory[$s2+100] = $s1 bne $s4,$s5,Label Jump to Label if $s4≠$s5 beq $s4,$s5,Label Jump to Label if $s4=$s5 j LabelNext instr. is at Label slt $t0, $s1, $s2 Set $t0 = 1 if $s1 < $s2 else set $t0 = 0

Lec 6Systems Architecture13 Quick review Formats op rs rt rdshamtfunct op rs rt 16 bit address op 26 bit address RIJRIJ

Lec 6Systems Architecture14 Loops Compiling a while loop (Assume i and k correspond to $s3, and $s5 and that the base address of save is in $s6.) while (save[i] == k) i += 1; Loop: sll $t1, $s3, 2 # $t1 = 4 * i add $t1, $t1, $s6 # address of save[i] lw $t0, 0($t1) # get save[i] bne $t0, $s5, Exit # goto Exit if save[i]  k add $s3, $s3, 1 # i = i + 1 j Loop Exit:

Lec 6Systems Architecture15 Switch Statement Use a “jump table” switch (k) { case 0: f = i + j; break; case 1: f = g + h; break; case 2: f = g - h; break; case 3: f = i - j; break; } Variables f - k in $s0 - $s5, $t4 contains address of jump table - JT slt $t3, $s5, $zero # k < 0? bne $t3,$zero, Exit slti $t3, $s5, 4 # k < 4? beq $t3, $zero, Exit add $t1, $s5, $s5 add $t1, $t1, $t1 add $t1, $t1, $t4 # addr JT[k] lw $t0, 0($t1) # JT[k] jr $t0 L0: add $s0, $s3, $s4 j Exit L1: add $s0, $s1, $s2 j Exit...

Lec 6Systems Architecture16 Procedures In the execution of a procedure, the program must follow these steps: –Place parameters in a place where the procedure can access them –Transfer control to the procedure –Acquire the storage resources needed for the procedure –Perform the desired task –Place the result where the calling program can access it –Return control to the point of origin

Lec 6Systems Architecture17 Registers for Procedure Calling and the jal Instruction $a0 - $a3: four argument registers used 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 jal ProcedureAddress: instruction to transfer control to a procedure and store the return address in $ra ($ra is set to PC + 4, address of the next instruction after procedure call) jr $ra - used to transfer control back to the calling program

Lec 6Systems Architecture18 Saving Registers using a Stack Additional registers used by the called procedure must be saved prior to use, or the values used by the calling procedure will be corrupted. The old values can be saved on a stack (call stack). After the called procedure completes, the old values can be popped off the stack and restored. $sp: stack pointer register contains the address of the top of the stack. By convention, address on the stack grows from higher addresses to lower address, which implies that a push subtracts from $sp and a pop adds to $sp.

Lec 6Systems Architecture19 Policy of Use Conventions Register 1 ($at) reserved for assembler, for the operating system.

Lec 6Systems Architecture20 Register Conventions The 8 “saved” registers $s0 - $s7 must be preserved on a procedure call, i.e. the called procedure must save these before using them. The 10 “temporary” registers $t0 - $t9 are not preserved by the called procedure. The calling procedure can not assume they will not change after a procedure call and, hence, must save them prior to the call if the values are needed after the call. Saved registers should be used for long lived variables, while temporary registers should be used for short lived variables

Lec 6Systems Architecture21 Nested Procedures and Automatic Variables A new call frame or activation record must be created for each nested procedure call. Argument registers and the return address register must be saved in addition to saved registers since new values will be put in them for the nested procedure call. Automatic variables (i.e. variables that are local to a procedure and are discarded when the procedure completes) are also allocated on the call stack. They are popped when the call completes.

Lec 6Systems Architecture22 Procedure Activation Records (Frames) and the Call Stack An activation record (frame) is a segment on the stack containing a procedure’s saved registers and local variables. Each time a procedure is called a frame ($fp: frame pointer register points to the current frame) is placed on the stack.

Lec 6Systems Architecture23 Leaf Procedure /* Example from page 134 */ int leaf_example (int g, int h, int I, int j) { int f; f = (g+h) - (i+j); return f; }

Lec 6Systems Architecture24 Leaf Procedure func:sub $sp,$sp,4 # push stack and save registers sw $s0,0($sp) add $t0,$a0,$a1 # g + h add $t1,$a2,$a3 # i + j sub $s0,$t0,$t1 # (g+h) - (i+j) add $v0,$s0,$zero # return f = (g+h)-(i+j) lw $s0,0($sp) # restore registers and pop stack add $sp,$sp,4 jr $ra # return to calling program

8 March 2016Chapter 2 — Instructions: Language of the Computer 25 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

8 March 2016Chapter 2 — Instructions: Language of the Computer 26 Character Data Byte-encoded character sets –ASCII: 128 characters 95 graphic, 33 control –Latin-1: 256 characters ASCII, +96 more graphic characters Unicode: 32-bit character set –Used in Java, C++ wide characters, … –Most of the world’s alphabets, plus symbols –UTF-8, UTF-16: variable-length encodings §2.9 Communicating with People

8 March 2016Chapter 2 — Instructions: Language of the Computer 27 Byte/Halfword Operations Could use bitwise operations MIPS byte/halfword load/store –String processing is a common case lb rt, offset(rs) lh rt, offset(rs) –Sign extend to 32 bits in rt lbu rt, offset(rs) lhu rt, offset(rs) –Zero extend to 32 bits in rt sb rt, offset(rs) sh rt, offset(rs) –Store just rightmost byte/halfword

8 March 2016Chapter 2 — Instructions: Language of the Computer 28 String Copy Example C code (naïve): –Null-terminated string void strcpy (char x[], char y[]) { int i; i = 0; while ((x[i]=y[i])!='\0') i += 1; } –Addresses of x, y in $a0, $a1 –i in $s0

8 March 2016Chapter 2 — Instructions: Language of the Computer 29 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

Lec 6Systems Architecture30 Recursive Procedure /* Factorial example from pp */ int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)); }

Lec 6Systems Architecture31 fact: sub $sp,$sp,8 # push stack sw $ra,4($sp) # save return address sw $a0,0($sp) # save n slt $t0,$a0,1 # test n < 1 beq $t0,$zero,L1 # branch if n >= 1 add $v0,$zero,1 # return 1 add $sp,$sp,8 # pop stack jr $ra # return to calling procedure L1: sub $a0,$a0,1 # set parameter to n-1 jal fact # call fact(n-1) lw $a0,0($sp) # restore previous value of n lw $ra,4($sp) # restore previous return address mul $v0,$a0,$v0 # return n * fact(n-1) add $sp,$sp,8 # pop stack jr $ra # return to calling procedure

8 March 2016Chapter 2 — Instructions: Language of the Computer 32 Arrays vs. Pointers Array indexing involves – Multiplying index by element size – Adding to array base address Pointers correspond directly to memory addresses – Can avoid indexing complexity

8 March 2016Chapter 2 — Instructions: Language of the Computer 33 Example: Clearing and Array clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; } move $t0,$zero # i = 0 loop1: sll $t1,$t0,2 # $t1 = i * 4 add $t2,$a0,$t1 # $t2 = # &array[i] sw $zero, 0($t2) # array[i] = 0 addi $t0,$t0,1 # i = i + 1 slt $t3,$t0,$a1 # $t3 = # (i < size) bne $t3,$zero,loop1 # if (…) # goto loop1 move $t0,$a0 # p = & array[0] sll $t1,$a1,2 # $t1 = size * 4 add $t2,$a0,$t1 # $t2 = # &array[size] loop2: sw $zero,0($t0) # Memory[p] = 0 addi $t0,$t0,4 # p = p + 4 slt $t3,$t0,$t2 # $t3 = #(p<&array[size]) bne $t3,$zero,loop2 # if (…) # goto loop2

8 March 2016Chapter 2 — Instructions: Language of the Computer 34 Comparison of Array vs. Ptr Multiply “strength reduced” to shift Array version requires shift to be inside loop – Part of index calculation for incremented i – c.f. incrementing pointer Compiler can achieve same effect as manual use of pointers – Induction variable elimination – Better to make program clearer and safer