Prof. Hsien-Hsin Sean Lee

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

Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
The University of Adelaide, School of Computer Science
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.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
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.
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.
Procedure call frame: Hold values passed to a procedure as arguments
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
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.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
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 */
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
6.S078 - Computer Architecture: A Constructive Approach Introduction to SMIPS Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts.
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.
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 12 Procedure Calling.
Instructor: Prof. Hany H Ammar, LCSEE, WVU
MIPS Programming.
COSC3330 Computer Architecture
CSCI206 - Computer Organization & Programming
Lecture 5: Procedure Calls
Lecture 6: Assembly Programs
Computer Architecture Instruction Set Architecture
Computer Architecture Instruction Set Architecture
CSCI206 - Computer Organization & Programming
Instructions: Language of the Computer
Lecture 4: MIPS Instruction Set
ECE3055 Computer Architecture and Operating Systems MIPS ISA
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
32-bit MIPS ISA.
ENGR 3410 – Computer Architecture Mark L. Chang Fall 2006
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
Instructions - Type and Format
CSCI206 - Computer Organization & Programming
Addressing in Jumps jump j Label go to Label op address 2 address
MIPS Instructions.
ECE232: Hardware Organization and Design
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
Instruction encoding The ISA defines Format = Encoding
Lecture 5: Procedure Calls
Chapter 2 Instructions: Language of the Computer part 2
Computer Instructions
Flow of Control -- Conditional branch instructions
Program and memory layout
Lecture 6: Assembly Programs
Computer Architecture Procedure Calls
Systems Architecture I
MIPS Instruction Set Architecture
Flow of Control -- Conditional branch instructions
CSE 331 Computer Organization and Design Fall 2007 Week 4
Computer Architecture
MIPS R3000 Subroutine Calls and Stack
Presentation transcript:

ECE2030 Introduction to Computer Engineering Lecture 19: Program Control Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Tech

Program Execution on Processors How a program is executed on a processor? How instructions ordering are maintained? Are there times we need to change the flow of a program? How to handle change of flow of a program?

Program Counter How a sequence of instructions are fetched and executed by a computer? Program Counter (PC) A special register Provide the logical ordering of a program Point to the address of the instruction to be executed main: la $8, array lb $9, ($8) lb $10, 1($8) add $11, $9, $10 sb $11, ($8) addiu $8, $8, 4 lh $9, ($8) lhu $10, 2($8) sh $11, ($8) lw $9, ($8) lw $10, 4($8) sub $11, $9, $10 sw $11, ($8) PC

Program Counter (Little Endian) PC 0x20 0x58 0x2a 0x01 0x00 0x0b 0xa1 0x04 add $11,$9,$10 sb $11, ($8) PC+12 PC+16 PC+20 0x01 main: la $8, array lb $9, ($8) lb $10, 1($8) add $11, $9, $10 sb $11, ($8) addiu $8, $8, 4 lh $9, ($8) lhu $10, 2($8) sh $11, ($8) lw $9, ($8) lw $10, 4($8) sub $11, $9, $10 sw $11, ($8) 0x10 la $8, array 0x08 0x3c 0x00 0x09 0x81 PC+4 lb $9, ($8) 0x01 0x00 0x0a 0x81 PC+8 lb $10, 1($8) Each MIPS instruction is 4-byte wide

Program Counter Control 32 + 32-bit PC Register System clock Instruction Register 32 2kx32 INSTRUCTION MEMORY Data (i.e. instruction) 32 4

Change of Flow Scenarios Our current default mode Program executed in sequential order When a program will break the sequential property? Subroutine Call and Return Conditional Jump (with Test/Compare) Unconditional Jump MIPS R3000/4000 uses Unconditional absolute instruction addressing Conditional relative instruction addressing

Absolute Instruction Addressing The next PC address is given as an absolute value PC address = <given address> Jump class examples J LABEL JAL LABEL JALR $r JR $r

Absolute Addressing Example main: la $8, array lb $9, ($8) lb $10, 1($8) add $11, $9, $10 sb $11, ($8) j Label2 ... Label2: addiu $8, $8, 4 lh $9, ($8) lhu $10, 2($8) Assume no delay slot J Label2 Encoding: Label2=0x00400048 J opcode= (000010)2 Encoding=0x8100012 Temp = <Label2 26-bit addr> PC = PC31..28|| Temp || 02

Relative Instruction Addressing An offset relative to the current PC address is given instead of an absolute address PC address = <current PC address> + <offset> Branch class examples bne $src, $dest, LABEL beq $src, $dest, LABEL bgez $src, LABEL Bltz $src, LABEL Note that there is no blt instruction, that’s why slt is needed. To facilitate the assembly programming, there is a “blt pseudo op” that programmers can use.

Relative Addressing Example la $8, array beq $20, $22, L1 lb $10, 1($8) add $11, $9, $10 sb $11, ($8) L1: addiu $8, $8, 4 Assume no delay slot beq $20,$22,L1 Encoding=0x12960004 (next slide) Target=(offset15)14 || offset || 02 PC = PC + Target

