140 likes | 274 Views
What Does an Assembler Do ?. main: Loop: sw $0, DEFN($0) jal getline la $s0, inBUF lb $t0, ($s0) lb $t1, DOLLAR($0) beq $t0, $t1, QUIT lb $t1, L($0) bne $t0, $t1, get_tok
E N D
What Does an Assembler Do ? main: Loop: sw $0, DEFN($0) jal getline la $s0, inBUF lb $t0, ($s0) lb $t1, DOLLAR($0) beq $t0, $t1, QUIT lb $t1, L($0) bne $t0, $t1, get_tok li $s0, 1 sw $s0, DEFN($0) get_tok:
What Does an Assembler Do ? main: loop: sw $0, DEFN($0) jal getline la $s0, inBUF lb $t0, ($s0) lb $t1, DOLLAR($0) beq $t0, $t1, QUIT lb $t1, L($0) bne $t0, $t1, get_tok li $s0, 1 sw $s0, DEFN($0) get_tok: Read Each Input Line For each input line:
What Does an Assembler Do ? main: loop: sw $0, DEFN($0) jal getline la $s0, inBUF lb $t0, ($s0) lb $t1, DOLLAR($0) beq $t0, $t1, QUIT lb $t1, L($0) bne $t0, $t1, get_tok li $s0, 1 sw $s0, DEFN($0) get_tok: Read Each Input Line For each input line: SAVE Labels and LOCs
What Does an Assembler Do ? main: loop: sw $0, DEFN($0) jal getline la $s0, inBUF lb $t0, ($s0) lb $t1, DOLLAR($0) beq $t0, $t1, QUIT lb $t1, L($0) bne $t0, $t1, get_tok li $s0, 1 sw $s0, DEFN($0) get_tok: Read Each Input Line For each input line: SAVE Labels and LOCs MAP op to machine code
What Does an Assembler Do ? main: loop: sw $0, DEFN($0) jal getline la $s0, inBUF lb $t0, ($s0) lb $t1, DOLLAR($0) beq $t0, $t1, QUIT lb $t1, L($0) bne $t0, $t1, get_tok li $s0, 1 sw $s0, DEFN($0) get_tok: Read Each Input Line For each input line: SAVE Labels and LOCs MAP op to machine code OPERANDS
What Does an Assembler Do ? main: loop: sw $0, DEFN($0) jal getline la $s0, inBUF lb $t0, ($s0) lb $t1, DOLLAR($0) beq $t0, $t1, QUIT lb $t1, L($0) bne $t0, $t1, get_tok li $s0, 1 sw $s0, DEFN($0) get_tok: Read Each Input Line For each input line: SCAN each line to tokens SAVE Labels and LOCs MAP op to machine code OPERANDS => PARSE tokens
Scanner • Scan each input line • Get a char and its type (HW2) • If current char and its preceding one is related • Keep collecting as a single token • Otherwise, • Save as a new token • Example • THISLOOP: LWU R2,63 # • Token Token Type • ---------------------------- ---------------- • THISLOOP 2 • : 4 • LWU 2 • R2 2 • , 4 • 63 1 • # 5
Automata • Given a state, no changes until an external stimulus • Light switches • Traffic lights • A system with a memory • How to relate one char to its predecessor ? • Repetitive actions • Get a char, search for its type • New token • Collect a char into a token string • Complete assembly of a token • State Diagrams
State Transition Diagram Q0 T=5 TOKEN=curChar, TokSpace=7 (ACT 2) Q1 T=1 curChar =Get next char, T=ChType(curChar) (ACT 1) T=3,4 T=6 T=2 curChar =Get next char, T=ChType(curChar) (ACT 1) Q3 Q4 Q2 Save TOKEN into TokArray (ACT 4) Q5 Return Q0 Save TOKEN into TokArray (ACT 4) T=1,2 T <> 1,2 T=1 T <> 1 Save TOKEN into TokArray (ACT 4) Q9 Q6 Q7 Q8 TOKEN=TOKEN+curChar TokSpace=TokSpace-1 (ACT 3) TOKEN=TOKEN+curChar TokSpace=TokSpace-1 (ACT 3) Q1 Q1
Operand • Registers • Replace by Reg Number • Literal • Easy • Variable (Label) • Backward Reference – simple • Forward Reference ==== PROBLEM main: loop: sw $0, DEFN($0) jal getline la $s0, inBUF lb $t0, ($s0) lb $t1, DOLLAR($0) beq $t0, $t1, QUIT lb $t1, L($0) bne $t0, $t1, get_tok li $s0, 1 sw $s0, DEFN($0) get_tok:
2-pass Assembler • Pass 1: construct a symbol table • Read a line and scan it • Check for label: save in Symbol Table with LOC as value • Check op field: if machine op, increment LOC • If neither, an error • Pass 2: • Reset LOC • Read a line and scan it • label: skip if found in symbol table • machine op: find op-code and default values, increment LOC • Operand: check expression • Assemble and output
1-pass Assembler • Read a line and scan • Label ? • Enter into symbol table with LOC as defined value • Operator • If machine op • Look up machine code and save • Increment LOC • If pseudo op • Process as necessary • Otherwise • error • Operand • Register – get reg # • Expression – evaluate • Assemble
Forward Reference Symbol value status FAN exit 1000 100 exit 1010 000 exit 1020 000 exit 1030 001 • Loc instr assembled • j exit 0b ff ff ff • beq $t1, $t0, exit • 11 28 1000 • blez $s0, exit • 1a 00 1010 • 1030 exit: jr $ra 03 e0 FF • Forward reference • Symbol not found • Assemble with FFFFx as offset • save the symbol with LOC as it value • Symbol found, but another forward ref. • Assemble with value in symTab as offset • Save LOC as value in symTab • Symbol found, new defintion • Assemble with offsets by tracing back pointers • Save LOC as value in symTab