10/4: Lecture Topics Overflow and underflow Logical operations

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
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
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.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
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.
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.
Ch. 8 Functions.
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
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.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
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)
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
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.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
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.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
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:
Subroutines, parameters and the stack Bryan Duggan.
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.
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.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Architecture & Operations I
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
© Craig Zilles (adapted from slides by Howard Huang)
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Procedures (Functions)
Instructions - Type and Format
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Functions.
Lecture 5: Procedure Calls
Logical and Decision Operations
Topic 3-a Calling Convention 1/10/2019.
Program and memory layout
Procedures and Calling Conventions
Systems Architecture I
Computer Architecture
10/6: Lecture Topics C Brainteaser More on Procedure Call
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

10/4: Lecture Topics Overflow and underflow Logical operations Procedure calls

Overflow and Underflow Overflow occurs when a number is too big to represent usually as the result of a numerical operation unsigned ints, > 232-1 signed ints, > 231-1 floats, > 3.40282347e+38 doubles, > 1.7976931348623157e+308 Underflow means the number is too small to represent unsigned ints < 0 signed ints, < -231 floats, > 1.17549435e-38 doubles, > 2.2250738585072014e-308

Logical Operations Bitwise operations (and/andi & or/ori) Shift operations left shift 0000 1111 << 2 = 0011 1100 (sll) right shift 0000 1111 >> 2 = 0000 0011 Signed >> 1110 0010 >> 2 = 1111 1000 sra Unsigned >> 1110 0010 >> 2 = 0011 1000 srl

Examples Evaluate the following Fill in the body of this procedure (1100 1100 & 0001 1111) | 1100 0000 (1100 1100 >> 3) | 1100 0000 Fill in the body of this procedure int GetBitFromPosition( int num, int pos ) { if( ( pos < 0 ) || ( pos >= 32 ) ) { fprintf( stderr, “You idiot.\n” ); return 0; } return

Examples Continued f is a single precision floating point number write code to extract the actual exponent from f

Procedure calls in assembly int fact( int n ) { int result; if( n <= 1 ) result = 1; else result = n * fact(n-1); return result; } main() { int i; i = fact( 5 );

Procedure Call More than just a branch and a return Data goes in arguments, parameters Data goes back out return value What makes this possible? the stack

Review of Stacks Two operations: push an item onto the stack pop an item off the stack

Stack Implementations Pretty easy to do with a linked list You probably saw this in 143 or 373 Top

A Stack in an Array Linked lists are nice if you have them Arrays are a lot faster A[5] A[4] Top A[3] A[2] A[1] A[0]

Calling Conventions Sequence of steps to follow when calling a procedure Determines: where arguments are passed to the callee how to transfer control from caller to callee and back where return values passed back out no unexpected side effects such as overwritten registers

Calling Conventions Mostly governed by the compiler We’ll see a MIPS calling convention Not the only way to do it, even on MIPS Most important: be consistent Procedure call is one of the most unpleasant things about writing assembly for RISC architectures

A MIPS Calling Convention 1. Place parameters where the procedure can get them 2. Transfer control to the procedure 3. Get the storage needed for the procedure 4. Do the work 5. Place the return value where the calling code can get it 6. Return control to the point of origin

Step 1: Parameter Passing The first four parameters are easy - use registers $a0, $a1, $a2, and $a3 You’ve seen this already What if there are more than four parameters?

Step 2: Transfer Control Getting from caller to callee is easy -- just jump to the address of the procedure Need to leave a way to get back again Special register: $ra (for return address) Special instruction: jal

Jump and Link Calling code Procedure proc: add .. jal proc

Step 3: Acquire Storage What storage do we need? Registers Other local variables Where do we get the storage? From the stack

Refining Program Layout Address Reserved 0x00400000 Program instructions Text 0x10000000 Static data Global variables 0x10008000 Dynamic data heap Local variables, saved registers Stack 0x7fffffff

Saving Registers on the Stack $sp $s2 $s1 $s0 $sp $sp Before Procedure During Procedure After Procedure

Assembly for Saving Registers We want to save $s0, $s1, and $s2 on the stack sub $sp, $sp, 12 # make room for 3 words # “addi $sp, $sp, -12” sw $s0, # store $s0 sw $s1, # store $s1 sw $s2, # store $s2

Step 4: Do the work We called the procedure so that it could do some work for us Now is the time for it to do that work Resources available: Registers freed up by Step 3 All temporary registers ($t0-$t9)

Callee-saved vs. Caller-saved Some registers are the responsibility of the callee callee-saved registers $s0-$s7 Other registers are the responsibility of the caller caller-saved registers $t0-$t9

Step 5: Return values MIPS allows for two return values Place the results in $v0 and $v1 You’ve seen this too Why are there two return values? What if the procedure needs more than two return values?

Step 6: Return control Because we laid the groundwork in step 2, this is easy Address of the point of origin + 4 is in register $ra Just use jr $ra to return

An Example int leaf(int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Let g, h, i, j be passed in $a0, $a1, $a2, $a3, respectively Let the local variable f be stored in $s0

Compiling the Example leaf: sub $sp, $sp, 4 # make room for $s0 # addi $sp, $sp, -4 sw $s0, 0($sp) # store $s0 add $t0, $a0, $a1 # $t0 = g + h add $t1, $a2, $a3 # $t1 = i + j sub $s0, $t0, $t1 # $s0 = f add $v0, $s0, $zero # copy result lw $s0, 0($sp) # restore $s0 addi $sp, $sp, 4 # put $sp back jr $ra # jump back to caller

Nested Procedures Suppose we have code like this: Potential problem: the return address is stored in $ra which will get overwritten main() { foo(); } int foo() { return bar(); int bar() { return 6;

A Trail of Bread Crumbs The registers $s0-$s7 are not the only ones we save on the stack What can the caller expect to have preserved across procedure calls? What can the caller expect to have overwritten during procedure calls?

Preservation Conventions Preserved Not Preserved Saved registers: $s0-$s7 Stack pointer register: $sp Return address register: $ra Stack above the stack pointer Temporary registers: $t0-$t9 Argument registers: $a0-$a3 Return value registers: $v0-$v1 Stack below the stack pointer

A Brainteaser in C What does this program print? Why? #include <stdio.h> int* foo() { int b = 6; return &b; } void bar() { int c = 7; main() { int *a = foo(); bar(); printf(“The value at a is %d\n”, *a);

Activation Record For a procedure call, the activation record is the portion of the stack containing saved registers local variables Also known as procedure frame