Compiling examples in MIPS

Slides:



Advertisements
Similar presentations
Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
Advertisements

MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
Assembly Code Example Selection Sort.
Fall EE 333 Lillevik 333f06-l4 University of Portland School of Engineering Computer Organization Lecture 4 Assembly language programming ALU and.
Wannabe Lecturer Alexandre Joly inst.eecs.berkeley.edu/~cs61c-te
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: 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++
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
Assembly Process. Machine Code Generation Assembling a program entails translating the assembly language into binary machine code This requires more than.
1 Lecture 25 (Assemblers). 2 Assemblers,Linkers,and Debuggers Assembler -Translates assembly to machine code. - Resolves symbolic address. - Extends virtual.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3.
Instruction Representation II (1) Fall 2007 Lecture 10: Instruction Representation II.
MIPS Assembler Programming
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.
The Assembly Language Level
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved The Assembly Language Level.
Computer Architecture and Design – ECEN 350 Part 4 [Some slides adapted from M. Irwin, D. Paterson and others]
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS coding. SPIM Some links can be found such as:
Lecture 15 Today’s lecture MARIE programming Assembler
1 (Based on text: David A. Patterson & John L. Hennessy, Computer Organization and Design: The Hardware/Software Interface, 3 rd Ed., Morgan Kaufmann,
The LC-3 – Chapter 7 COMP 2620 Dr. James Money COMP
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 4.
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.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Computer Science 210 Computer Organization More on Assembler.
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
The Assembly Language Level Part C – Linking and Loading.
The Assembly Process Computer Organization and Assembly Language: Module 10.
Lecture 16: 10/29/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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.
Compiling examples in MIPS
CSCI206 - Computer Organization & Programming
Computer Science 210 Computer Organization
Compiler Construction (CS-636)
System Programming and administration
MIPS Coding Continued.
MIPS assembly syntax Comments
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Conditional Branches What distinguishes a computer from a simple calculator is its ability to make decisions Decisions are made using the if statement,
Assembler Design Options
Instructions for Making Decisions
Computer Science 210 Computer Organization
MIPS coding.
The Assembly Language Level
Chapter 7 LC-2 Assembly Language.
Lecture 4: MIPS Instruction Set
CSCI206 - Computer Organization & Programming
ARM Control Structures
Assembler Directives Example program: .data # DATA segment city: .asciiz “Seattle” .align 2 num: .word 2210, 2341, 26, 0x022ec,
MIPS Instructions.
MIPS Instruction Encoding
MIPS Instruction Encoding
Computer Organization and Design Assembly & Compilation
MIPS Coding.
The Assembly Language Level
Optional Assembler Features 2
Computer Architecture
MIPS Coding Continued.
MIPS coding.
MIPS assembly.
10/6: Lecture Topics C Brainteaser More on Procedure Call
Program Assembly.
Lecture 7 Separate compilation
MIPS instructions.
Presentation transcript:

Compiling examples in MIPS Compiling of one module The compiler’s task is to create the machine instructions in correspondence to the existing assembly language instructions.

Compiler prepares & “maps” – doesn’t run .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop [0x00400000] ?? ?? ?? ?? [0x00400004] [0x00400008] [0x0040000c] [0x00400010] [0x00400014] [0x00400018] [0x0040001c] [0x00400020] . . . Compiler’s task is not to put the machine instructions to the memory addresses but to prepare the machine instructions content supposing that later those instructions will be placed in the memory by OS starting from the address 0x00400000 and then should run. Compiler creates the map of memory. We have declared “.text” segment so our program will start from the default address 0x00400000 and the first machine instruction will be placed in that address when the program will run. Declaration of “.text 0x00500000” supposes that the program will be placed on that address to run

Some instructions are translated immediately some of them - not [0x00400000] 0x34030001 ori $3, $0, 1 [0x00400004] [0x00400008] [0x0040000c] [0x00400010] [0x00400014] [0x00400018] [0x0040001c] [0x00400020] .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop ori $3,$0,1 could be translated to the machine instruction immediately MIPS has 3 types of machine instructions format and 5 types of addressing modes Some assembly instruction types and some addressing modes immediately could be translated to the machine instructions.

What to do with the main ? Symbol table of this module [0x00400000] 0x34030001 ori $3, $0, 1 [0x00400004] [0x00400008] [0x0040000c] [0x00400010] [0x00400014] [0x00400018] [0x0040001c] [0x00400020] .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop Symbol table of this module Label Type Address main global 0x00400000 . . . All labels (instructions and data) are collected in “Symbol table” to be used later by compiler or linker to resolve the addresses of that labels. Symbol table belongs to this object module and is kept together with the object module to be used later

The next instruction So far so good sltiu $8,$2,56 [0x00400000] 0x34030001 ori $3, $0, 1 [0x00400004] 0x2c480038 sltiu $8, $2, 56 [0x00400008] [0x0040000c] [0x00400010] [0x00400014] [0x00400018] [0x0040001c] [0x00400020] .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop So far so good sltiu $8,$2,56 translated to the machine instruction without dependencies and takes the next word

Branch – unresolved address [0x00400000] 0x34030001 ori $3, $0, 1 [0x00400004] 0x2c480038 sltiu $8, $2, 56 [0x00400008] 0x1100xxxx beq $8, $0, out ??? [0x0040000c] [0x00400010] [0x00400014] [0x00400018] [0x0040001c] [0x00400020] Symbol table of this module Label Type Address main global 0x00400000 . . . .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop Temporary unresolved Symbol Table out – 0x0040 0008 beq $8,$0,out Only the first half of the instruction is known. We don’t know the address of “out” to fill the 2nd half (look at symbol table). We remember this gap in temporary symbol table (???).

No dependency is always easy to compile [0x00400000] 0x34030001 [0x00400004] 0x2c480038 [0x00400008] 0x1100xxxx beq $8, $0, out ??? [0x0040000c] 0x00000000 nop [0x00400010] 0x2c48001e sltiu $8, $2, 30 [0x00400014] [0x00400018] [0x0040001c] [0x00400020] .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop

Again Branch – unresolved address .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop [0x00400000] 0x34030001 [0x00400004] 0x2c480038 [0x00400008] 0x1100xxxx [0x0040000c] 0x00000000 [0x00400010] 0x2c48001e [0x00400014] 0x1100xxxx [0x00400018] [0x0040001c] [0x00400020] Temporary unresolved Symbol Table out – 0x0040 0008 cont- 0x0040 0014 beq $8,$0,cont We don’t know the address of “cont” to fill the 2nd half (look at symbol table). We remember this gap also in temporary symbol table. Symbol table of this module Label Type Address main global 0x00400000 . . .

First pass is finished .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop [0x00400000] 0x34030001 [0x00400004] 0x2c480038 [0x00400008] 0x1100xxxx [0x0040000c] 0x00000000 [0x00400010] 0x2c48001e [0x00400014] 0x1100xxxx [0x00400018] 0x00000000 [0x0040001c] 0x34030000 [0x00400020] 0x00000000 Now we know the addresses and can fill the gaps. Go to second pass. nop – 0s out – Should be added to the Symbol Table. cont – Should be added to the Symbol Table. Symbol table of this module Label Type Address main global 0x00400000 out local 0x0040001c cont local 0x00400020

Second pass, fill the gaps from symbol table. .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop [0x00400000] 0x34030001 [0x00400004] 0x2c480038 [0x00400008] 0x11000004 [0x0040000c] 0x00000000 [0x00400010] 0x2c48001e [0x00400014] 0x11000002 [0x00400018] 0x00000000 [0x0040001c] 0x34030000 [0x00400020] 0x00000000 +16 +8 Our program is ready to load to memory and run from 0x00400000 address Symbol table of this module (kept) Label Type Address main global 0x00400000 out local 0x0040001c cont local 0x00400020 Temporary unresolved Symbol Table out – 0x0040 0008 cont- 0x0040 0014 Can this program work if it will be loaded to the memory another address? Temporary unresolved Symbol Table eliminated out – 0x0040 0008 cont- 0x0040 0014 Yes. The addresses are relative.