assembly language source assembler code "machine code" 1 These slides may be freely used, distributed, and incorporated into other works.
To execute a program: 1 put the "machine code" into memory 2 jal __start (the OS does this) memory "machine code" 2 These slides may be freely used, distributed, and incorporated into other works.
assembler's task assign addresses 1. generate "machine code" 2. (architecture dependent) 3. further translation of assembly language source code 3 These slides may be freely used, distributed, and incorporated into other works.
previous architectures 1 assembly 1 machine language code instruction instruction MIPS architecture 1 assembly 1 or more language machine code instruction instructions 4 These slides may be freely used, distributed, and incorporated into other works.
This further translation is also called synthesis. MIPS example of synthesis: add $8, $9, -16 becomes addi $8, $9, -16 5 These slides may be freely used, distributed, and incorporated into other works.
Two operands in the source code add $8, $9 are expanded back out to become add $8, $8, $9 6 These slides may be freely used, distributed, and incorporated into other works.
integer multiplication and division each produce 2 32-bit results integer division produces quotient remainder integer multiplication of 2 32-bit operands produces a 64-bit result 7 These slides may be freely used, distributed, and incorporated into other works.
MIPS hardware implements 2 extra registers (called HI and LO) to hold these results. Here are 4 more MIPS instructions: mflo R mtlo R mfhi R mthi R m move lo register LO f from hi register HI t to 8 These slides may be freely used, distributed, and incorporated into other works.
multiplication mul $8, $9, $10 becomes mult $9, $10 mflo $8 X HI LO 9 These slides may be freely used, distributed, and incorporated into other works.
division div $8, $9, $10 becomes div $9, $10 mflo $8 # quotient in LO rem $12, $13, $14 becomes div $13, $14 mfhi $12 # remainder in HI 10 These slides may be freely used, distributed, and incorporated into other works.
puts , putc , getc , and done are not TAL ! I/O is accomplished by requesting service from the operating system (OS). All architectures do this with a single instruction. On MIPS, this instruction is syscall (no operands) 11 These slides may be freely used, distributed, and incorporated into other works.
(note that this is specific to our simulator) To help the OS distinguish what service is required, $v0 ( $2 ) is set: $v0 I/O operation 11 putc 12 getc 4 puts 10 done 12 These slides may be freely used, distributed, and incorporated into other works.
synthesis of puts $8 first pass final synthesis li $2, 4 addi $2, $0, 4 move $4, $8 add $4, $8, $0 syscall syscall 13 These slides may be freely used, distributed, and incorporated into other works.
lw $8, X becomes la $8, X lw $8, 0($8) Oops! la must be synthesized. 14 These slides may be freely used, distributed, and incorporated into other works.
synthesis of la $8, my_label requires the address assigned for my_label every address is assigned by the assembler MS part LS part 16 16 32 15 These slides may be freely used, distributed, and incorporated into other works.
la $8, my_label becomes lui $8, 0xMS part ori $8, $8, 0xLS part 16 These slides may be freely used, distributed, and incorporated into other works.
after lui $8, 0xMS part $8 MS part 000 . . . 0 this is then logically ORed with LS part 000 . . . 0 due to the instruction ori $8, $8, 0xLS part resulting contents of $8 : $8 MS part LS part 17 These slides may be freely used, distributed, and incorporated into other works.
Synthesize lw $8, X Assume X is assigned address 0xaaee0018. first try: la $8, X lw $8, 0($8) with synthesis of the la instruction: lui $8, 0xaaee ori $8, $8, 0x0018 lw $8, 0($8) 18 These slides may be freely used, distributed, and incorporated into other works.
Synthesize sb $12, X Assume X is assigned address 0x080001a0. 19 These slides may be freely used, distributed, and incorporated into other works.
Generate machine code for addi $8, $20, 15 From the TAL table: addi R t , R s , I R t is $8 R s is $20 I is 0000 0000 0000 1111 0010 00ss ssst tttt ii .. ii op code 16 bits sssss is 10100 (for $20 ) ttttt is 01000 (for $8 ) 0010 0010 1000 1000 0000 0000 0000 1111 in hex 0x2288000f 20 These slides may be freely used, distributed, and incorporated into other works.
Generate machine code for lw $8, 12($sp) lw R t , I(R b ) R t is $8 R b is $sp (which is $29) I is 12 1000 11bb bbbt tttt ii .. ii op code 16 bbbbb is 11101 value 12 ttttt is 01000 1000 1111 1010 1000 0000 0000 0000 1100 in hex 0x8fa8000c 21 These slides may be freely used, distributed, and incorporated into other works.
assembly language assembler source assign addresses code produce machine code memory image 22 These slides may be freely used, distributed, and incorporated into other works.
Problem: forward references .text beq $8, $11, later_in_code later_in_code: lw $20, X .data X: .word 16 23 These slides may be freely used, distributed, and incorporated into other works.
Simple solution: 2-pass assembler first pass: (MIPS-only) MAL TAL synthesis assign all addresses second pass: produce all machine code More complex and more efficient: 1-pass assembler Keep a list of instructions that cannot be completed due to yet-to-be-assigned addresses. As addresses are assigned, check the list and complete instructions. 24 These slides may be freely used, distributed, and incorporated into other works.
assign all addresses (and remember them) implies the use of a table holding the mapping of addresses to labels called a symbol table 25 These slides may be freely used, distributed, and incorporated into other works.
As the assembler works on the source code, it scans the characters in the file. Scanner (a SW module) breaks a set of characters into significant groups known as tokens often, tokens are separated by white space or special punctuation .data a1: .word 3 loop: lw $7, 4($6) 26 These slides may be freely used, distributed, and incorporated into other works.
.data a1: .word 3 a2: .word 16:4 a3: .word 5 .text __start: la $6, a2 loop: lw $7, 4($6) mult $9, $10 b loop done 27 These slides may be freely used, distributed, and incorporated into other works.
2 segments: code and data The assembler places items into these 2 segments. So, it needs addresses. Use starting addresses of data 0x0040 0000 code 0x0080 0000 The variable internal to the assembler that represents the next address to be assigned is the location counter. 28 These slides may be freely used, distributed, and incorporated into other works.
TAL equivalent of code: .text __start: lui $6, 0x0040 # la $6, a2 ori $6, $6, 0x0004 loop: lw $7, 4($6) mult $9, $10 beq $0, $0, loop # b loop ori $2, $0, 10 # done syscall 29 These slides may be freely used, distributed, and incorporated into other works.
As a result of processing the entire .data section, the memory image will be address contents 0x0040 0000 0x0000 0003 0x0040 0004 0x0000 0010 0x0040 0008 0x0000 0010 0x0040 000c 0x0000 0010 0x0040 0010 0x0000 0010 0x0040 0014 0x0000 0005 30 These slides may be freely used, distributed, and incorporated into other works.
.data a1: .word 3 a2: .word 16:4 a3: .word 5 .text __start: la $6, a2 loop: lw $7, 4($6) mult $9, $10 b loop done 31 These slides may be freely used, distributed, and incorporated into other works.
Machine code for la $6, a2 Synthesized: (1) lui $6, 0x0040 (address from symbol table) (2) ori $6, $6, 0x0004 (1) lui R t , I R t is $6 0011 1100 000t tttt ii .. ii op code 16 ttttt is 00110 0011 1100 0000 0110 0000 0000 0100 0000 in hex 0x3c060040 32 These slides may be freely used, distributed, and incorporated into other works.
Add to the memory image address contents 0x0080 0000 0x3c06 0040 33 These slides may be freely used, distributed, and incorporated into other works.
Machine code for ori $6, $6, 0x0004 (2) ori R t , R s , I R t is $6 R s is $6 0011 01ss ssst tttt ii .. ii op code 16 ttttt is 00110 sssss is 00110 0011 0100 1100 0110 0000 0000 0000 0100 in hex 0x34c60004 34 These slides may be freely used, distributed, and incorporated into other works.
Add it to the memory image as well, updating the location counter address contents 0x0080 0000 0x3c06 0040 0x0080 0004 0x34c6 0004 35 These slides may be freely used, distributed, and incorporated into other works.
.data a1: .word 3 a2: .word 16:4 a3: .word 5 .text __start: la $6, a2 loop: lw $7, 4($6) mult $9, $10 b loop done 36 These slides may be freely used, distributed, and incorporated into other works.
Recommend
More recommend