Languages and the Machine Chapter 5 CS221. Topics The Compilation Process The Assembly Process Linking and Loading Macros We will skip –Case Study: Extensions.

Slides:



Advertisements
Similar presentations
Chapt.2 Machine Architecture Impact of languages –Support – faster, more secure Primitive Operations –e.g. nested subroutine calls »Subroutines implemented.
Advertisements

Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Henk Corporaal TUEindhoven 2011
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.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 8: MIPS Instruction Set
The Assembly Language Level
3. Loaders & Linkers1 Chapter III: Loaders and Linkers Chapter goal: r To realize how a source program be loaded into memory m Loading m Relocation m Linking.
Chapter 3 Loaders and Linkers
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
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++
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Software Language Levels Machine Language (Binary) Assembly Language –Assembler converts Assembly into machine High Level Languages (C, Perl, Shell)
CS 536 Spring Code generation I Lecture 20.
Assemblers Dr. Monther Aldwairi 10/21/20071Dr. Monther Aldwairi.
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Software Development and Software Loading in Embedded Systems.
Chapter 8 :: Subroutines and Control Abstraction
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
A Simple Two-Pass Assembler
MIPS coding. SPIM Some links can be found such as:
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
5-1 Chapter 5 - Languages and the Machine Department of Information Technology, Radford University ITEC 352 Computer Organization Principles of Computer.
6-1 Chapter 6 - Languages and the Machine Computer Architecture and Organization by M. Murdocca and V. Heuring © 2007 M. Murdocca and V. Heuring Computer.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Basic Semantics Associating meaning with language entities.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Prof. Fateman CS 164 Lecture 281 Virtual Machine Structure Lecture 28.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Hello world !!! ASCII representation of hello.c.
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.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
Lecture 3 Translation.
Instruction Set Architectures
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
The University of Adelaide, School of Computer Science
Separate Assembly allows a program to be built from modules rather than a single source file assembler linker source file.
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Introduction to Compilers Tim Teitelbaum
Chapter 9 :: Subroutines and Control Abstraction
Lecture 4: MIPS Instruction Set
Loaders and Linkers: Features
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Lecture 30 (based on slides by R. Bodik)
Computer Organization and Design Assembly & Compilation
A Simple Two-Pass Assembler
Computer Architecture
10/6: Lecture Topics C Brainteaser More on Procedure Call
Presentation transcript:

Languages and the Machine Chapter 5 CS221

Topics The Compilation Process The Assembly Process Linking and Loading Macros We will skip –Case Study: Extensions to the Instruction Set – The Intel MMX™ and Motorola AltiVec™ SIMD Instructions

Compilation Process Assembly to Machine code fairly straightforward, but compilation is not Translate a program written in a high level language into a functionally equivalent program in assembly language Consider a simple high-level language assignment statement: –Foo = Bar + Zot + 15; Steps involved in compiling this statement into assembly code: –Lexical analysis: separate into tokens, Foo, =, +, etc. –Syntactic Analysis / Parsing : Determine that we are performing an assignment, VAR = EXPRESSION –Semantic Analysis : Determine that Foo, Bar, Zot are names, 4 is an integer –Code Generation : Determine the proper assembly code to perform the action ld [Bar], %r0, %r1 ld [Zot], %r0, %r2 addcc %r1, %r2, %r1 addcc %r1, 15, %r2 st %r2, %r0, [Foo]

Compiler Issues Each compiler specific to a particular ISA –E.g., an int on one machine may be 32 bits, on another may be 64 bits Cause of error in networking library ported to Alpha Int issue not a problem in Java; JVM specifies 32 bits –E.g., in previous example, if the ISA allowed operands of addcc to be memory addresses, we could have done addcc [Bar], [Zot], %r1 addcc %r1, 15, [Foo] –Hopefully the compiler generates efficient code but optimization is a tough issue! Cross compiler: one that generates code for a different ISA (example, CodeWarrior)

Mapping Variables to Memory Global variables –Accessible from anywhere in the program, given a fixed address –E.g., global variable X at memory address 400 Local variables –Also called automatic variables –Defined inside a function or method, e.g. void foo() { int a,b; … } –These variables created when foo is invoked, destroyed when foo exits –These variables are created by pushing them on the stack when the function is invoked, and are popped off when the function exits

Local Variables and the Stack Recall that the stack typically grows downward in memory Here we start with 1234 stored on the top of the stack Mem 048…048… SP = 8 Push FFFF Mem 048…048… SP = 4 FFFF 1234

Local Variables and the Stack In our case, local variables are “pushed” on the stack upon entering the function –void foo() { int a; } Copy SP into Frame Pointer FP (also called the Base Pointer, or BP) Mem before Foo 048…048… SP = 8 Mem in Foo 048…048… SP = 4 Var a 1234 FP = 8

