14:332:331 Computer Architecture and Assembly Language Spring 06 Week 4: Addressing Mode, Assembler, Linker [Adapted from Dave Patterson’s UCB CS152 slides.

Slides:



Advertisements
Similar presentations
Henk Corporaal TUEindhoven 2011
Advertisements

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.
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 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
CS3350B Computer Architecture Winter 2015 Lecture 4
Chapter 2 Instructions: Language of the Computer
Wannabe Lecturer Alexandre Joly inst.eecs.berkeley.edu/~cs61c-te
1 Computer Architecture MIPS Simulator and Assembly language.
1 COMS 361 Computer Organization Title: Instructions Date: 9/28/2004 Lecture Number: 10.
331 W08.1Spring :332:331 Computer Architecture and Assembly Language Spring 2006 Week 8: Datapath Design [Adapted from Dave Patterson’s UCB CS152.
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.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
1 Instructions: Language of the Machine More primitive than higher level languages e.g., no sophisticated control flow Very restrictive e.g., MIPS Arithmetic.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
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.
331 Practice Exam.1Fall 2003 Naming Conventions for Registers 0$zero constant 0 (Hdware) 1$atreserved for assembler 2$v0expression evaluation & 3$v1function.
The MIPS 32 Our objective is to get an appreciation with: working with a “typical” computer machine language. programming in assembly language. debuging.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
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:
Mohamed Younis CMCS 411, Computer Architecture 1 CMCS Computer Architecture Lecture 5 Addressing Mode & Architectural Design Guidelines February.
CS35101 Computer Architecture Spring 2006 Week 5 Paul Durand ( Course url:
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
1  1998 Morgan Kaufmann Publishers Machine Instructions: Language of the Machine Lowest level of programming, control directly the hardware Assembly instructions.
CSIE30300 Computer Architecture Unit 02: MIPS ISA Review Hsin-Chou Chi [Adapted from material by and
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Computer Architecture (CS 207 D) Instruction Set Architecture ISA.
Small constants are used quite frequently (50% of operands) e.g., A = A + 5; B = B + 1; C = C - 18; Solutions? Why not? put 'typical constants' in memory.
CMPE 325 Computer Architecture II
 1998 Morgan Kaufmann Publishers MIPS arithmetic All instructions have 3 operands Operand order is fixed (destination first) Example: C code: A = B +
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
Csci 136 Computer Architecture II – More on MIPS ISA Xiuzhen Cheng
EE472 – Spring 2007P. Chiang, with Slide Help from C. Kozyrakis (Stanford) ECE472 Computer Architecture Lecture #3—Oct. 2, 2007 Patrick Chiang TA: Kang-Min.
The Assembly Process Computer Organization and Assembly Language: Module 10.
Instruction Set Architecture Chapter 3 – P & H. Introduction Instruction set architecture interface between programmer and CPU Good ISA makes program.
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.
Computer Architecture & Operations I
Computer Architecture Instruction Set Architecture
MIPS Instruction Set Advantages
Computer Architecture Instruction Set Architecture
CSE 331 Computer Organization and Design Fall 2007 Week 5
MIPS assembly syntax Comments
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Henk Corporaal TUEindhoven 2010
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
Instruction encoding The ISA defines Format = Encoding
Computer Architecture
COMS 361 Computer Organization
Computer Architecture
10/6: Lecture Topics C Brainteaser More on Procedure Call
Program Assembly.
Presentation transcript:

14:332:331 Computer Architecture and Assembly Language Spring 06 Week 4: Addressing Mode, Assembler, Linker [Adapted from Dave Patterson’s UCB CS152 slides and Mary Jane Irwin’s PSU CSE331 slides]

MIPS (SPIM) Assembler Syntax Comments begin with #. Everything from # to the end of the line is ignored. Identifiers are a sequence of alphanumeric characters, underbars (_), and dots (.) that do not begin with a number. Labels are declared by putting them at the beginning of a line followed by a colon. .data item: .word 1 .text .global main # Must be global main: lw $t0, item

SPIM supported MIPS directive .align n align the next datum on a 2n byte boundary. .ascii str store the string str in mem, but do not null-terminate it. .asciiz str store the string str in mem, but null- terminate it. .byte b1, …, bn store the n values in successive bytes of memory. .data <addr> subsequent items are stored in the data segment. If the optional argument addr is present, subsequent items are stored starting at address addr. .double d1,…,dn store the n floating-point double precision numbers in successive memory locations.

SPIM supported MIPS directive (cont’d) .extern sym size Declare that the datum stored at sym is size bytes large and is a global label. .float f1,…,fn store the n floating-point single precision numbers in successive memory locations. .global sym Declare that label sym is global and can be referenced from other files. .half h1, …, hn store the n 16-bit quantities in successive memory halfwords. .kdata <addr> subsequent items are stored in the kernel data segment. If the optional argument addr is present, subsequent items are stored starting at addr. .ktext <addr> Subsequent items are put in the kernel text segment. If the optional argument addr is present, subsequent items are stored starting at addr.

SPIM supported MIPS directive (cont’d) .set no at It prevents SPIM from complaining about subsequent instructions that use register $at. .space n Allocate n bytes of space in the current segment (which must be data segment in SPIM) .text <addr> Subsequent items are put in the text segment. If the optional argument addr is present, subsequent items are stored starting at addr. .word w1,…,wn store the n 32-bit quantities in successive memory words.

Branching Far Away What if the branch destination is further away than can be captured in 16 bits? beq $s0, $s1, L1

Dealing with Constants Small constants are used quite frequently (often 50% of operands) e.g., A = A + 5; B = B + 1; C = C - 18; Solutions? Why not? put “typical constants” in memory and load them create hard-wired registers (like $zero) for constants Allow for MIPS instructions like addi $sp, $sp, 4 slti $t0, $t1, 10 andi $t0, $t0, 6 ori $t0, $t0, 4 How do we make this work? in gcc 52% of arithmetic operations involve constants – in spice its 69% have he students answer why not – speed and limited number of registers notice new instructions – bit wise and and bit wise or – requiring an ALU – talk a bit about how they work

Immediate Operands MIPS immediate instructions: addi $sp, $sp, 4 #$sp = $sp + 4 slti $t0, $s2, 15 #$t0 = 1 if $s2<15 Machine format: The constant is kept inside the instruction itself! I format – Immediate format Limits immediate values to the range +215–1 to -215 I format op rs rt 16 bit immediate 8 29 29 4 much faster than if loaded from memory addi and slti does sign extend of immediate operand into the leftmost bits of the destination register (ie., copies the leftmost bit of the 16-bit immediate value into the upper 16 bits) by contrast, ori and andi loads zero’s into the upper 16 bits and so is usually used (instead of the addi) to build 32 bit constants 10 18 8 15

How About Larger Constants? We'd also like to be able to load a 32 bit constant into a register Must use two instructions, new "load upper immediate" instruction lui $t0, 1010101010101010 Then must get the lower order bits right, i.e., ori $t0, $t0, 1010101010101010 16 0 8 1010101010101010 For class handout 1010101010101010 0000000000000000 0000000000000000 1010101010101010

MIPS Addressing Modes Register addressing – operand is in a register Base (displacement) addressing – operand is at the memory location whose address is the sum of a register and a 16-bit constant contained within the instruction Immediate addressing – operand is a 16-bit constant contained within the instruction PC-relative addressing –instruction address is the sum of the PC and a 16-bit constant contained within the instruction Pseudo-direct addressing – instruction address is the 26-bit constant contained within the instruction concatenated with the upper 4 bits of the PC

Addressing Modes Illustrated 1. Register addressing op rs rt rd funct Register word operand op rs rt offset 2. Base addressing base register Memory word or byte operand 3. Immediate addressing op rs rt operand 4. PC-relative addressing op rs rt offset Program Counter (PC) Memory branch destination instruction 5. Pseudo-direct addressing op jump address Program Counter (PC) Memory jump destination instruction ||

Design Principles Simplicity favors regularity Smaller is faster fixed size instructions – 32-bits small number of instruction formats Smaller is faster limited instruction set limited number of registers in register file limited number of addressing modes Good design demands good compromises three instruction formats Make the common case fast arithmetic operands from the register file (load-store machine) allow instructions to contain immediate operands

Review: MIPS ISA, so far Arithmetic (R & I format) add 0 and 32 Category Instr Op Code Example Meaning Arithmetic (R & I format) add 0 and 32 add $s1, $s2, $s3 $s1 = $s2 + $s3 subtract 0 and 34 sub $s1, $s2, $s3 $s1 = $s2 - $s3 add immediate 8 addi $s1, $s2, 6 $s1 = $s2 + 6 or immediate 13 ori $s1, $s2, 6 $s1 = $s2 v 6 Data Transfer (I format) load word 35 lw $s1, 24($s2) $s1 = Memory($s2+24) store word 43 sw $s1, 24($s2) Memory($s2+24) = $s1 load byte 32 lb $s1, 25($s2) $s1 = Memory($s2+25) store byte 40 sb $s1, 25($s2) Memory($s2+25) = $s1 load upper imm 15 lui $s1, 6 $s1 = 6 * 216 Cond. Branch (I & R format) br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L br on not equal 5 bne $s1, $s2, L if ($s1 !=$s2) go to L set on less than 0 and 42 slt $s1, $s2, $s3 if ($s2<$s3) $s1=1 else $s1=0 set on less than immediate 10 slti $s1, $s2, 6 if ($s2<6) $s1=1 else Uncond. Jump (J & R format) jump 2 j 2500 go to 10000 jump register 0 and 8 jr $t1 go to $t1 jump and link 3 jal 2500 go to 10000; $ra=PC+4

The Code Translation Hierarchy C program compiler assembly code assembler object code library routines machine code executable linker Four logical phases all programs go through C code – x.c or .TXT assembly code – x.s or .ASM object code – x.o or .OBJ executable – a.out or .EXE loader memory

Compiler Transforms the C program into an assembly language program Advantages of high-level languages many fewer lines of code easier to understand and debug Today’s optimizing compilers can produce assembly code nearly as good as an assembly language programming expert and often better for large programs good – smaller code size, faster execution

Assembler Transforms symbolic assembler code into object (machine) code Advantages of assembly language Programmer has more control compared to higher level language much easier than remembering instruction binary codes can use labels for addresses – and let the assembler do the arithmetic can use pseudo-instructions e.g., “move $t0, $t1” exists only in assembler (would be implemented using “add $t0,$t1,$zero”) However, must remember that machine language is the underlying reality e.g., destination is no longer specified first And, when considering performance, you should count real instructions executed, not code size

Other Tasks of the Assembler Determines binary addresses corresponding to all labels keeps track of labels used in branches and data transfer instructions in a symbol table pairs of symbols and addresses Converts pseudo-instructions to legal assembly code register $at is reserved for the assembler to do this Converts branches to far away locations into a branch followed by a jump Converts instructions with large immediates into a load upper immediate followed by an or immediate Converts numbers specified in decimal and hexidecimal into their binary equivalents Converts characters into their ASCII equivalents

Typical Object File Pieces Object file header: size and position of following pieces Text module: assembled object (machine) code Data module: data accompanying the code static data - allocated throughout the program dynamic data - grows and shrinks as needed by the program Relocation information: identifies instructions (data) that use (are located at) absolute addresses – those that are not relative to a register (e.g., jump destination addr) – when the code and data is loaded into memory Symbol table: remaining undefined labels (e.g., external references) Debugging information Object file header text segment data segment relocation information symbol table debugging information

MIPS (spim) Memory Allocation f f f f f f f c Mem Map I/O Kernel Code & Data 8000 0080 $sp 7f f e f f fc Stack 230 words Dynamic data $gp 1000 8000 ( 1004 0000) Note that stack grows down into dynamic data area. Static data 1000 0000 Your Code PC 0040 0000 Reserved 0000 0000

Process that produces an executable file Source file object file compiler + assembler Source file object file Executable file compiler + assembler linker program library object file Source file compiler + assembler

Linker Takes all of the independently assembled code segments and “stitches” (links) them together Much faster to patch code and recompile and reassemble that patched routine, than it is to recompile and reassemble the entire program Decides on memory allocation pattern for the code and data modules of each segment remember, segments were assembled in isolation so each assumes its code’s starting location is 0x0040 0000 and its static data starting location is 0x1000 0000 Absolute addresses must be relocated to reflect the new starting location of each code and data module Uses the symbol table information to resolve all remaining undefined labels branches, jumps, and data addresses to external segments File has same format as object file output by assembler - except it contains no unresolved reference, relocation information, symbol table, or debugging information

Loader object file object file sub: . executable file main: jal ??? . call, sub call, printf main: jal sub . jal printf printf: sub: instructions linker Relocation records C library printf: .

Loader Loads (copies) the executable code now stored on disk into memory at the starting address specified by the operating system Initializes the machine registers and sets the stack pointer to the first free location (0x7ffe fffc) Copies the parameters (if any) to the main routine onto the stack Jumps to a start-up routine (at PC addr 0x0040 0000 on xspim) that copies the parameters into the argument registers and then calls the main routine of the program with a jal main