Digital Logic Design Alex Bronstein

Slides:



Advertisements
Similar presentations
10/6: Lecture Topics Procedure call Calling conventions The stack
Advertisements

Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
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
Procedure call frame: Hold values passed to a procedure as arguments
1 Today  Remember to use your late days wisely —We can’t post solutions until all HWs have been turned in  One complete example —To put together the.
Intro to Computer Architecture
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
What are Exception and Interrupts? MIPS terminology Exception: any unexpected change in the internal control flow – Invoking an operating system service.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
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.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
6.S078 - Computer Architecture: A Constructive Approach Introduction to SMIPS Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts.
February 3, 2003Functions in MIPS1 CS 232 It is important that students bring a certain ragamuffin, barefoot irreverence to their studies; they are not.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
Constructive Computer Architecture Interrupts/Exceptions/Faults Arvind Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology.
Computer Organization Rabie A. Ramadan Lecture 3.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Exceptions and Interrupts “Unexpected” events requiring change in flow of control – Different ISAs use the terms differently Exception – Arises within.
Digital Logic Design Alex Bronstein
Computer Architecture & Operations I
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
Prof. Hsien-Hsin Sean Lee
Computer Organization CS224
Handling Exceptions In MIPS, exceptions managed by a System Control Coprocessor (CP0) Save PC of offending (or interrupted) instruction In MIPS: Exception.
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
the Operating System (OS)
Chapter 10 And, Finally... The Stack
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Functions and Procedures
Chapter 10 The Stack.
Pipelining: Advanced ILP
The processor: Exceptions and Interrupts
CSCE 212 Chapter 5 The Processor: Datapath and Control
Instructions - Type and Format
The processor: Pipelining and Branching
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Logical and Decision Operations
Review.
10/4: Lecture Topics Overflow and underflow Logical operations
Chapter 10 And, Finally... The Stack
Interrupts and Exception Handling
Program and memory layout
Procedures and Calling Conventions
ECE 445 – Computer Organization
Program and memory layout
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Interrupts and Exception Handling
Presentation transcript:

Digital Logic Design 234262 Alex Bronstein Lecture 9: Function calls, exceptions, interrupts

Abstraction Hierarchy Application Software Operating System (threads, files, exceptions) Computer Architecture (instruction set) Micro-Architecture (execution pipeline) Logic (adders, multipliers, FSMs) Digital Circuits (gates) Analog Circuits (amplifiers) Process and Devices (MOS FET transistors) Physics (electrons, holes, tunneling, band gap)

Control instructions Unconditional jump Conditional branch Function call/return Exception (aka software interrupt) Hardware interrupt

Function calls Invoking a function changes the flow of a program twice: Calling the function Returning from the function Each time a function is called, PC is updated and CPU has to store return address Function accepts arguments and returns return values void main() { … a = fact(8); b = fact(3); c = fact(8-3); coef = a/b/c; } int fact(int n) int i, f=1; for (i=n; i>1; i--) f = f*i; return f;

Function calls in MIPS Function call: jump-and-link (jal) instruction: jal (encoded as J-type) stores the PC of the next instruction in R31 and performs a jump jal target  $31 ← PC+4 j target Return from function: jr $s jr (encoded as R-type) sets PC to the value stored in the supplied Rs The only MIPS instructions that can access PC

Passing arguments and returning values MIPS uses the following conventions: Up to four arguments are passed on registers $4-$7 (alias $a0-$a3) Up to two values can be returned on registers $2-$3 (alias $v0-$v1) Note: assembly does not check types! A 32-bit value may be a signed/unsigned integer, single-precision floating point, or a pointer to an arbitrarily typed object Conventions are not enforced by hardware or assembler Compilers and assembly programmers must follow conventions so that functions written by different people interface with each other A “rogue” function may cause unpredictable results

Nested calls Stack-like behavior: last in-first out Someone calls A A calls B B calls C C returns to B B returns to A A returns Calling C overwrites arguments of B in $a0-$a3 and return address in $ra A: … jal B # $ra=A2 A2: … jr $ra B: … jal C # $ra=B2 B2: … C: …

Register spilling/filling The number of CPU registers is limited and it is possible that several functions will conflict over the same registers Important registers can be saved (spilling) before function executes and restored (filling) after function completes Who is responsible for saving registers – the caller or the callee? Where are they saved?

Who saves the registers? The caller knows which registers are important to it and shall be saved across function call The callee knows exactly which register it will use and overwrite Good engineering practices is black box: the caller and callee do not know anything about each other’s implementation Possible conventions: Caller saves them all Callee saves them all MIPS convention: split responsibilities between caller and callee

MIPS register conventions Register Alias Conventional use $2-$3 $v0-$v1 values from expression evaluation & function results $4-$7 $a0-$a3 arguments – first four arguments of a function $8-$15 $t0-$t7 temporaries $16-$23 $s0-$s7 saved values $31 $ra return address * saved by caller when necessary (callee may modify these registers) * saved by callee when necessary (caller may assume them unchanged by the callee) * saved by callee that is also a caller

