CS 35101.1 Spring 2008
CS 35101 Computer Architecture Spring 2008 2.9 and 2.10 Taken - - PowerPoint PPT Presentation
CS 35101 Computer Architecture Spring 2008 2.9 and 2.10 Taken - - PowerPoint PPT Presentation
CS 35101 Computer Architecture Spring 2008 2.9 and 2.10 Taken from Mary Jane Irwin (www.cse.psu.edu/~mji) Week 5 FA07 CSE 331 slides [ adapted from D. Patterson slides ] CS 35101.1 Spring 2008 MIPS Operand Addressing Modes Summary
CS 35101.2 Spring 2008
MIPS Operand Addressing Modes Summary
Register addressing – operand is in a register Base (displacement) addressing – operand’s
address in memory 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
- 1. Register addressing
- p rs rt rd funct
Register
word operand
- p rs rt offset
- 2. Base addressing
base register
Memory
word or byte operand
- 3. Immediate addressing
- p rs rt operand
CS 35101.3 Spring 2008
MIPS Instruction Addressing Modes Summary
PC-relative addressing – instruction’s address
in memory is the sum of the PC and a 16-bit constant contained within the instruction
Pseudo-direct addressing – instruction’s
address in memory is the 26-bit constant contained within the instruction concatenated with the upper 4 bits of the PC
- 4. PC-relative addressing
- p rs rt offset
Program Counter (PC)
Memory
branch destination instruction
- 5. Pseudo-direct addressing
- p jump address
Program Counter (PC)
Memory
jump destination instruction | |
CS 35101.4 Spring 2008
Review: MIPS Instructions, so far
$s1 = 0xffff0000 lui $s1, 0xffff f load upper immediate $s1 = $s2 | 0xff00
- r $s1, $s2, ff00
d
- r immediate
$s1 = $s2 & 0xff00 and $s1, $s2, ff00 c and immediate $s1 = not ($s2 | $s3) nor $s1, $s2, $s3 0 & 27 nor $s1 = $s2 | $s3
- r $s1, $s2, $s3
0 & 25
- r
$s1 = $s2 & $s3 and $s1, $s2, $s3 0 & 24 and $s1 = $s2 >> 4 (fill with sign bit) sra $s1, $s2, 4 0 & 03 shift right arithmetic $s1 = $s2 >> 4 (fill with zeros) srl $s1, $s2, 4 0 & 02 shift right logical $s1 = $s2 << 4 sll $s1, $s2, 4 0 & 00 shift left logical $s1 = $s2 + 4 addi $s1, $s2, 4 8 add immediate 0 & 22 0 & 20
OpC
$s1 = $s2 - $s3 sub $s1, $s2, $s3 subtract $s1 = $s2 + $s3 add $s1, $s2, $s3 add Arithmetic (R & I format)
Meaning Example Instr Category
CS 35101.5 Spring 2008
Review: MIPS Instructions, so far
go to 10000; $ra=PC+4 jal 2500 3 jump and link Memory($s2+102) = $s1 sh $s1, 101($s2) 29 store half $s1 = Memory($s2+102) lh $s1, 101($s2) 21 load half Memory($s2+101) = $s1 sb $s1, 101($s2) 28 store byte $s1 = Memory($s2+101) lb $s1, 101($s2) 20 load byte if ($s2<$s3) $s1=1; else $s1=0 slt $s1, $s2, $s3 0 & 2a set on less than go to $t1 jr $t1 0 & 08 jump register go to 10000 j 2500 2 jump Uncond. jump if ($s2<100) $s1=1; else $s1=0 slti $s1, $s2, 100 a set on less than immediate if ($s1 !=$s2) go to L bne $s1, $s2, L 5 br on not equal if ($s1==$s2) go to L beq $s1, $s2, L 4 br on equal Cond. branch (I & R format) 2b 23
OpC
Memory($s2+100) = $s1 sw $s1, 100($s2) store word $s1 = Memory($s2+100) lw $s1, 100($s2) load word Data transfer (I format)
Meaning Example Instr Category
CS 35101.6 Spring 2008
Review: MIPS R3000 ISA
Instruction Categories
Load/Store Computational Jump and Branch Floating Point
- coprocessor
Memory Management Special
3 Instruction Formats: all 32 bits wide
R0 - R31 PC HI LO OP rs rt rd shamt funct OP rs rt 16 bit number OP 26 bit jump target Registers
R format I format
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
J format
CS 35101.7 Spring 2008
RISC Design Principles Review
Simplicity favors regularity
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
CS 35101.8 Spring 2008
(2.10) The Code Translation Hierarchy
C program compiler assembly code
CS 35101.9 Spring 2008
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
smaller code size, faster execution
CS 35101.10 Spring 2008
The Code Translation Hierarchy
C program compiler assembly code assembler
- bject code
CS 35101.11 Spring 2008
CS 35101.12 Spring 2008
Assembler Does a syntactic check of the code (i.e., did you type it in correctly) and then transforms the symbolic assembler code into object (machine) code
Advantages of assembler
much easier than remembering instr’s 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”)
When considering performance, you should count
instructions executed, not code size
CS 35101.13 Spring 2008
The Two Main Tasks of the Assembler
Builds a symbol table which holds the
symbolic names (labels) and their corresponding addresses
A label is local if it is used only within the file where its defined. Labels are local by default.
A label is external (global) if it refers to code or data in another file or if it is referenced from another file. Global labels must be explicitly declared global (e.g., .globl main)
Translates each assembly language
statement into object (machine) code by “assembling” the numeric equivalents of the
- pcodes, register specifiers, shift amounts,
and jump/branch targets/offsets
CS 35101.14 Spring 2008
MIPS (spim) Memory Allocation
Memory
230 words 0000 0000 f f f f f f f c
Text Segment Reserved Static data Mem Map I/O
0040 0000 1000 0000 1000 8000 7f f f f f fc
Stack Dynamic data $sp $gp PC Kernel Code & Data
CS 35101.15 Spring 2008
Other Tasks of the Assembler
Converts pseudo-instr’s 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 lui followed by an ori
Converts numbers specified in decimal and
hexidecimal into their binary equivalents and characters into their ASCII equivalents
Deals with data layout directives (e.g., .asciiz) Expands macros (frequently used sequences of
instructions)
CS 35101.16 Spring 2008
Typical Object File Pieces
Object file header: size and position of the following
pieces of the file
Text (code) segment (.text) : assembled object
(machine) code
Data segment (.data) : data accompanying the code
static data - allocated throughout the program dynamic data - grows and shrinks as needed
Relocation information: identifies instructions (data)
that use (are located at) absolute addresses – not relative to a register (including the PC)
on MIPS only j, jal, and some loads and stores (e.g.,
lw $t1, 100($zero) ) use absolute addresses
Symbol table: global labels with their addresses (if
defined in this code segment) or without (if defined external to this code segment)
Debugging information
CS 35101.17 Spring 2008
An Example
.data .align 0 str: .asciiz "The answer is " cr: .asciiz "\n" .text .align 2 .globl main .globl printf main:
- ri
$2, $0, 5 syscall move $8, $2 loop: beq $8, $9, done blt $8, $9, brnc sub $8, $8, $9 j loop brnc: sub $9, $9, $8 j loop done: jal printf
???? ???? printf yes yes
Gbl?
0040 0024 done 0040 001c brnc 0040 000c loop 0040 0000 main 1000 000b cr 1000 0000 str
Address Symbol
jal printf 0040 0024 str 1000 0000 cr 1000 000b
Relocation Info
j loop 0040 0020 j loop 0040 0018
Data/Instr Address
CS 35101.18 Spring 2008
The Code Translation Hierarchy
C program compiler assembly code assembler
- bject code
library routines executable linker machine code
main text segment printf text segment
CS 35101.19 Spring 2008
Linker
Takes all of the independently assembled code segments and “stitches” (links) them together
Faster to recompile and reassemble a patched segment, than it is to recompile and reassemble the entire program
Decides on memory allocation pattern for the code and
data segments of each module
Remember, modules were assembled in isolation so each has assumed its code’s starting location is 0x0040 0000 and its static data starting location is 0x1000 0000
Relocates absolute addresses to reflect the new starting
location of the code segment and its data segment
Uses the symbol tables information to resolve all
remaining undefined labels
branches, jumps, and data addresses to/in external modules
Linker produces an executable file
CS 35101.20 Spring 2008
Linker Code Schematic
printf: . . . main: . . . jal ???? call, printf
Linker
Object file C library Relocation info
main: . . . jal printf printf: . . .
Executable file
CS 35101.21 Spring 2008
Linking Two Object Files
Hdr Txtseg Dseg Reloc Smtbl Dbg
File 1
Hdr Txtseg Dseg Reloc Smtbl Dbg
File 2 + Executable
Hdr Txtseg Dseg Reloc
CS 35101.22 Spring 2008
The Code Translation Hierarchy
C program compiler assembly code assembler
- bject code
library routines executable linker loader memory machine code
CS 35101.23 Spring 2008
Loader
Loads (copies) the executable code now stored
- n disk into memory at the starting address
specified by the operating system
Copies the parameters (if any) to the main
routine onto the stack
Initializes the machine registers and sets the
stack pointer to the first free location (0x7fff fffc)
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
CS 35101.24 Spring 2008
Dynamically Linked Libraries
Statically linking libraries mean that the library
becomes part of the executable code
It loads the whole library even if only a small part is
used (e.g., standard C library is 2.5 MB)
What if a new version of the library is released ?
(Lazy) dynamically linked libraries (DLL) –
library routines are not linked and loaded until a routine is called during execution
The first time the library routine called, a dynamic
linker-loader must
- find the desired routine, remap it, and “link” it to the calling