Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recitation Material of Engineering an Assembler June 11, 2013.

Similar presentations


Presentation on theme: "Recitation Material of Engineering an Assembler June 11, 2013."— Presentation transcript:

1 Recitation Material of Engineering an Assembler June 11, 2013

2 Step 1. Program Load Make sure where the code will be loaded, so that the corresponding address is correct. In modern assembler, address is determined when source file is loaded ~~~

3 [ ] 23bdfff8 addi $sp, $sp, -8 [ ] afbf0000 sw $ra, 0($sp) [ c] ori $v0, $0, 1 ; 16: li $v0, 1 # 0! = 1 [ ] beq $a0, $0, 24 [zero-0x ] [ ] afa40004 sw $a0, 4($sp) [ c] 0c jal 0x [factorial] [ ] 8fa40004 lw $a0, 4($sp) [ ] mul $v0, $a0, $v0 [ ] 8fbf0000 lw $ra, 0($sp) [ c] 23bd0008 addi $sp, $sp, 8 [ ] 03e00008 jr $ra [ ] 23bdfffc addi $sp, $sp, -4 [main: Entry] [ ] afbf0000 sw $ra, 0($sp) [ c] 3c lui $at, 4097 [prompt] ; 39: la $a0, prompt [ ] ori $a0, $at, 0 [prompt] [ ] ori $v0, $0, 4 ; 40: li $v0, 4 [ ] c syscall [ c] ori $v0, $0, 5 ; 43: li $v0, 5 [ ] c syscall [ ] addu $a0, $0, $v0 [ ] 0c jal 0x [factorial]; [ c] addu $a0, $0, $v0 [ ] ori $a0, $0, 1 ; 50: li $v0, 1 [ ] c syscall [ ] 3c lui $at, 4097 [endl] ; 53: la $a0, endl [ c] f ori $a0, $1, 31 [endl] [ ] ori $v0, $0, 4 ; 54: li $v0, 4 [ ] c syscall [ ] ori $at, $0, 0 ; 57: li $v0, 0 # Return zero. [ c] 8fbf0000 lw $ra, 0($sp) [004000a0] 23bd0004 addi $sp, $sp, 4 [004000a4] 03e00008 jr $ra ; 61: jr $ra $sp 0x7ffffdfc 0x x c 0x x x x x x

4 Step 2. Instruction Table Take addi $sp, $sp, -8 for example Addi is 8 rs rtimm Which is 0x

5 Step 3. Register Table (optional) For example, the table could be an array, which is in format: $sp, $a0, $v0, $ra, $t1, $t2, $t3 Then you can use a lookup-table function, the return value is offset or index of the corresponding value of the register name(Notice the value is in array format).

6 # the lookup_vartable_func need to be called multiple times lookup_vartable_func: add $sp, $sp -4 # push v1 first sw $v1, 0($sp) add $sp, $sp, -4 # push a0 second sw $a0, 0($sp) add $sp, $sp, -4 # push a1 third sw $a1, 0($sp) add $sp, $sp, -4 # push t7 fourth sw $t7, 0($sp) add $sp, $sp, -4 # push t8 fifth sw $t8, 0($sp) add $sp, $sp, -4 # push s3 sixth sw $s3, 0($sp) add $sp, $sp, -4 # push s3 sw $s4, 0($sp) # now we begin to compare variable with parameter buffer # this_prepare_variable: # set up reference key for variable table li $v0, 0 li $a0, 0 # offset pointer in parameter buffer li $a1, 0 # set matching flag = 0 li $t8, 0x2c # t8 = comma or, li $t7, 0x28 # t7 = left parenthesis or ( lb $v1, parameter_buffer($a0) li $s4, 0 lb $s3, value_buffer($s4) Lookup –Register Function

7 this_not_set_cur_flag: add $a0, $a0, 1 add $s4, $s4, 1 lb $s3, value_buffer($s4) lb $v1, parameter_buffer($a0) j this_lookup_variable_table this_finish_compare_one: add $v0, $v0, 1 beqz $a1, this_return_match_var li $s4, 0 add $a0, $a0, 1 lb $s3, value_buffer($s4) lb $v1, parameter_buffer($a0) # reset a1 to be 0 to prepare next comparison li $a1, 0 j this_lookup_variable_table Lookup –Register Function (Cont. ) this_finish_all_keys: add $v0, $v0, 1 beqz $a1, this_return_match_var li $v0, 0 this_return_match_var: lw $s4, 0($sp) add $sp, $sp, 4 lw $s3, 0($sp) add $sp, $sp, 4 lw $t8, 0($sp) add $sp, $sp, 4 lw $t7, 0($sp) add $sp, $sp, 4 lw $a1, 0($sp) add $sp, $sp, 4 lw $a0, 0($sp) add $sp, $sp, 4 lw $v1, 0($sp) jr $ra

8 Step 4. Recognize Token and Assemble Instructions Shift bit according to recognized opcode say value of $rs is loaded using: lw $t3, register_value_table($s0) sll $t3, $t3, 21 Say your addi opcode is loaded into $t1 now we assemble them, say: add $t2, $t1, $t3

9 Step 5. Output instructions Simply create an output buffer and sequentially copy each instruction first Copy 0xffffffff ffffffff to separate.text segment and.data segment Copy all data into.data segment


Download ppt "Recitation Material of Engineering an Assembler June 11, 2013."

Similar presentations


Ads by Google