id – add alpha to id
constant – do not relocate
else – do not relocate (including beq, bne)
P <- next program to run
$3 <- load-and-relocate (P)
beq $0, $0, repeat
– assembled file is a stream of bits
- how do we know which come
from .word (with an id!) and which are instructions?
- we cant! We need more info
from the assembler. The output of most assemblers is not pure machine
code. It is object code.
file – contains binary code AND auxiliary info needed by the loader (and
later, the linker)
object file format: MERL
- (MIPS Executable
do we need to put into our object file?
- which lines of the code
(addresses) were originally .word id?
(sanity check: is MERL)
+ MIPS + Symbol table
1 for relocation entries
in MIPS of relocatable word
Note: 0x10000002 is MIPS for beq $0, $0, 2 (the command to skip the header)
- so MERL files can be
executed as ordinary MIPS programs (if loaded at 0x00)
- want the assembler to
generate relocatable object code cs241.relasm
beq $0, $0, 2
beq $0, $0, B
- input: MERL file and
relocation address alpha
- output: non-relocatable
MIPS file with MERL header + footer removed, ready to load at alpha.
mips.array – optional 2nd argument = address at which to load the MIPS file.
myobj.merl at 0x10000
cs241.merl 0x10000 < myobj.merl > myobj.mips
mips.twopoints myobj.mips 0x10000
– typically done by the loader
of MERL file
<- read() –
of code (minus header)
<- findFreeRAM(codeLen + stack)
(i=0, i < codeLen; i+=4)
MEM[alpha + i] <- read()
<- codeLen + 12
(i < endMod)
format <- read()
if (format == 1)
to be relocated. This is relative
header, not start of the code
MEM[alpha + rel - 12] += alpha – 12
// alpha + rel – 12 = actual location
//header not included
// alpha = adjust forward by alpha
// backward by header length because header
//is not loaded
i += 8
want reloadable code, always use labels to specific jump targets.
- Potential problems
- if you use a label for
to split MIPS programs into smaller units
– how can the assembler resolve a reference to a label in a different file?
(a.asm, b.asm, c.asm)
Solution 1: Concatenate the files and then
a.asm b.asm c.asm | java cs241.binasm
- This will work – but… Why
should we have to assemble these files more than once?
Can we assemble first + then cat?
- the output needs to be
relocatable – at most one piece can reside at address 0. Therefore we
need to assemble to MERL, not just MIPS
- catting two MERL files
doesn’t yield a MERL file
Solution 2: Need a tool that understands
MERL files and puts them together intelligently. A linker.
- but still – what should
the assembler do with references to labels it can’t find?
- need to change the
- when the assembler
encounters .word id, where label id is not found, output 0, and
indicate that the value of id is needed before the program can run.