cs 35101 computer architecture spring 2008 2 9 and 2 10
play

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


  1. 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

  2. MIPS Operand Addressing Modes Summary  Register addressing – operand is in a register 1. Register addressing op rs rt rd funct Register word operand  Base (displacement) addressing – operand’s address in memory is the sum of a register and a 16-bit constant contained within the instruction 2. Base addressing op rs rt offset Memory word or byte operand base register  Immediate addressing – operand is a 16-bit constant contained within the instruction 3. Immediate addressing op rs rt operand CS 35101.2 Spring 2008

  3. 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 4. PC-relative addressing op rs rt offset Memory branch destination instruction Program Counter (PC)  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 5. Pseudo-direct addressing Memory op jump address | jump destination instruction | Program Counter (PC) CS 35101.3 Spring 2008

  4. Review: MIPS Instructions, so far Category Instr OpC Example Meaning Arithmetic add 0 & 20 add $s1, $s2, $s3 $s1 = $s2 + $s3 (R & I subtract 0 & 22 sub $s1, $s2, $s3 $s1 = $s2 - $s3 format) add immediate 8 addi $s1, $s2, 4 $s1 = $s2 + 4 shift left logical 0 & 00 sll $s1, $s2, 4 $s1 = $s2 << 4 shift right 0 & 02 srl $s1, $s2, 4 $s1 = $s2 >> 4 (fill with logical zeros) shift right 0 & 03 sra $s1, $s2, 4 $s1 = $s2 >> 4 (fill with arithmetic sign bit) and 0 & 24 and $s1, $s2, $s3 $s1 = $s2 & $s3 or 0 & 25 or $s1, $s2, $s3 $s1 = $s2 | $s3 nor 0 & 27 nor $s1, $s2, $s3 $s1 = not ($s2 | $s3) and immediate c and $s1, $s2, ff00 $s1 = $s2 & 0xff00 or immediate d or $s1, $s2, ff00 $s1 = $s2 | 0xff00 load upper f lui $s1, 0xffff $s1 = 0xffff0000 immediate CS 35101.4 Spring 2008

  5. Review: MIPS Instructions, so far Category Instr OpC Example Meaning Data load word 23 lw $s1, 100($s2) $s1 = Memory($s2+100) transfer store word 2b sw $s1, 100($s2) Memory($s2+100) = $s1 (I format) load byte 20 lb $s1, 101($s2) $s1 = Memory($s2+101) store byte 28 sb $s1, 101($s2) Memory($s2+101) = $s1 load half 21 lh $s1, 101($s2) $s1 = Memory($s2+102) store half 29 sh $s1, 101($s2) Memory($s2+102) = $s1 Cond. br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L branch br on not equal 5 bne $s1, $s2, L if ($s1 !=$s2) go to L (I & R set on less a slti $s1, $s2, if ($s2<100) $s1=1; format) than immediate 100 else $s1=0 set on less 0 & 2a slt $s1, $s2, $s3 if ($s2<$s3) $s1=1; than else $s1=0 Uncond. jump 2 j 2500 go to 10000 jump jump register 0 & 08 jr $t1 go to $t1 jump and link 3 jal 2500 go to 10000; $ra=PC+4 CS 35101.5 Spring 2008

  6. Review: MIPS R3000 ISA  Instruction Categories Registers  Load/Store  Computational R0 - R31  Jump and Branch  Floating Point - coprocessor PC  Memory Management HI  Special LO  3 Instruction Formats: all 32 bits wide 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits R format OP rs rd shamt funct rt I format rt 16 bit number OP rs J format 26 bit jump target OP CS 35101.6 Spring 2008

  7. 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.7 Spring 2008

  8. (2.10) The Code Translation Hierarchy C program compiler assembly code CS 35101.8 Spring 2008

  9. 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.9 Spring 2008

  10. The Code Translation Hierarchy C program compiler assembly code assembler object code CS 35101.10 Spring 2008

  11. CS 35101.11 Spring 2008

  12. 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.12 Spring 2008

  13. 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 opcodes, register specifiers, shift amounts, and jump/branch targets/offsets CS 35101.13 Spring 2008

  14. MIPS (spim) Memory Allocation Memory f f f f f f f c Mem Map I/O Kernel Code & Data $sp 7f f f f f fc Stack 2 30 words Dynamic data $gp 1000 8000 Static data 1000 0000 Text Segment PC 0040 0000 Reserved 0000 0000 CS 35101.14 Spring 2008

  15. 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.15 Spring 2008

  16. 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.16 Spring 2008

  17. An Example Gbl? Symbol Address .data .align 0 str 1000 0000 str: .asciiz "The answer is " cr 1000 000b cr: .asciiz "\n" yes main 0040 0000 .text .align 2 loop 0040 000c .globl main brnc 0040 001c .globl printf main: ori $2, $0, 5 0040 0024 done syscall yes ???? ???? printf move $8, $2 loop: beq $8, $9, done Relocation Info blt $8, $9, brnc Address Data/Instr sub $8, $8, $9 j loop 1000 0000 str brnc: sub $9, $9, $8 1000 000b cr j loop done: jal printf 0040 0018 j loop 0040 0020 j loop 0040 0024 jal printf CS 35101.17 Spring 2008

  18. The Code Translation Hierarchy C program compiler main text segment assembly code printf text segment assembler object code library routines linker executable machine code CS 35101.18 Spring 2008

  19. 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.19 Spring 2008

Recommend


More recommend