331 Week 4.1 Spring 2005 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 331 Week 4.2 Spring 2005 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. .datasubsequent 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. 331 Week 4.3 Spring 2005 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 subsequent items are stored in the kernel data segment. If the optional argument addr is present, subsequent items are stored starting at addr. .ktext Subsequent items are put in the kernel text segment. If the optional argument addr is present, subsequent items are stored starting at addr. 331 Week 4.4 Spring 2005 SPIM supported MIPS directive (cont’d) .set no at It prevents SPIM from complaining about subsequent instructions that use register $at. .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 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. 331 Week 4.5 Spring 2005 Branching Far Away What if the branch destination is further away than can be captured in 16 bits? beq $s0, $s1, L1 331 Week 4.6 Spring 2005 Small constants are used quite frequently (often 50% of operands) e.g., A = A + 5; B = B + 1; C = C - 18; Solutions? Why not? z put “typical constants” in memory and load them z create hard-wired registers (like $zero) for constants Dealing with Constants Allow for MIPS instructions like addi $sp, $sp, 4slti $t0, $t1, 10andi $t0, $t0, 6ori $t0, $t0, 4 How do we make this work? 331 Week 4.7 Spring 2005 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! z I format – Immediate format z Limits immediate values to the range +215–1 to -215 Immediate Operands op rs rt 16 bit immediate I format 8 29 29 4 10 18 8 15 331 Week 4.8 Spring 2005 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 1010101010101010 0000000000000000 0000000000000000 1010101010101010 331 Week 4.9 Spring 2005 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 331 Week 4.10 Spring 2005 Addressing Modes Illustrated 1. Register addressing op rs rt rd funct Register word operand 2. Base addressing op rs rt offset 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|| 331 Week 4.11 Spring 2005 Design Principles Simplicity favors regularity z fixed size instructions – 32-bits z small number of instruction formats Smaller is faster z limited instruction set z limited number of registers in register file z limited number of addressing modes Good design demands good compromises z three instruction formats Make the common case fast z arithmetic operands from the register file (load-store machine) z allow instructions to contain immediate operands 331 Week 4.12 Spring 2005 Review: MIPS ISA, so far Category Instr Op Code Example Meaning add 0 and 32 0 and 34 add immediate 8 addi $s1, $s2, 6 $s1 = $s2 + 6 or immediate 13 ori $s1, $s2, 6 $s1 = $s2 v 6 load upper imm 15 lui $s1, 6 $s1 = 6 * 216 set on less than immediate 10 slti $s1, $s2, 6 if ($s2<6) $s1=1 else $s1=0 35 43 load byte 32 lb $s1, 25($s2) $s1 = Memory($s2+25) store byte 40 sb $s1, 25($s2) Memory($s2+25) = $s1 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 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 Uncond. Jump (J & R format) Cond. Branch (I & R format) add $s1, $s2, $s3 $s1 = $s2 + $s3 subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 load word lw $s1, 24($s2) $s1 = Memory($s2+24) store word sw $s1, 24($s2) Memory($s2+24) = $s1 Arithmetic (R & I format) Data Transfer (I format) 331 Week 4.13 Spring 2005 The Code Translation Hierarchy C program compiler assembly code assembler object code library routines executable linker loader memory machine code 331 Week 4.14 Spring 2005 Compiler Transforms the C program into an assembly language program Advantages of high-level languages z many fewer lines of code z 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 z good – smaller code size, faster execution 331 Week 4.15 Spring 2005 Assembler Transforms symbolic assembler code into object (machine) code Advantages of assembly language z Programmer has more control compared to higher level language z much easier than remembering instruction binary codes z can use labels for addresses – and let the assembler do the arithmetic z 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 z e.g., destination is no longer specified first And, when considering performance, you should count real instructions executed, not code size 331 Week 4.16 Spring 2005 Other Tasks of the Assembler Determines binary addresses corresponding to all labels z 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 z 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 331 Week 4.17 Spring 2005 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 z static data - allocated throughout the program z 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 331 Week 4.18 Spring 2005 MIPS (spim) Memory Allocation Memory 230 words 0000 0000 f f f f f f f c Your Code Reserved Static data Mem Map I/O 0040 0000 1000 0000 1000 8000 (→ 1004 0000) 7f f e f f fc Stack Dynamic data $sp $gp PC Kernel Code & Data 8000 0080 331 Week 4.19 Spring 2005 Process that produces an executable file Source file Source file Source file compiler + assembler compiler + assembler compiler + assembler object file object file object file linker program library Executable file 331 Week 4.20 Spring 2005 Linker Takes all of the independently assembled code segments and “stitches” (links) them together z 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 z 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 z branches, jumps, and data addresses to external segments 331 Week 4.21 Spring 2005 Loader object file sub: . . . object file main: jal ??? . . . jal ??? call, sub call, printf executable file main: jal sub . . . jal printf printf: . . . sub: . . . instructions Relocation records printf: . . . C library linker 331 Week 4.22 Spring 2005 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