BEQ Encoding (I-Format) rs Offset Value = (Target addr – beq addr)/4 = # of instructions in-between including beq instruction beq $20, $22, L1 rt Branch to L1 if $20==$22 Offset = 4 instructions 1 31 opcode rs rt 26 25 21 20 16 15 Offset Value 1 Encoding = 0x12960004

Relative Addressing Example The offset can be positive as well as negative L2: addiu $20, $22, 4 lw $24, ($22) lw $23, ($20) beq $24, $23, L2 Offset = -3 instructions beq $24, $23, L2 1 31 opcode rs rt 26 25 21 20 16 15 Offset Value 1 Encoding = 0x1317FFFD

MIPS Control Code Example See array.s and prime.s

Procedural Calls in MIPS MIPS uses jal or jalr to perform procedure calls (assume no branch delay slot) jal LABEL # r31 = PC + 4 jal rd, rs # rd = PC + 4 Return address is automatically saved When return from procedure jr $31 or jr $d Note that $31 is aliased to $ra Call convention Use $a0 to $a3 ($4 to $7) for the first 4 arguments Use $v0 ($2) for passing the result back to the caller

Procedural Call Example C code snippet z = pyth(x, y); z = sqrt(z); int pyth(int x, int y) { return(x2+y2); } MIPS snippet la $t0, x la $t1, y lw $a0, ($t0) lw $a1, ($t1) jal pyth add $a0, $v0, $zero jal sqrt la $t2, z sw $v0, ($t2) pyth: mult $a0, $a0 mflo $t0 mult $a1, $a1 mflo $t1 add $v0, $t0, $t1 jr $ra sqrt: …. jr $ra Note that by software convention, $t0 to $t7 are called “caller-saved” registers, i.e. the caller is responsible to back them up before procedure call if they are to be used after the procedure call again

Procedural Call: Recursion MIPS snippet la $t0, x lw $a0, ($t0) jal fact fact: addi $v0, $zero, 1 blt $a0, $v0, L1 addi $a0, $a0, -1 jal fact L1: jr $ra C code snippet z = fact(x); int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)) } ? $ra ($31) is overwritten and old value is lost

Procedural Call: Many Arguments MIPS snippet la $t0, a lw $a0, 0($t0) lw $a1, 4($t0) lw $a2, 8($t0) lw $a3, 12($t0) lw ???, 16($t0) foo: ... jr $ra C code snippet long a[5]; z = foo(a[0], a[1], a[2], a[3], a[4]); int foo(int t, int u, int v, int w, int x) { return(t+u-v*w+x); } Run out of passing argument registers !

Stack The solution to address the prior 2 problems a LIFO (Last-In First-Out) memory A scratch memory space Typically grow downward (i.e. push to lower address)

Stack Stack Frame Push Pop Base pointed by a special register $sp (=$29) Each procedure has its own stack frame (if stack space is allocated) Push Allocate a new stack frame when entering a procedure subu $sp, $sp, 32 What to store? Return address Additional passing arguments Local variables Pop Deallocate the stack frame when return from procedure addu $sp, $sp, 32

Stack Push New Stack Frame for foo() 4 bytes jal foo higher ... addr subu $sp, $sp, 32 addu $sp, $sp, 32 jr $ra higher addr $sp PUSH New Stack Frame for foo() lower addr

Stack Pop New Stack Frame for foo() 4 bytes jal foo higher ... addr subu $sp, $sp, 32 addu $sp, $sp, 32 jr $ra higher addr Current Stack Frame New Stack Frame for foo() $sp lower addr

Recursive Call MIPS snippet li $v0, 1 j fact fact: beq $a0, $v0, return return: jr $ra C code snippet z = fact(x); int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)) }

Recursive Call MIPS snippet li $v0, 1 j fact fact: beq $a0, $v0, return jal fact return: jr $ra C code snippet z = fact(x); int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)) } What to do with $a0 and $ra ?

Push onto the Stack MIPS snippet li $v0, 1 j fact fact: beq $a0, $v0, return sub $sp, $sp, 8 sw $ra, 4($sp) sw $a0, 0($sp) sub $a0, $a0, 1 jal fact return: jr $ra C code snippet z = fact(x); int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)) } What happens after returning from the procedure ??? $sp Return address $a0 (= X)

Pop from the Stack MIPS snippet li $v0, 1 j fact fact: beq $a0, $v0, return sub $sp, $sp, 8 sw $ra, 4($sp) sw $a0, 0($sp) sub $a0, $a0, 1 jal fact lw $a0, 0($sp) lw $ra, 4($sp) mult $a0, $v0 mflo $v0 add $sp, $sp, 8 return: jr $ra C code snippet z = fact(x); int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)) } Return address $a0 (= X) $sp

Recursive call for Factorial MIPS snippet li $v0, 1 j fact fact: beq $a0, $v0, return sub $sp, $sp, 8 sw $ra, 4($sp) sw $a0, 0($sp) sub $a0, $a0, 1 jal fact lw $a0, 0($sp) lw $ra, 4($sp) mult $a0, $v0 mflo $v0 add $sp, $sp, 8 return: jr $ra C code snippet z = fact(x); int fact(int n) { if (n < 1) return(1); else return(n * fact(n-1)) }