How Computers Work Lecture 2 Assembly Tools Calling Conventions

Slides:



Advertisements
Similar presentations
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Advertisements

SE 292 (3:0) High Performance Computing L2: Basic Computer Organization R. Govindarajan
Embedded Software 1. General 8051 features (excluding I/O) CPU 8 bit microcontroller The basic registers include (more discussed later) The 8-bit A (accumulator)
Lilian Blot Recursion Autumn 2012 TPOP 1. Lilian Blot Recursion Autumn 2012 TPOP 2.
The “Little Man Computer” Version
Based on Mike Feeley’s original slides; Modified by George Tsiknis Unit 11 Local Variables, Parameters and the Stack Relevant Information CPSC 213 Companion.
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
Pointers and Arrays Chapter 12
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Data Structures Using C++ 2E
Instruction execution and sequencing
CSC 3210 Computer Organization and Programming
CPSC 330 Fall 1999 HW #1 Assigned September 1, 1999 Due September 8, 1999 Submit in class Use a word processor (although you may hand-draw answers to Problems.
THUMB Instructions: Branching and Data Processing
Code Generation.
Based on Mike Feeley’s and Tamara Munzner’s original slides; Modified by George Tsiknis Parameters and Local Variables Relevant Information CPSC 213 Companion.
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.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
Computer Architecture CSCE 350
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
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
CS 536 Spring Code generation I Lecture 20.
Intro to Computer Architecture
Run time vs. Compile time
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
6.004 – Fall /17/0L12 – Instruction Set 1 Designing an Instruction Set Lab 4 due today NEXT TUESDAY, 10/22!
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
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:
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
How Computers Work Lecture 3 Page 1 How Computers Work Lecture 3 A Direct Execution RISC Processor: The Unpipelined BETA.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Lecture 3 Translation.
Chapter 14 Functions.
Writing Functions in Assembly
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
Computer Architecture Instruction Set Architecture
Lecture 4: MIPS Instruction Set
Writing Functions in Assembly
Chapter 7 Subroutines Dr. A.P. Preethy
6.001 SICP Stacks and recursion
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Stack Frame Linkage.
MIPS Instructions.
Chapter 8 Central Processing Unit
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Topic 3-a Calling Convention 1/10/2019.
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

How Computers Work Lecture 2 Assembly Tools Calling Conventions

Review: b Model of Computation Fetch/Execute Loop: Processor State Instruction Memory PC • Fetch <PC> • PC ¬ <pc> + 1 • Execute fetched instruction • Repeat! r0 r1 r2 r31 32 bits always 0 32 bits (4 bytes) next instr

Review: BETA Instructions Two 32-bit Instruction Formats: Unused Rc Ra OPCODE Rb OPCODE Ra 16 bit Constant Rc

Review: b ALU Operations SIMILARLY FOR: What the machine sees (32-bit instruction word): • SUB, SUBC • (optional) MUL, MULC DIV, DIVC BITWISE LOGIC: • AND, ANDC • OR, ORC • XOR, XORC SHIFTS: • SHL, SHR, SAR (shift left, right; shift arith right) COMPARES CMPEQ, CMPLT, CMPLE Unused Rc Ra OPCODE Rb What we prefer to see: symbolic ASSEMBLY LANGUAGE ADD(ra, rb, rc) rc ¬ <ra> + <rb> “Add the contents of ra to the contents of rb; store the result in rc” Alternative instruction format: OPCODE Ra 16 bit Constant Rc ADDC(ra, const, rc) rc ¬ <ra> + sext(const) “Add the contents of ra to const; store the result in rc”

Review: b Loads & Stores LD(ra, C, rc) rc ¬ < Mem[<ra> + sext(C)] > “Fetch into rc the contents of the data memory location whose address is the contents of ra plus C” ST(rc, C, ra) Mem[<ra> + sext(C)] ¬ <rc> “Store the contents of rc into the data memory location whose address is the contents of ra plus C” NO BYTE ADDRESSES: only 32-bit word accesses are supported. This is similar to how Digital Signal Processors work It is somewhat unusual for general purpose processors, which are usual byte (8 bit) addressed

Review: b Branches Conditional: rc = <PC>+1; then BRNZ(ra, label, rc) if <ra> nonzero then PC <- <PC> + displacement BRZ(ra, label, rc) if <ra> zero then Unconditional: rc = <PC>+1; then BRZ(r31, label, rc) PC <- <PC> + displacement Indirect: rc = <PC>+1; then JMP(ra, rc) PC <- <ra> Note: “displacement” is coded as a CONSTANT in a field of the instruction!

Review: Iterative Optimized Factorial ;assume n = 20, val = 1 (define (fact-iter n val) (if (= n 0) val (fact-iter (- n 1) (* n val)) ) ) n: 20 val: 1 LD(n, r1) ; n in r1 LD(val, r3) ; val in r3 BRZ(r1, done) loop: MUL(r1, r3, r3) SUBC(r1, 1, r1) BRNZ(r1, loop) done: ST(r1, n) ; new n ST(r3, val) ; new val

