COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.

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
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
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.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
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.
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
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.”
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.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
Lecture 6 Sept 16 Chapter 2 continue MIPS translating c into MIPS – examples MIPS simulator (MARS and SPIM)
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
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 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
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,
Lecture 4: MIPS Instruction Set
IFT 201: Unit 1 Lecture 1.3: Processor Architecture-3
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
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.
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 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.
Chapter 2 Instructions: Language of the Computer.
Computer Architecture & Operations I
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Computer Architecture Instruction Set Architecture
The University of Adelaide, School of Computer Science
Lecture 4: MIPS Instruction Set
Computer Architecture & Operations I
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
The University of Adelaide, School of Computer Science
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
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
The University of Adelaide, School of Computer Science
Lecture 6: Assembly Programs
Systems Architecture I
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Presentation transcript:

COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji

Review This Class Conditional Instructions Procedure Quiz 3

Instructions for Making Decisions High-level programming language C/C++: if … else … (conditional) goto(unconditional) for, while, until (loops) Assembly Languages MIPS: beq (branch if equal) bne (branch if not equal) j(unconditional jump)

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

Compiling If Statements C code: if (i==j) f = g+h; else f = g-h;

Compiling If Statements C code: if (i==j) f = g+h; else f = g-h; f ($s0), g ($s1), h($s2), i($s3), j($s4) Compiled MIPS code: bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: … Assembler calculates addresses

Compiling Loop Statements C code: while (save[i] == k) i = i+1; i in $s3, k in $s5, address of save in $s6 Flowchart?

Compiling Loop Statements C code: while (save[i] == k) i = 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: …

Basic Blocks A basic block is a sequence of instructions with No embedded branches (except at end) No branch targets (except at beginning) A compiler identifies basic blocks for optimization An advanced processor can accelerate execution of basic blocks

More Conditional Operations Less than Greater than Combination of logical operations

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

Exercise Convert the following C++ statement into MIPS if (i>j && i<k) { a++; } Assume i in $s0, j in $s1, k in $s2, a in $s3

Exercise if (i>j && i<k) { a++; } Assume i in $s0, j in $s1, k in $s2, a in $s3 slt $t0, $s1, $s0 slt $t1, $s0, $s2 and $t0, $t0, $t1 beq $t0, $zero, L addi $s3, $s3, 1 L: …

Better Solution if (i>j && i<k) { a++; } Assume i in $s0, j in $s1, k in $s2, a in $s3 slt $t0, $s1, $s0 beq $t0, $zero, L slt $t0, $s0, $s2 beq $t0, $zero, L addi $s3, $s3, 1 L: …

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

Signed vs. Unsigned Signed comparison: slt, slti Unsigned comparison: sltu, sltui Example $s0 = $s1 = slt $t0, $s0, $s1 # signed –1 < +1  $t0 = 1 sltu $t0, $s0, $s1 # unsigned +4,294,967,295 > +1  $t0 = 0

Branch Addressing Branch instructions specify Opcode, two registers, target address oprsrtconstant or address 6 bits5 bits 16 bits

Branch Addressing Branch instructions specify Opcode, two registers, target address Addresses of the program have to fit in the 16- bit field. Directly using branch address Problem: Is far too small to be a realistic. oprsrtconstant or address 6 bits5 bits 16 bits

Branch Addressing Branch instructions specify Opcode, two registers, target address Addresses of the program have to fit in the 16- bit field. Directly using branch address Problem: Is far too small to be a realistic. Solution: use a register and branch address oprsrtconstant or address 6 bits5 bits 16 bits

Program Counter (PC)

PC-relative addressing In PC-relative addressing Because it is convenient for the hardware to increment the PC early to point to the next instruction, (which will be discussed in Chapter 4) The MIPS address is actually relative to the address of the following instruction (PC+4) instead of the current instruction (PC)

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

Jump Addressing Branch instructions target are near branch Jump instructions Invoke procedure that may not be near the call With long addresses in the format Use other forms of addressing

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

Jump Addressing Jump ( j and jal ) targets could be anywhere in text segment Encode full address in instruction (Pseudo)Direct jump addressing Target address = PC 31…28 : (address × 4) opaddress 6 bits 26 bits Replaces only the lower 28 bits of the PC, leaving the upper 4 bits of the PC unchanged.

