10/6: Lecture Topics C Brainteaser More on Procedure Call

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
Lecture 9: MIPS Instruction Set
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
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.
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.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
Assembly Code Example Selection Sort.
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.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
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
Procedure call frame: Hold values passed to a procedure as arguments
Lecture 8: MIPS Instruction Set
Computer Organization CS224 Fall 2012 Lesson 12. Synchronization  Two processors or threads sharing an area of memory l P1 writes, then P2 reads l Data.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
Intro to Computer Architecture
MIPS Assembler Programming
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
RISC Concepts, MIPS ISA and the Mini–MIPS project
CS 61C L14Introduction to MIPS: Instruction Representation II (1) Garcia, Spring 2004 © UCB Roy Wang inst.eecs.berkeley.edu/~cs61c-tf inst.eecs.berkeley.edu/~cs61c.
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/
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
MIPS coding. SPIM Some links can be found such as:
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Spr 2015, Feb 9... ELEC / Lecture 4 1 ELEC / Computer Architecture and Design Spring 2015 Compiling and Executing Programs.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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,
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
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.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Lecture 3 Translation.
Computer structure: Procedure Calls
Computer Architecture & Operations I
Lecture 6: Assembly Programs
Computer Architecture Instruction Set Architecture
The University of Adelaide, School of Computer Science
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Instructions - Type and Format
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
The University of Adelaide, School of Computer Science
Logical and Decision Operations
Computer Organization and Design Assembly & Compilation
10/4: Lecture Topics Overflow and underflow Logical operations
Lecture 6: Assembly Programs
Procedures and Calling Conventions
Systems Architecture I
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

10/6: Lecture Topics C Brainteaser More on Procedure Call More than four arguments A recursive example Starting a Program Exercise 3.2 from H+P

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);

5+ Arguments Up to four arguments can be passed in registers $a0-$a3 To pass more than four, Push them on the stack Set the frame pointer ($fp) to point to them The frame pointer provides a stable base register for the duration of the procedure why not just use $sp?

Stack Allocation $sp Low address $fp $sp $sp $fp $fp Before During Local vars. Callee’s stack frame Saved $s regs. Saved $ra Saved args $fp $sp $sp Caller’s stack frame Caller’s stack frame $fp $fp Before During After High address

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

Recursive Procedure Call A recursive procedure calls itself With the stack, no more difficult than nested procedure call int fact(int n) { if(n < 1) return 1; else return (n * fact(n-1)); }

Factorial Implementation fact: subi $sp, $sp, 8 sw $ra, 4($sp) sw $a0, 0($sp) slt $t0, $a0, 1 beq $t0, $zero, L1 add $v0, $zero, 1 add $sp, $sp, 8 jr $ra L1: sub $a0, $a0, 1 jal fact lw $a0, 0($sp) lw $ra, 4($sp) mult $v0, $a0, $v0

Starting a Program Two phases from source code to execution Compile time compiler creates assembly assembler creates machine code linker creates an executable Run time loader moves the executable into memory and starts the program

Compile Time You’re experts on compiling from source to assembly Two parts to translating from assembly to machine language: Instruction encoding (including translating pseudoinstructions) Translating labels to addresses Label translations go in the symbol table

Symbol Table Symbols are names of global variables or labels (including procedure entry points) Symbol table associates symbols with their addresses in the object file This allows files compiled separately to be linked Label1: 0x01031ff0 bigArray 0x10006000

Modular Program Design Small projects may use only one file Any time any one line changes, recompile and reassemble the whole thing (death of Pascal) For larger projects, recompilation time is significant Solution: split project into modules compile and assemble modules separately link the object files

The Compiler + Assembler Translates high-level language source files into object files Object files Contains machine instructions (1’s & 0’s) Bookkeeping information Procedures and variables the object file defines Procedures and variables the source files use but are undefined (unresolved references) Debugging information associating machine instructions with lines of source code

Object File Example main.c area.c main.o area.o double PI = 3.14159; double A = Area( 5.0 ); } extern int ; double Area( double r ) { return PI * r * r; main.c area.c code: static data: defined symbols: undefined symbols: main.o area.o

The Linker The linker’s job is to “stitch together” the object files: 1. Place the data modules in memory 2. Determine the addresses of data and labels 3. Match up references between modules

Placing Modules in Memory Link a word processor application: text Editing text text static data Spell checking static data static data Paperclip

Determining Addresses Some addresses change during memory layout Modules were compiled in isolation Absolute addresses must be relocated Object file keeps track of instructions that use absolute addresses text text

Resolving References The editing module calls a routine from the spell checker That symbol is unresolved at compile time The linker matches unresolved symbols to locations in other modules

Linker Example main.o area.o LINKER main.exe code: main:A=area(5.0) static data: PI = 3.1415 defined symbols: main, PI undefined symbols: Area code: Area:return PI*r*r static data: defined symbols: Area undefined symbols: PI LINKER main.exe header code: main:A=area(5.0) Area:return PI*r*r static data: PI = 3.1415 defined symbols: main, PI, Area

Libraries Some code is used so often, it is bundled into libraries for common access Libraries contain most of the code you use but didn’t write: e.g., printf() Library code is (often) merged with yours at link time main.o main.exe libc.a

The Executable End result of compiling, assembling, and linking: the executable Contains: Header, listing the lengths of the other segments Text segment Static data segment Potentially other segments, depending on architecture & OS conventions

Run Time We’ll learn a lot more about this during the OS part of the course In a nutshell: Some dynamic linking may occur some symbols aren’t defined until run time Windows’ dlls (dynamic link library) why do this? The segments are loaded into memory The OS transfers control to the program and it runs

add $a1, $a1, $a1 add $v0, $zero, $zero add $t0, $zero, $zero outer: add $t4, $a0, $t0 lw $t4, 0($t4) add $t5, $zero, $zero add $t1, $zero, $zero inner: add $t3, $a0, $t1 lw $t3, 0($t3) bne $t3, $t4, skip addi $t5, $t5, 1 skip: addi $t1, $t1, 4 bne $t1, $a1, inner slt $t2, $t5, $v0 bne $t2, $zero, next add $v0, $t5, $zero add $v1, $t4, $zero next: addi $t0, $t0, 4 bne $t0, $a1, outer

int size = 5000; int values[size]; int mode = 0; int modeCount = 0; int i,j; for( i = 0; i < size; i++ ) { count = 0; for( j = 0; j < size; j++ ) { if( values[i] == values[j] ) { count++; } if( count > modeCount ) { modeCount = count; mode = values[i];