Accessing Stack Variables These variables are referenced as offsets from the frame pointer, called based addressing To access a : [%fp – 4] Mem in Foo 048…048… SP = 4 Var a 1234 FP = 8 Why not use [%sp] ? Consider pushing lots of stuff on the stack… Or data structures

C to ASM Example on x86 #include int c; int main() { int a,b; a=3; b=4; c=a*b; } pushl%ebp movl%esp, %ebp subl$8, %esp movl $3, -4(%ebp) movl $4, -8(%ebp) movl -4(%ebp),%eax imul1 -8(%ebp),%eax movl%eax, c ….comm c,4,4

Arrays in Memory Arrays may be allocated on the stack or allocated off the heap, a pool of memory where portions may be dynamically allocated. Access elements of an array a bit different than regular variables. int A[10];Array of 10 integers Mem allocated for A … 40 A (Base) = 4A[0] A[1] … A[9] ElementAddr = A + (Index*Size) e.g. A[2] is at 4 + (2*4) = 12

If-Statements Conditional statements map to a comparison and a branch instruction C –if (x==y) statement1; else statement2; Assembly (assume X in r1, Y in r2) –subcc %r1, %r2 ! Zero flag set if res=0 –bne Statement2 ! Branch if zero flag is not set –! Statement1 code –ba StatementNext! Branch always Statement2: ! Statement2 code StatementNext:

Loops While, Do-While, For loops implemented using the same conditional check and branch as the if-then statement –The branch returns back to previous code instead of jumping forward over code

Production Level Assemblers Allow programmer to specify location of data and code Provide mnemonics for all instructions and addressing modes Permit the use of symbolic labels to represent addresses and constants Provide a means to specify the starting address of the program Include a way to share variables between different assembled programs Support macros

Assembly Example

Assembled Code

Two Pass Assemblers Most assemblers are “two-pass” –First pass Determine addresses of all data and instructions Perform any assembly-time arithmetic Put definitions and constants into the symbol table –Second pass Generate machine code Insert actual addresses and values of symbols which are known from the symbol table –Two passes useful for forward references, i.e. referencing later on in the program

Forward Reference

Symbol Table Generated during the first pass Maps identifiers to values, table filled in as values are encountered and the program is parsed from top to bottom.org 2048; Says assemble code starting at 2048 const.equ value ; Defines const equal to value

Assembled Program

Final Tasks of the Assembler Linking and Loading We need the following additional info –Module name and size –Address start symbol –Information about global and external symbols –Information about any library routines –Values of constants –Relocation information

Location of Programs in Memory We have been using.org to specify a fixed start location Typically we will want programs capable of running in arbitrary locations –If we are concatenating together different modules, the addresses for identifiers in the different modules must be relocated Linker : software that combines separately assembled modules Loader : software that loads another program into memory and may modify addresses if the program is loaded in a location different from the origin –Must also set appropriate registers, e.g. %SP

Linking:.global and.extern A.global is used in the module that a symbols is defined and.extern is used in every other module that refers to it

Linking and Loading Symbol tables for previous example Symbols whose address might change market relocatable (not all addresses! Some may be fixed)

DLL’s Windows uses Dynamic Link Libraries, or DLL’s Linking a common routine in many programs results in duplicate code from that common routine in each program In a DLL, commonly used routines (e.g. memory management, graphics) present in only one place, the DLL –Smaller program sizes, each program does not need to have its own copy –All programs share the exact same code while executing –Don’t need recompiling or relinking Disadvantages –Deletion of a shared DLL by mistake can cause problems –Versions must be the same –DLL code file can live in many places in Windows –“DLL Hell”

Macros An assembly macro looks kind of like defining a subroutine For example, there say that there is no PUSH instruction to push data on the stack. We can make a macro for push:

Macro Expansion Given the previous macro, we could now write the following code: push %r15! Push r15 on the stack push %r20! Push r20 on the stack Upon assembly, these macros are expanded to generate the following actual code: addcc %r14, -4, %r14 st %r15, %r14 addcc %r14, -4, %r14 st %r20, %r14

Macros vs. Subroutines Later we will see how to write actual subroutines we can call –Only one copy of the shared code in a subroutine Tradeoffs –Subroutines Takes up less memory since only one copy of the code But slower than macros; subroutines have overhead of invoking and returning –Macros Take up more space than subroutine call due to macro expansion for each occurrence of the macro Faster than subroutines; no overhead to invoke/return

Skipping for now Discussion on Pentium MMX We may return to this later if time permits