Where are the registers spilled? Part of the memory is used for stack (a last in first out data structure) Stack grows downwards in terms of addresses Top element of the stack is pointed by (by convention) by register $29 (alias $sp) No push/pop instructions built into the ISA 7FFFFFFF stack $sp 00000000

Push To push elements into the stack: word 1 Decrement $sp to make room for new data Store elements on the stack To push registers $t1,$t2 onto the stack: sub $sp,8 sw $t1,4($sp) sw $t2,0($sp) or alternatively: sw $t1,-4($sp) sw $t2,-8($sp) word 1 $sp word 2 word 1 word 2 $t1 $sp $t2

Pop Elements can be accessed relative $sp word 1 To retrieve the value of $t1: lw $s0,4($sp) To pop elements from the stack increment $sp add $sp,8 Data past stack pointer still remain in memory but are considered invalid Arguments can be passed to function on stack word 1 word 2 $t1 $sp $t2 word 1 $sp word 2 $t1 $t2

Example of function call jal B … B: … jal C B2: … jr $ra C: … PC $sp

Example of function call jal B … B: … jal C B2: … jr $ra C: … PC saved $v*,$a*,$t*,$ra $sp B args

Example of function call jal B … B: … jal C B2: … jr $ra C: … saved $v*,$a*,$t*,$ra PC B args $sp saved $s*

Example of function call jal B … B: … jal C B2: … jr $ra C: … saved $v*,$a*,$t*,$ra B args PC saved $s* saved $v*,$a*,$t*,$ra $sp C args

Example of function call jal B … B: … jal C B2: … jr $ra C: … saved $v*,$a*,$t*,$ra B args saved $s* saved $v*,$a*,$t*,$ra PC C args $sp saved $s*

Example of function call jal B … B: … jal C B2: … jr $ra C: … saved $v*,$a*,$t*,$ra B args saved $s* saved $v*,$a*,$t*,$ra $sp C args PC saved $s*

Example of function call jal B … B: … jal C B2: … jr $ra C: … saved $v*,$a*,$t*,$ra $sp B args saved $s* saved $v*,$a*,$t*,$ra PC C args saved $s*

Example of function call jal B … jr $ra B: … jal C B2: … C: … $sp saved $v*,$a*,$t*,$ra PC B args saved $s* saved $v*,$a*,$t*,$ra C args saved $s*

Exceptions (software interrupts) MIPS terminology: Exception = unexpected change in control flow caused internally (e.g., arithmetic overflow, undefined instruction) or a desired change of control yielded to OS (e.g., system call) Interrupt = unexpected change in control flow caused externally (e.g., request from an I/O device) MIPS exceptions and interrupts are handled by a peripheral device “coprocessor 0” (cp0)

Coprocessor 0 MIPS exceptions and interrupts are handled by “coprocessor 0” (cp0) cp0 has several 32-bit registers EPC (cp0 register $14) – exception program counter Cause register (cp0 register $13) – contains bits identifying exception cause Status register (cp0 register $12) – is used to configure interrupts cp0 registers cannot be directly accessed by MIPS instructions Special instructions transfer information from/to cp0 like load/store: mfc0 $c0rt, $rd Move from coprocessor 0 Semantics: rd ← c0rt mtc0 $c0rd, $rt Move to coprocessor 0 Semantics: $c0rd ← rt

MIPS exception handling 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 b Pending intrpts ExCode Cause register (alias $cr): Exception code (bits 0-6): describe the cause of the last exception 0 INT External hardware interrupt 6 IBUS Invalid instruction 8 SYSCALL Program-initiated system call 12 OVF Arithmetic overflow Pending interrupts (bits 8-15): when an interrupt at a given SW/HW level is raised, the corresponding bit is asserted Branch delay (bit 31): set to one if last exception occurred in an instruction scheduled in a delay slot of a branch

MIPS exception handling 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Interrupt mask IE Status register (alias $sr): Interrupt enable (bit 0): interrupts are not handled and EPC is not updated when 0 Interrupt mask (bits 8-15): defining masks for the eight interrupt levels If an interrupt/exception occurs when its mask is set to 0, it is ignored though pending interrupt bit is raised

Exception vs function call When a function is called via jal: Caller saves state prior to issuing jal so callee can safely modify registers Control is transferred by setting PC to the address provided by the instruction Return address to next instruction (PC+4) is saved to $ra Function returns via jr $ra

Exception vs function call Exception/interrupt have no explicit call: Control is transferred to a fixed location (usually 0x80000080) where the exception handler must reside EPC stores the return address (PC+4) Status register IE bit is masked to disable interrupts If control is returned to the program, special instruction rfe (return from exception) restores the status register reenabling interrupts Exception is an unexpected event and exception handler shall take care of saving and restoring previous state Exception handlers may use registers $26-$27 (alias $k0-$k1) that are not used by user programs

Exception vs function call Typical exception return code mfc0 $k0, $epc rfe # issued in jr delay slot jr $k0