C 函数到 MIPS
C function to MIPS
我已经被这个作业问题困扰了很长一段时间,希望得到一些帮助。
到目前为止我已经想到了这个:
f:
lw $t0, a
lw $t1, b
lw $t2, c
lw $t3, d
addi $sp, $sp, -4
真正让我困惑的部分是 return 语句。我不知道该怎么做。这是作业问题。
这明显是一道作业题
.data
.text
f:
# load arguments IN into temporary registers
lw $t0, 0($sp)
lw $t1, 4($sp)
lw $t2, 8($sp)
lw $t3, 12($sp)
# $t4 <-- calling func(a, b)
# backup $ra register on the stack
addi $sp, $sp, -4
sw $ra, 0($sp)
# allocate words for all required backup registers
addi $sp, $sp, -16
# store the values as backup on the stack
sw $t0, 0($sp)
sw $t1, 4($sp)
sw $t2, 8($sp)
sw $t3, 12($sp)
# allocate space for all the arguments (arguments in & argument out)
addi $sp, $sp, -12
# store arguments IN on the stack:
sw $t0, 0($sp)
sw $t1, 4($sp)
# call the subprogram func
jal func
# read arguments OUT from stack
lw $t4, 8($sp)
# deallocate space for arguments IN and OUT
addi $sp, $sp, 12
# restore the backup values:
lw $t0, 0($sp)
lw $t1, 4($sp)
lw $t2, 8($sp)
lw $t3, 12($sp)
# deallocate space for backed up registers
addi $sp, $sp, 16
# restore $ra value
lw $ra, 0($sp)
# deallocate space for $ra
addi $sp, $sp, 4
# $t4 <-- calling func(func(a, b), c + d)
# backup $ra register on the stack
addi $sp, $sp, -4
sw $ra, 0($sp)
# allocate words for all required backup registers
addi $sp, $sp, -20
# store the values as backup on the stack
sw $t0, 0($sp)
sw $t1, 4($sp)
sw $t2, 8($sp)
sw $t3, 12($sp)
sw $t4, 16($sp)
# allocate space for all the arguments (arguments in & argument out)
addi $sp, $sp, -12
# store arguments IN on the stack:
sw $t4, 0($sp)
add $t5, $t2, $t3
sw $t5, 4($sp)
# call the subprogram func
jal func
# read arguments OUT from stack
lw $t6, 8($sp)
# deallocate space for arguments IN and OUT
addi $sp, $sp, 12
# restore the backup values:
lw $t0, 0($sp)
lw $t1, 4($sp)
lw $t2, 8($sp)
lw $t3, 12($sp)
lw $t4, 16($sp)
# deallocate space for backed up registers
addi $sp, $sp, 20
# restore $ra value
lw $ra, 0($sp)
# deallocate space for $ra
addi $sp, $sp, 4
# return $t6
sw $t6, 16($sp)
f_end:
jr $ra
我已经被这个作业问题困扰了很长一段时间,希望得到一些帮助。
到目前为止我已经想到了这个:
f:
lw $t0, a
lw $t1, b
lw $t2, c
lw $t3, d
addi $sp, $sp, -4
真正让我困惑的部分是 return 语句。我不知道该怎么做。这是作业问题。
这明显是一道作业题
.data
.text
f:
# load arguments IN into temporary registers
lw $t0, 0($sp)
lw $t1, 4($sp)
lw $t2, 8($sp)
lw $t3, 12($sp)
# $t4 <-- calling func(a, b)
# backup $ra register on the stack
addi $sp, $sp, -4
sw $ra, 0($sp)
# allocate words for all required backup registers
addi $sp, $sp, -16
# store the values as backup on the stack
sw $t0, 0($sp)
sw $t1, 4($sp)
sw $t2, 8($sp)
sw $t3, 12($sp)
# allocate space for all the arguments (arguments in & argument out)
addi $sp, $sp, -12
# store arguments IN on the stack:
sw $t0, 0($sp)
sw $t1, 4($sp)
# call the subprogram func
jal func
# read arguments OUT from stack
lw $t4, 8($sp)
# deallocate space for arguments IN and OUT
addi $sp, $sp, 12
# restore the backup values:
lw $t0, 0($sp)
lw $t1, 4($sp)
lw $t2, 8($sp)
lw $t3, 12($sp)
# deallocate space for backed up registers
addi $sp, $sp, 16
# restore $ra value
lw $ra, 0($sp)
# deallocate space for $ra
addi $sp, $sp, 4
# $t4 <-- calling func(func(a, b), c + d)
# backup $ra register on the stack
addi $sp, $sp, -4
sw $ra, 0($sp)
# allocate words for all required backup registers
addi $sp, $sp, -20
# store the values as backup on the stack
sw $t0, 0($sp)
sw $t1, 4($sp)
sw $t2, 8($sp)
sw $t3, 12($sp)
sw $t4, 16($sp)
# allocate space for all the arguments (arguments in & argument out)
addi $sp, $sp, -12
# store arguments IN on the stack:
sw $t4, 0($sp)
add $t5, $t2, $t3
sw $t5, 4($sp)
# call the subprogram func
jal func
# read arguments OUT from stack
lw $t6, 8($sp)
# deallocate space for arguments IN and OUT
addi $sp, $sp, 12
# restore the backup values:
lw $t0, 0($sp)
lw $t1, 4($sp)
lw $t2, 8($sp)
lw $t3, 12($sp)
lw $t4, 16($sp)
# deallocate space for backed up registers
addi $sp, $sp, 20
# restore $ra value
lw $ra, 0($sp)
# deallocate space for $ra
addi $sp, $sp, 4
# return $t6
sw $t6, 16($sp)
f_end:
jr $ra