Recitation Material of Engineering an Assembler June 11, 2013

advertisement
Recitation Material of
Engineering an Assembler
June 11, 2013
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 ~~~
[00400024] 23bdfff8 addi $sp, $sp, -8
[00400028] afbf0000 sw $ra, 0($sp)
[0040002c] 34020001 ori $v0, $0, 1
; 16: li $v0, 1 # 0! = 1
[00400030] 10800006 beq $a0, $0, 24 [zero-0x00400030]
[00400034] afa40004 sw $a0, 4($sp)
[0040003c] 0c100009 jal 0x00400024 [factorial]
[00400040] 8fa40004 lw $a0, 4($sp)
[00400044] 70821002 mul $v0, $a0, $v0
[00400048] 8fbf0000 lw $ra, 0($sp)
[0040004c] 23bd0008 addi $sp, $sp, 8
[00400050] 03e00008 jr $ra
[00400054] 23bdfffc addi $sp, $sp, -4 [main: Entry]
[00400058] afbf0000 sw $ra, 0($sp)
[0040005c] 3c011001 lui $at, 4097 [prompt] ; 39: la $a0, prompt
[00400060] 34240000 ori $a0, $at, 0 [prompt]
[00400064] 34020004 ori $v0, $0, 4
; 40: li $v0, 4
[00400068] 0000000c syscall
[0040006c] 34020005 ori $v0, $0, 5
; 43: li $v0, 5
[00400070] 0000000c syscall
[00400074] 00022021 addu $a0, $0, $v0
[00400078] 0c100009 jal 0x00400024 [factorial];
[0040007c] 00022021 addu $a0, $0, $v0
[00400080] 34020001 ori $a0, $0, 1
; 50: li $v0, 1
[00400084] 0000000c syscall
[00400088] 3c011001 lui $at, 4097 [endl] ; 53: la $a0, endl
[0040008c] 3424001f ori $a0, $1, 31 [endl]
[00400090] 34020004 ori $v0, $0, 4
; 54: li $v0, 4
[00400094] 0000000c syscall
[00400098] 34020000 ori $at, $0, 0
; 57: li $v0, 0 # Return zero.
[0040009c] 8fbf0000 lw $ra, 0($sp)
[004000a0] 23bd0004 addi $sp, $sp, 4
[004000a4] 03e00008 jr $ra
; 61: jr $ra
0x0040001
8
0x0000000
3
0x0040007c
0x0000000
2
0x0040004
0
0x0000000
1
0x0040004
0
$sp 0x7ffffdfc
0x0040004
0
Step 2. Instruction Table
• Take addi $sp, $sp, -8 for example
Addi is
8
00100000
rs
00000000
Which is 0x 20 00 00 00
rt
00000000
imm
000000000
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).
Lookup –Register Function
# 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 (Cont. )
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
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,
lw
$s3,
0($sp)
add
$sp,
$sp,
lw
$t8,
0($sp)
add
$sp,
$sp,
lw
$t7,
0($sp)
add
$sp,
$sp,
lw
$a1,
0($sp)
add
$sp,
$sp,
lw
$a0,
0($sp)
add
$sp,
$sp,
lw
$v1,
0($sp)
jr
$ra
4
4
4
4
4
4
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
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