It assumes that the output of the assembler is an object file.
.data # line 1
foo: .word 10 # line 2
.text # line 3
.globl bar # line 4
bar: la $t0, foo # line 5
lw $t1, ($t0) # line 6
add $v0, $zero, $zero # line 7
loop: beq $t1, $zero, quit # line 8
add $v0, $v0, $a0 # line 9
addi $t1, $t1, -1 # line 10
j loop # line 11
quit: jr $ra # line 12
.data # line 13
.globl quux # line 14
quux: .word 42 # line 15
.data
This tells the assembler that the upcoming line(s) will describe
data segment contents.
No updates to the data structures are needed.
Line 2 is
foo: .word 10This causes the assember to add entries to its list of symbols and to the data segment, as follows:
Assembler data structures after processing Line 2 ...
TEXT SEGMENT DATA SEGMENT SYMBOLS
offset word offset word name address global
0 0 0x0000_000a foo data+0 no
4
RECORD OF INCOMPLETE INFORMATION
address what needs to be done at that address
Line 3 is
.text
This tells the assembler that the upcoming line(s) will describe
text segment contents.
No updates to the data structures are needed.
Line 4 is
.globl bar
A new entry is needed in the list of symbols.
All that is known so far about foo is that it is global.
Assembler data structures after processing Line 4 ...
TEXT SEGMENT DATA SEGMENT SYMBOLS
offset word offset word name address global
0 0 0x0000_000a foo data+0 no
4 bar yes
RECORD OF INCOMPLETE INFORMATION
address what needs to be done at that address
Line 5 is
bar: la $t0, fooNow the assembler can fill in an address for foo in the list of symbols. la is a pseudoinstruction that needs two real instructions, one to set up bits 31-16 of $t0 and another to set up bits 15-0 of $t0. These two instructions get added to the text segment. But it isn't known what the address of foo will be in an executable, so the bits 15-0 of each of the two instructions are left as zero and notes are placed in the incomplete information data structure.
Assembler data structures after processing Line 5 ... TEXT SEGMENT DATA SEGMENT SYMBOLS offset word offset word name address global 0 0x3c01_0000 0 0x0000_000a foo data+0 no 4 0x3428_0000 4 bar text+0 yes 8 RECORD OF INCOMPLETE INFORMATION address what needs to be done at that address text+0 replace bits 15-0 of insn with bits 31-16 of addr of foo text+4 replace bits 15-0 of insn with bits 15-0 of addr of foo
Lines 6 and 7 are
lw $t1, ($t0)
add $v0, $zero, $zero
The assembler can completely translate these two instructions.
Assembler data structures after processing Lines 6 and 7 ... TEXT SEGMENT DATA SEGMENT SYMBOLS offset word offset word name address global 0 0x3c01_0000 0 0x0000_000a foo data+0 no 4 0x3428_0000 4 bar text+0 yes 8 0x8d09_0000 12 0x0000_1020 16 RECORD OF INCOMPLETE INFORMATION address what needs to be done at that address text+0 replace bits 15-0 of insn with bits 31-16 of addr of foo text+4 replace bits 15-0 of insn with bits 15-0 of addr of foo
loop: beq $t1, $zero, quitThe assembler adds loop and quit to the list of symbols. The instruction can't be completely translated because the assembler doesn't yet know where quit is. The assembler leaves bits 15-0 of the instruction as zero and puts a note in the incomplete information section.
Assembler data structures after processing Line 8 ... TEXT SEGMENT DATA SEGMENT SYMBOLS offset word offset word name address global 0 0x3c01_0000 0 0x0000_000a foo data+0 no 4 0x3428_0000 4 bar text+0 yes 8 0x8d09_0000 loop text+16 no 12 0x0000_1020 quit no 16 0x1120_0000 20 RECORD OF INCOMPLETE INFORMATION address what needs to be done at that address text+0 replace bits 15-0 of insn with bits 31-16 of addr of foo text+4 replace bits 15-0 of insn with bits 15-0 of addr of foo text+16 replace bits 15-0 of insn with offset from text+20 to quit
Lines 9 and 10 are
add $v0, $v0, $a0
addi $t1, $t1, -1
The assembler can completely translate these two instructions.
Assembler data structures after processing Lines 9 and 10 ... TEXT SEGMENT DATA SEGMENT SYMBOLS offset word offset word name address global 0 0x3c01_0000 0 0x0000_000a foo data+0 no 4 0x3428_0000 4 bar text+0 yes 8 0x8d09_0000 loop text+16 no 12 0x0000_1020 quit no 16 0x1120_0000 20 0x0044_1020 24 0x2129_ffff 28 RECORD OF INCOMPLETE INFORMATION address what needs to be done at that address text+0 replace bits 15-0 of insn with bits 31-16 of addr of foo text+4 replace bits 15-0 of insn with bits 15-0 of addr of foo text+16 replace bits 15-0 of insn with offset from insn to quit
j loop
This instruction can't be completely translated because the address
of loop isn't known.
So a note has to be placed
in the incomplete information data structure.
Assembler data structures after processing Line 11 ... TEXT SEGMENT DATA SEGMENT SYMBOLS offset word offset word name address global 0 0x3c01_0000 0 0x0000_000a foo data+0 no 4 0x3428_0000 4 bar text+0 yes 8 0x8d09_0000 loop text+16 no 12 0x0000_1020 quit no 16 0x1120_0000 20 0x0044_1020 24 0x2129_ffff 28 0x0800_0000 32 RECORD OF INCOMPLETE INFORMATION address what needs to be done at that address text+0 replace bits 15-0 of insn with bits 31-16 of addr of foo text+4 replace bits 15-0 of insn with bits 15-0 of addr of foo text+16 replace bits 15-0 of insn with offset from insn to quit text+28 replace bits 25-0 of insn with bits 27-2 of addr of loop
quit: jr $ra
The instruction can be translated completely.
Because the line has a label, there is an update to
the list of symbols.
Assembler data structures after processing Line 12 ... TEXT SEGMENT DATA SEGMENT SYMBOLS offset word offset word name address global 0 0x3c01_0000 0 0x0000_000a foo data+0 no 4 0x3428_0000 4 bar text+0 yes 8 0x8d09_0000 loop text+16 no 12 0x0000_1020 quit text+32 no 16 0x1120_0000 20 0x0044_1020 24 0x2129_ffff 28 0x0800_0000 32 0x03e0_0008 36 RECORD OF INCOMPLETE INFORMATION address what needs to be done at that address text+0 replace bits 15-0 of insn with bits 31-16 of addr of foo text+4 replace bits 15-0 of insn with bits 15-0 of addr of foo text+16 replace bits 15-0 of insn with offset from insn to quit text+28 replace bits 25-0 of insn with bits 27-2 of addr of loop
.data
This tells the assembler that the upcoming line(s)
will describe data segment contents. No updates to the
data structures are needed.
.globl quux
The assembler will update the table of symbols.
Assembler data structures after processing Line 14 ... TEXT SEGMENT DATA SEGMENT SYMBOLS offset word offset word name address global 0 0x3c01_0000 0 0x0000_000a foo data+0 no 4 0x3428_0000 4 bar text+0 yes 8 0x8d09_0000 loop text+16 no 12 0x0000_1020 quit text+32 no 16 0x1120_0000 quux yes 20 0x0044_1020 24 0x2129_ffff 28 0x0800_0000 32 0x03e0_0008 36 RECORD OF INCOMPLETE INFORMATION address what needs to be done at that address text+0 replace bits 15-0 of insn with bits 31-16 of addr of foo text+4 replace bits 15-0 of insn with bits 15-0 of addr of foo text+16 replace bits 15-0 of insn with offset from insn to quit text+28 replace bits 25-0 of insn with bits 27-2 of addr of loop
quux: .word 42
Assembler data structures after processing Line 15 ... TEXT SEGMENT DATA SEGMENT SYMBOLS offset word offset word name address global 0 0x3c01_0000 0 0x0000_000a foo data+0 no 4 0x3428_0000 4 0x0000_002a bar text+0 yes 8 0x8d09_0000 8 loop text+16 no 12 0x0000_1020 quit text+32 no 16 0x1120_0000 quux data+4 yes 20 0x0044_1020 24 0x2129_ffff 28 0x0800_0000 32 0x03e0_0008 36 RECORD OF INCOMPLETE INFORMATION address what needs to be done at that address text+0 replace bits 15-0 of insn with bits 31-16 of addr of foo text+4 replace bits 15-0 of insn with bits 15-0 of addr of foo text+16 replace bits 15-0 of insn with offset from insn to quit text+28 replace bits 25-0 of insn with bits 27-2 of addr of loop
Final state of sssembler data structures TEXT SEGMENT DATA SEGMENT SYMBOLS offset word offset word name address global 0 0x3c01_0000 0 0x0000_000a foo data+0 no 4 0x3428_0000 4 0x0000_002a bar text+0 yes 8 0x8d09_0000 8 loop text+16 no 12 0x0000_1020 quit text+32 no 16 0x1120_0003 quux data+4 yes 20 0x0044_1020 24 0x2129_ffff 28 0x0800_0000 32 0x03e0_0008 36 RECORD OF INCOMPLETE INFORMATION address what needs to be done at that address text+0 replace bits 15-0 of insn with bits 31-16 of addr of foo text+4 replace bits 15-0 of insn with bits 15-0 of addr of foo text+28 replace bits 25-0 of insn with bits 27-2 of addr of loop
TEXT SEGMENT (36 bytes) 0x3c01_0000 0x3428_0000 0x8d09_0000 0x0000_1020 0x1120_0003 0x0044_1020 0x2129_ffff 0x0800_0000 0x03e0_0008 DATA SEGMENT (8 bytes) 0x0000_000a 0x0000_002a RELOCATION INFORMATION replace bits 15-0 of word at text+0 with bits 31-16 of addr of data+0 replace bits 15-0 of word at text+4 with bits 15-0 of addr of data+0 replace bits 25-0 of word at text+28 with bits 27-2 of addr of text+16 SYMBOL TABLE bar text+0 quux data+4