Language Tools The Assembler STREAM of Words to be loaded into memory 01101101 11000110 00101111 10110001 ..... STREAM of Words to be loaded into memory Assembler Symbolic SOURCE text file Binary Machine Language Translator program Symbol Memory + Expression Evaluator Textual Macro Pre-Processor

Macros Macros are parameterized abbreviations that when invoked cause TEXTUAL SUBSTITION | Macro to generate 4 consecutive numbers: .macro consec4(n) n n+1 n+2 n+3 | Invocation of above macro: consec4(37) Is translated into: 37 37+1 37+2 37+3

Some Handy Macros | BETA Instructions: ADD(ra, rb, rc) | rc ¬ <ra> + <rb> ADDC(ra, const, rc) | rc ¬ <ra> + const LD(ra, C, rc) | rc ¬ <C + <ra>> ST(rc, C, ra) | C + <ra> ¬ <rc> LD(C, rc) | rc ¬ <C> ST(rc, C) | C ¬ <ra>

Constant Expression Evaluation 37 -3 255 decimal (default); 0b100101 binary (0b prefix); 0x25 hexadecimal (0x prefix); Values can also be expressions; eg: 37+0b10-0x10 24-0x1 4*0b110-1 0xF7&0x1F generates 4 words of binary output, each with the value 23

Symbolic Memory We can define SYMBOLS: x = 1 | 1 y = x + 1 | 2 ADDC(x, 37, y) | R2 ¬ <R1> + 37 Which get remembered by the assembler. We can later use them instead of their values:

How Are Symbols Different Than Macros? Answer: A macro’s value at any point in a file is the last previous value it was assigned. Macro evaluation is purely textual substitution. A symbol’s value throughout a file is the very last value it is assigned in the file. Repercussion: we can make “forward” references to symbols not yet defined. Implementation: the assembler must first look at the entire input file to define all symbols, then make another pass substituting in the symbol values into expressions.

Dot, Addresses, and Branches Special symbol “.” (period) changes to indicate the address of the next output byte. We can use . to define branches to compute RELATIVE address field: .macro BRNZ(ra,loc) betaopc(0x1E,ra,(loc-.)-1,r31) loop = . | “loop” is here... ADDC(r0, 1, r0) ... BRNZ(r3, loop) | Back to addc instr.

Address Tags Will loop 232 times! x: is an abbreviation for x =. -- leading to programs like x: 0 buzz: LD(x, r0) do { x = x-1; } ADDC(r0, -1, r0) ST(r0, x) BRNZ(r0, buzz) while (x > 0); ... Will loop 232 times!

Macros Are Also Distinguished by Their Number of Arguments We can extend our assembly language with new macros. For example, we can define an UNCONDITIONAL BRANCH: BR(label, rc) rc ¬ <PC>+4; then PC ¬ <PC> + displacement ___________________________________________ BR(label) PC ¬ <PC> + displacement BEQ(r31,lab,rc) BR(lab, r31) by the definitions .macro BR(lab, rc) BRZ (r31,lab, rc) .macro BR(lab) BR(lab,r31)

OK, How about recursive fact? (define (fact n) (if (= n 0) 1 (* n (fact (- n 1))) ) ) Suppose caller: 1. Stores nin agreed-on location (say, r1) 2. Calls fact using, say, BR(fact, r28) Then fact: 1. Computes value, leaves (say) in r0. 2. Returns via JMP(r28, r31) int fact(int n) { if (n == 0) return (1); else return (n * fact(n-1)); } R28 Convention: We call it the Linkage Pointer (just like scheme RML’s continue register)

Does this really work? fact: ... BR(fact, LP) (put result in r0) OOPS! int fact(int n) { if (n == 0) return (1); else return (n * fact(n-1)); } OOPS! We need a STACK ! ! !

Recursion... fact(3) ... Caller n=3 fact(3) n=2 fact(2) n=1 fact(1)

Stack Implementation one of several possible implementations Lower addresses Old Stuff Conventions: • Builds UP on push • SP points to first UNUSED location. (stacked data) Memory: (stacked data) (stacked data) (stacked data) <sp> unused space To push <x>: sp ¬ <sp> + 1; Mem[<sp> - 1] ¬ <x> To pop() a value into x: x ¬ <Mem[<sp> - 1]> sp ¬ <sp> - 1; PUSH Higher addresses New Stuff

Support Macros for Stacks sp = r29 push(rx) - pushes 32-bit value onto stack. ADDC(sp, 1, sp) ST(rx, -1, sp) pop(rx) - pops 32-bit value into rx. LD(rx, -1, sp) ADDC(SP, -1, sp) allocate(k) - reserve k WORDS of stack ADDC(SP, k, sp) deallocate(k) - give back k WORDS SUBC(SP, k, sp)

