CS241 Notes
CS241 Notes
The handout should NOT be considered a substitute for the lecture. Dissemination of this handout (including posting on a website) is explicitly prohibited unless permission is obtained. Please report any errors to [email protected].
Loader handout
The following steps create a MERL le (in ASSEMBLY) that can then be assembled using an assembler that does not know of the MERL le format e.g. cs241.binasm.
Assembly
.word endModule .word endCode ......... reloc1: .word A ......... reloc2: .word B ............ endCode: .word 1 .word reloc1 .word 1 .word reloc2 endModule:
Add Header
reloc1: .word A ......... reloc2: .word B ............ endCode: .word 1 .word reloc1 .word 1 .word reloc2 endModule:
reloc1: .word A ......... reloc2: .word B ............ endCode: .word 1 .word reloc1 .word 1 .word reloc2
The 1 here is a FORMAT CODE for relocation The next line gives the location to be relocated
The rst instruction in the header results in a MERL le to act as a normal executable MIPS program. If a relocator is not being used, the rst instruction simply causes the program to jump the next two words in memory and hence the program can run without any relocation with starting memory address 0 If a relocator is used, the relocator knows where to nd the information.
The following example illustrates the MERL le in assembly, the memory addresses for each line when loaded at starting address 0, and the actual 32-bit binary for each line in the assembly le
; .merl VERSION OF RELOCATION EXAMPLE ; MERL = MIPS executable relocatable linkable ; address beq $0, $0, 2 ; skip over header ; 0x00000000 .word endmodule ; 0x00000004 .word endcode ; 0x00000008 lis $3 .word 0xabc lis $1 reloc1: .word A jr $1 B: jr $31 A: beq $0, $0, B reloc2: .word B endcode: .word 1 ; relocate .word reloc1 ; location .word 1 ; relocate .word reloc2 ; location endmodule: ; ; ; ; ; 0x0000000c 0x00000010 0x00000014 0x00000018 0x0000001c machine l. 0x10000002 0x0000003c 0x0000002c 0x00001814 0x00000abc 0x00000814 0x00000024 0x00200008 0x03e00008 0x1000fffe 0x00000020 0x00000001 0x00000018 0x00000001 0x00000028
The last column above is the input to a RELOCATABLE LOADER i.e. a loader which knows how to read the MERL format to relocate the code at any starting address . The relocating loader rst loads the program at starting address as shown in the diagram on the following page: (Note: in the diagram below the instruction beq is shown for clarity. This location will in fact contain the 32-bit binary for the beq instruction from the header. )
Relocated Address
Original Address
-------------------------! beq $0, $0, 2 0 ! + 4 endModule 4 ! + 8 endCode 8 -------------------------CODE ! + endCode -------------------------TABLE ! + endModule -------------------------- endModule endCode
The relocator runs the following code to relocate each relocation entry:
i + MEM[+8]
end + MEM[+4]
The third word in the original code contained the address of the endCode label. Since the program is now loaded at , the 3rd word is +8. Therefore we read the memory at this location This gives us the value of endCode in the original code Since the program is loaded at we add to this AGAIN!! The 2nd word in memory contains the address of the end of le So we load it through MEM[+4] The value at that address was based on addresses starting at 0, so we add This loop takes us through the footer table containing relocation entries Remember that each entry is 2 words. The rst word has value ONE to indicate a relocation entry Since MEM[i] is the .word 1, MEM[i+4] is the address that contained a .word label. Since we started at , the .word label is actually at + MEM[i+4] We READ in that value MEM[ + MEM[i+4]] and this represents what .word label would have had we started at 0. Add to this, and STORE IT BACK Jump to next table entry
Input: merl le and relocation address Output: NON-relocatable mips le (MERL HEADER AND FOOTER HAS BEEN REMOVED ready to load at given address NOTE: mips.twoint, mips.address CAN take a second argument: an address at which to load a mips le Relocatable Assembler: cs241.relasm A tool which knows what to write in the header and table to create a relocatable merl le
Syntactic Categories
E {expr, term, factor, lvalue} T {test} S {statement, statements} {int, int}
NUM : int
NULL : int E: (E ) :
[Pointers]
E : int new int [E ] : int E1 : int E2 : int E1 + E2 : int E1 : int E2 : int E1 E2 : int E1 : int E2 : int E1 % E2 : int
[Addition]
[Subtraction]
E1 : E 2 : well-typed(E1 > E2 )
E1 : E 2 : well-typed(E1 >= E2 )
[Printing]
E : int well-typed(println E ; ) E1 : E 2 : well-typed(E1 = E2 ; ) well-typed(S1 ) well-typed(S2 ) well-typed(S1 S2 ) well-typed(dcls ) well-typed(dcls int* id = NULL; )
[Assignment]
[Sequencing]
well-typed()
[Declns]
well-typed() [Procedure]
decl 2 : int well-typed(decls ) well-typed(S ) E : int well-typed(int wain(decl 1 , decl 2 ) {decls S return E ; })