1 / 32

CMPS 255 Computer Architecture

CMPS 255 Computer Architecture. Introduction to MIPS assembler, Addressing modes; Assemblers, linkers and loaders Reading assignment - 2.12, A.1-A.5. 1. Register addressing. op rs rt rd funct. Register. word operand. 2. Base addressing.

Download Presentation

CMPS 255 Computer Architecture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMPS 255Computer Architecture Introduction to MIPS assembler, Addressing modes; Assemblers, linkers and loaders Reading assignment - 2.12, A.1-A.5

  2. 1. Register addressing op rs rt rd funct Register word operand 2. Base addressing op rs rt offset Memory word or byte operand base register 3. Immediate addressing op rs rt operand 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

  3. 4. PC-relative addressing op rs rt offset Memory branch destination instruction Program Counter (PC) 5. Pseudo-direct addressing Memory op jump address || jump destination instruction Program Counter (PC) 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’saddress in memory is the 26-bit constant contained within the instruction concatenated with the upper 4 bits of the PC

  4. Review: MIPS Instructions, so far

  5. Review: MIPS Instructions, so far

  6. Review: MIPS ISA Registers • 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 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

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

  8. Compiler Assembly program: foo.s (or asm) Assembler Object(mach lang module): foo.o Linker lib.o Executable(mach lang pgm): a.out Loader Memory Steps to Starting a Program C program: foo.c

  9. compiler assembly code The Code Translation Hierarchy-Compiler C program To learn more about compiler take CMPS 274 • Transforms the C program into an assembly language program • High-level languages have many advantages such as • 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 • Note: compiler output may contain pseudo-instructions

  10. Pseudo-instructions • A common variation of assembly language instructions often treated as if it were an instruction in its own right but do not have equivalent machine language instruction • give MIPS a richer set of assembly language instructions than those implemented by the hardware. • The only cost is reserving one register, $at, for use by the assembler • Example: move $t0,$t1 # copies the contents of $t1 to $t0. • MIPS assembler accepts this instruction but converts it into the machine language equivalent of the following instruction • add $t0,$zero,$t1 # register $t0 gets 0 + register $t1 • Other examples blt, bgt, bge, and ble. blt $t0, $t1, L → slt $at, $t0, $t1bne $at, $zero, L

  11. compiler assembly code assembler object code The Code Translation Hierarchy-Assembler C program • Does a syntactic check of the code (i.e., did you type it in correctly) and then 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 • 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 (converts them to legal assembly code) • Deals with data layout directives (e.g., .asciiz) • Expands macros (frequently used sequences of instructions) • When considering performance, you should count instructions executed, not code size • Assembler produces an Object File (described later)

  12. Assembler- Object File • Assembler turns the assembly language program into an object file, • a combination of machine language instructions, data, and information needed to place instructions properly in memory • To produce the binary version of each instruction in the assembly program, the assembler must determine the addresses corresponding to all labels used in branches and data transfer instructions. • The object file typically contains six distinct 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

  13. Assembler- Object File • Relocation information: identifies instructions and data words that depend on absolute addresses (not relative to a register) when the program is loaded into memory • on MIPS only j, jal, and some loads and stores (e.g., lw $t1,100($zero) ) use absolute addresses • These references must change if portions of the program are moved in memory • Symbol table: holds the symbolic names (labels) and their corresponding addresses • local labels: used only within the file where its defined. Labels are local by default • globallabels: refers tocode or data in another file or is referenced fromanother file. Global labels must be explicitly declared global • Debugging information contains a concise description of the way in which the program was compiled, so a debugger can find which instruction addresses correspond to lines in a source file and print the data structures in readable for

  14. An Example .data .align 0 str: .asciiz "The answer is " cr: .asciiz "\n" .text .align 2 .globl main .globl printf main: ori $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

  15. Assembler Directives • allow a programmer to describe data in a more concise and natural manner than its binary representation • make assembler programs short and easier to write • tell the assembler how to translate a program but do not produce machine instructions. • .text: Subsequent items put in user text segment (machine code) • .data: Subsequent items put in user data segment (binary rep of data in source file) • .globlsym: declaressymglobal and can be referenced from other files • .asciiz str: Store the string strin memory and null-terminate it • .word w1…wn: Store the n 32-bit quantities in successive memory words • .alignn : Align the next datum on a 2nbyte boundary. • For example, .align 2 aligns the next value on a word boundary. • .align 0 turns off automatic alignment of .half, .word, .float, and.double directives until the next .data or .kdata directive

  16. Assembler Macros • provide a simple mechanism to name a frequently used sequence of instructions. • Instead of repeatedly typing the same instructions every time they are used, a programmer invokes the macro and the assembler replaces the macro call with the corresponding sequence of instructions. • likesubroutines, macros permit a programmer to create and name a new abstraction for a common operation. • Unlike subroutines, macros do not cause a subroutine call and return when the program runs since a macro call is replaced by the macro’s body when the program is assembled.

  17. Assembler Macros .data tells the assembler to store the string in the program’s data segment. .text tells the assembler to store the instructions in its text segment. • suppose that a programmer needs to print many numbers. • introduce a macro, print_int, to print an integer. print_int ($7) expands to the code print_int ($t0) expands to the code

  18. compiler assembly code assembler object code library routines linker executable The Code Translation Hierarchy-Linker • Assembler only sees one compiled program at a time, that’s why it has to make a symbol & relocation table. It’s the job of the linker to take all of the independently assembled code segments and “stitches” (links) them together C program main text segment printf text segment machine code • It is faster to recompile and reassemble a patched segment, than it is to recompile and reassemble the entire program • Changing one line would require compiling and assembling only one procedure

  19. Linker There are three steps for the linker: • Place code and data modules symbolically in memory • decide on memory allocation for code and data segments of each module • Remember, modules were assembled in isolation so eachhas assumed its code’s starting location is 0x0040 0000 and its static data starting location is 0x1000 0000 • Determine the addresses of data and instruction labels • Relocates absolute addresses to reflect the new starting location of the code segment and its data segment • Patch both the internal and external references • 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

  20. Linker Code Schematic

  21. Linker: Example slide 1 • Procedure A needs to find the : • address for the variable labeled X to put in the load instruction • address of procedure B to place in the jalinstruction.

  22. Linker: Example slide 2 • Procedure B needs to find the : • address for the variable labeled Y to put in the store instruction • address of procedure A to place in the jalinstruction.

  23. Linker: Example slide 3 • Text segment in memory starts at address 40 0000hex and data segment at 1000 0000hex. • Text of procedure A is placed at 40 0000hex and itsdata at 1000 0000hex. • In object file header for procedure A says that its text is 100hex bytes and its data is 20hex bytes, • Hence, starting address for procedure B text is40 0100hex, and its data starts at 1000 0020hex.

  24. Linker: Example slide 4 • Given so far: • text of procedure A is placed at 40 0000hex and its data at 1000 0000hex. • text of procedure B is placed at 40 0100hex and its data at 1000 0020hex. • Linker updates the address fields of the instructions: • jal in procedure A is at address 40 0004hex and gets 40 0100hex (address of procedure B) • The load is relative to a base register. Assume $gpis used as the base register. $gp is initialized to 1000 8000hex. • To get the address 1000 0000hex (the address of word X), we place 8000hex in the address field of lw at address 40 0000hex.

  25. Linker: Example slide 5 • Given so far: • text of procedure A is placed at 40 0000hex and its data at 1000 0000hex. • text of procedure B is placed at 40 0100hex and its data at 1000 0020hex. • Linker updates the address fields of the instructions: • jal in procedure B is at address 40 0104hex and gets 40 0000hex (address of procedure A) • The store is relative to a base register. Assume $gpis used as the base register. $gp is initialized to 1000 8000hex. • To get the address 1000 0020hex (the address of word Y), we place 8020hex in the address field of sw at address 40 0100hex.

  26. Linker: Example slide 6 • Recall jal uses 26 bits to create a 28-bit byte address. So it drops the right two. • Thus, the actual address in the lower 26 bits of the jal instruction is 10 0040hex, rather than 40 0100hex.

  27. Linking Two Object Files Executable File 1 + File 2 Hdr Txtseg Dseg Reloc Hdr Txtseg Dseg Reloc Smtbl Dbg Hdr Txtseg Dseg Reloc Smtbl Dbg

  28. compiler assembly code assembler object code library routines linker executable loader memory The Code Translation Hierarchy-Loader • Now that the executable fi le is on disk, the operating system reads it to memory and starts it. • loaderA system program that places an object program in main memory so that it is ready to execute. C program machine code

  29. Loader • Reads the executable file header to determine size of the text and data segments • Creates an address space large enough for the text and data. • Copies the instructions and data from the executable file into memory. • Copies the parameters (if any) to the main program onto the stack. • Initializes the machine registers(including $sp, $fp, $gp) and sets $spto the first free location (0x7fff fffc) • Jumps to startup routine • Copies arguments to $a0, … and calls main • When main returns, do exit syscall • To learn more take CMPS 272

  30. Statically vs 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 routine (see book for more details) • The subsequent calls: these steps are skipped • DLLs require extra space for dynamic linking information, but do not require the whole library to be copied or linked

  31. Dynamically Linked Libraries • The first time the library routine is called, • the program calls the dummy entry and follows the indirect jump. • It points to code that puts a number in a register to identify the desired library routine and • then jumps to the dynamic linker/loader to find the desired routine, remaps it, and changes the address in the indirect jump location to point to that routine. It then jumps to it. • When the routine completes, it returns to the original calling site. • Thereafter, the call to the library routine jumps indirectly to the routine without the extra hops

  32. MIPS Data Declaration • Data Declarations - format for declarations: name: storage_type value(s) • var1: .word 3 # create a single integer variable with initial value 3 • array1: .byte 'a','b' # create a 2-element character array with elements initialized to a and b • array2: .space 40 # Allocate 40 consecutive bytes, with storage uninitialized could be used #as a 40-element character array, or a 10-element integer array; a #comment should indicate which!

More Related