Target Addressing Example Loop code from earlier example Assume Loop starting at location in memory. 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 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:… Replace the short-address condition branch

Addressing Mode Summary

Time for a Break (10 mins)

Review Last Session Procedure Call Steps of procedure call caller and callee Branch Addressing This Session Leaf procedure Next Session Non-leaf procedure Quiz

Procedure Procedure (function) A stored subroutine that performs a specific task based on the parameters with which it is provided Important when writing a large program Make code easier to understand Allow code to be reused Allow a programmer to focus on a specific task

Procedure Parameters of procedure Act as an interface between the procedure and the rest of the program and data Pass values Return results

Procedure Calling The exaction of a procedure, the program requires the following steps: 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

Caller and Callee Caller The program that instigates a procedure and provides the necessary parameter values Callee A procedure that executes a series of stored instructions based on parameters provided by the caller and then returns control to the caller

Register Usage Registers are the fastest place to hold data in computer, $a0 – $a3: arguments (reg’s 4 – 7) $v0, $v1: result values (reg’s 2 and 3) $t0 – $t9: temporaries Can be overwritten by callee $s0 – $s7: saved 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)

Procedure Call Instructions Procedure call: jump and link jal ProcedureLabel Address of following instruction put in $ra Jumps to target address Procedure return: jump register jr $ra Jump to the address specified in the register $ra ( Copies $ra to program counter )

Using More Registers Four arguments and two return value registers: $a0 – $a3: arguments (reg’s 4 – 7) $v0, $v1: result values (reg’s 2 and 3) If compiler needs more registers, Spill registers The process of putting less commonly used variables (or those needed later) into memory.

Using More Registers Stack A last-in-first-out queue for spilling registers Stack pointer $sp (register 29) Point to the address of the most recent element in the stack Push Add element onto the stack Pop Remove element from the stack

Stack By historical precedent, stack “grows” from higher addresses to lower addresses. Push Add element onto the stack By subtracting from the stack pointer addi $sp, $sp, -12 Pop Remove element from the stack By adding to the stack pointer addi $sp, $sp, 12

Leaf Procedure and non-Leaf Procedure Leaf Procedure Procedures that do not call other procedures Non-leaf Procedure Procedures that call other procedures

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

Leaf Procedure Example MIPS code: (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 Save $s0, $t1, $t0 on stack Procedure body Restore $s0, $t1, $t0 from the stack Result Return

Status of Stack

Temporary Registers MIPS Assumption $t0 – $t9: temporary registers that are not preserved by the callee on a procedure call $s0 – $s7: saved registers Must be preserved by callee on a procedure call If used, the callees saves and restores them

Simplified 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 Save $s0 on stack Procedure body Restore $s0 from the stack Result Return

Time for a Break (10 mins)

Review Last Session Registers used Stack jal and jr leaf and no-leaf procedure Allocating space for new data on the heap This Session Non-leaf Procedure Quiz

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

Non-Leaf Procedure Example C code: int fact (int n) { if (n < 1) return 1; else return n * fact(n - 1); } Argument n in $a0 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 = n - 1 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

What is preserved and what is not? Data and registers preserved and not preserved across a procedure call

Procedure Frame Revisiting Stack Stack not only stores the saved registers but also local variables that do not fit in registers local arrays or structures Procedure Frame (activation record) Segment of the stack containing a procedure’s saved registers and local variables Frame pointer $fp Point to the location of the saved registers and local variables for a given procedure

Local Data on the Stack The frame pointer points to the location where the stack pointer was. Procedure frame (activation record) Used by some compilers to manage stack storage

Global Pointer Two kinds of C/C++ variables automatic Local to a procedure Discarded when the procedure exits static Global to a procedure Still exist after procedure exits Can be revisited Global Pointer $gp Point to static area

MIPS Memory Layout 32-bit address space 0x ~ 0xFFFFFFFF Not available for user program For OS and ROM 0x ~0x7FFFFFFF Data Stack Dynamic Malloc() in C, New in java Static Static variables Constants 0x ~0x0FFFFFFF Text: Machine language of the user program 0x ~0x003FFFFF Reserved

Memory Layout

Stack and Heap grow toward each other

Register Summary Register 1: $at reserved for the assembler Register 26-27: $k0-$k1 reserved for the OS

Summary Conditional Instructions Procedure Call Next Class Characters Starting a Program Linking

What I want you to do Review Chapter 2 Work on your assignment 3