The Stack REVOLUTIONARY IMPACT: STACK as central storage-management mechanism... SIMPLE, EFFICIENT to implement using contiguous memory and a "stack pointer" ORIGINAL USE: subroutine return points - push/pop STACK DISCIPLINE follows natural order of call/return nesting. EXPANDED USE: "automatic" or "dynamic" allocation of local variables. REVOLUTIONARY IMPACT: ALL modern machines, to varying extents; ALL modern languages argi (arg) rtn PC rtn locj (Local) sp STACK DISCIPLINE: most recently allocated location is next location to be deallocated. IMPACT: BLOCK STRUCTURE. 2ND MAJOR INFLUENCE: STACKS. [REALITY: STACK IMPLEMENTATION TECHNOLOGY INFLUENCED PROGRAMMING MODEL, MORE THAN VV]

Call / Return Linkage lp = r28 sp = r29 Using these macros and r28 as a “linkage pointer”, we can call f by: BR(f, lp) And code procedure f like: f: PUSH(lp) | SAVE lp <perform computation> | (may trash lp) POP(lp) | RESTORE lp JMP(lp) | Return to caller

Recursion with Register-Passed Arguments | Compute Fact(n) | n passed in r1, result returned in r0 fact: PUSH(lp) | Save linkage pointer BRZ(r1,fact1) | terminal case? PUSH(r1) | Save n, ADDC(r1,-1,r1) | compute fact(n-1). BR(fact, lp) | recursive call to fact. POP(r1) | restore arg, MUL(r1,r0,r0) | return n*fact(n-1) factx: POP(lp) | restore linkage pointer JMP(lp) | and return to caller. fact1: MOVC(1,r0) | fact(0) => 1 BR(factx) .macro MOV(rsrc,rdest) ADD (rsrc, r31, rdest) .macro MOVC(csrc,rdest) ADDC (r31, csrc, rdest)

A Generic Stack Frame Structure: The 6.004 Stack Discipline RESERVED REGISTERS: bp = r27. Base ptr, points to 1st local. lp = r28. Linkage pointer, saved <PC>. sp = r29. Stack ptr, points to 1st unused word. xp = r30. Exception pointer, saved <PC> STACK FRAME (a.k.a. Function Activation Record): PUSH arguments old <lp> old <bp> bp: Local variables ... Temporaries sp: (as yet unused)

6.004 Stack Discipline Procedure Linkage Calling Sequence: PUSH(argn) | push args, in ... | RIGHT-TO-LEFT PUSH(arg1) | order! BR(f, lp) | Call f. DEALLOCATE(n) | Clean up! ... | (returned value now in r0) Entry Sequence: f: PUSH(lp) | Save <LP>, <BP> PUSH(bp) | for new calls. MOV(sp,bp) | set bp=frame base ALLOCATE(locals) | allocate locals (push other regs) | preserve regs used ... (pop other regs) | restore regs MOV(val, r0) | set return value MOV(bp,sp) | strip locals, etc POP(bp) | restore linkage POP(lp) | (the return <PC>) JMP(lp) | return. Return Sequence:

Stack Frame Detail old old <lp> old old <bp> CALLER’S caller’s local 0 ••• caller’s local n-1 arg n-1 (caller’s return PC) ••• arg 0 old <lp> CALLEE’S FRAME old <bp> local 0 bp: ••• local n-1 sp: free space

Access to Arguments & Local Variables To access jth local variable (j >= 0) LD(BP, (j, rx) or ST(rx, j, bp) To access jth argument (j >= 0): LD(BP, 3-j, rx) ST(rx, 3-j, bp) QUESTION: Why push args in REVERSE order??? arg 0 arg n-1 old <lp> old <bp> local 0 local n-1 free space bp: sp: •••

Procedure Linkage: The Contract The caller will: • Push args onto stack, in reverse order. • Branch to callee, putting return address into lp. • Remove args from stack on return. The callee will: • Perform promised computation, leaving result in r0. • Branch to return address. • Leave all regs (except lp, r0) unchanged.

Recursive factorial with stack-passed arguments | (define (fact n) | (if (= n 0) 1 (* n (fact (- n 1)))) | ) fact: PUSH(lp) | save linkages PUSH(bp) MOV(sp,bp) | new frame base PUSH(r1) | preserve regs LD(bp,-3,r1) | r0 ¬ n BRNZ(r1, big) | if n>0 MOVC(1,r0) | else return 1; BR(rtn) big: SUBC(r1,1,r1) | r1 ¬ (n-1) PUSH(r1) | arg1 BR(fact,lp) | fact(n-1) DEALLOCATE(1) | (pop arg) LD(bp, -3, r1) | r0 ¬ n MUL(r1,r0,r0) | r0 ¬ n*fact(n-1) rtn: POP(r1) | restore regs MOV(bp,sp) | Why? POP(bp) | restore links POP(lp) JMP(lp) | return.

What did we Learn Today? Next In Section C Tutorial How to call functions How to do recursive factorial The 6.004 Stack Discipline How to retrieve arguments and local variables Next In Section Practice with Stack Discipline C Tutorial http://www.csc.lsu.edu/tutorial/ten-commandments/bwk-tutor.html