Assemblers and Linkers
CS 2253 Owen Kaser, UNBSJ
Assemblers and Linkers CS 2253 Owen Kaser, UNBSJ Contents Review - - PowerPoint PPT Presentation
Assemblers and Linkers CS 2253 Owen Kaser, UNBSJ Contents Review of assembler tasks A look at linker tasks Assembler implementation The location counter and symbol table Two-pass assembler Macros and conditional
CS 2253 Owen Kaser, UNBSJ
– directives, such as
– instructions
– labels
foo DCD foo ; say this is 40 bytes into AAA … 100 instructions later LDR R1,=foo
LDR R1, [PC,#-408] “relocatable”
foo ends up in memory. The assembler does not know this. It just knows that foo will be 40 bytes into its area.
AAA starts at 3000. The linker will fill in 3040 as part of its relocation.
recording something like “fixme: start of AAA + 40” for the linker.
include xstdsys.h extern_main,__initiostreams,__init_cvars,__HP global__cstart,_exit ;***************************************************************************************** ; These sections are required by the Crossware C Compiler: area __STACK,4,data,high ; Linker places this at highest available ram location space1
global __START __START * give the linker the start address dcdu __STACK ; Initialise supervisor stack for C compiler dcdu __cstart+1 ; Jump to __cstart on power up …...
– a “location counter”. (Some assemblers let you use
– an array of area information
– a symbol table, mapping labels to their addresses.
is global or external, etc.
Area ← default; $ ← 0; buffer ← empty buffer Repeat get a line of text, parse it and discard any comments if line has label L, then SymTab.put(L, Area, $) if line has directive AREA nm, type ( where nm is new) Areas.put(Area, $, buffer); Area ← nm; $ ← 0; else if line has directive DCB <someexpression> x = evaluate_constant_expression( <someexpression>, SymTab) buffer.add_byte(x); $ ← $+1; else if line has instruction ADD rX, rY, rZ x = figure out machine code(“ADD”, rX, rY, rZ); buffer.add_word( x); $ ← $+4; else if line has instruction B <someexpression> if <someexpression> is a label in SymTab whose area matches Area distance = ($+8 – SymTab.getValue( <someexpression>))/4; x = figure out machine code(“B”, distance) buffer.add_word(x); $ ← $+4 else ??? else if line has ….. Until line has directive END
B foo where foo is a label in a different source code file?
buffer.add(figure_out_machine_code(B, 0)) buffer.add(<fixme note for linker: value foo>) $ ← $ + 4 // even if fixme note is large
“fixme” places.
it will replace the offset of 0 by the actual distance.
foo add r1, r2, r3 bne foo (foo is in the SymTab already)
add r1, r2, r3 bne foo (foo may not be in the SymTab yet) add r4, r5, r6 foo add r7, r7, r7
their 8051 assembler appear similar. The 8051 is documented...(see course website for link)
….. some lines of assembly (body of macro)… endm
(On each invocation of the macro, a different label will be used)
silly macr ifc \0,R0 ← no space mov \1, \0 elsec cmp R7, #\0 beq \.0 mov \1, #\0 \.0 add R0, R0, R0 endc endm
a REPT <n> at the start of a block of lines, and ENDREPT at the end.
REPT 5 ADD R1, R2, R3 ← assembler sees this 5 times ENDREPT