我试图用MIPS编写一个程序,它不断提示输入两个整数,并打印和,直到和为0。诀窍是,如果和为13,我需要调用一个方法来更改组装的MIPS代码,以便
add $t2, $t0, $t1变成了
and $t2, $t0, $t1循环的所有后续运行都使用和指令。
我有一个求和循环,这样当13是和时,方法instMod就会被调用,我想修改这个指令。不幸的是,我不知道从哪里开始,在网上找不到任何例子。我假设我需要从集合的代码中获取加法的十六进制代码,并将其替换为and的十六进制代码,但我不知道如何做到这一点,或者这是否是正确的行动方针。
# Nick Gilbert
# MIPS Program to demonstrate self-modifying code
.data
num1Prompt: .asciiz "Enter num1: "
num2Prompt: .asciiz "Enter num2: "
num1: .word 0
num2: .word 0
addOut: .asciiz "ADD: "
andOut: .asciiz "AND: "
.text
main:
sumLoop:
la $a0, num1Prompt #Asking user for num1
li $v0, 4 #Call code to print string
syscall
li $v0, 5 #Call code to read an int
syscall
move $t0, $v0 #Moving read int to $t1
la $a0, num2Prompt #Asking user for num2
li $v0, 4 #Call code to print string
syscall
li $v0, 5 #Call code to read an int
syscall
move $t1, $v0 #Moving read int to $t2
add $t2, $t0, $t1 #Adding num1 and num2 together
la $a0, addOut
li $v0, 4
syscall
move $a0, $t2
li $v0, 1
syscall
beq $t2, 13, instMod #Calling method to modify add instruction if sum = 13
bne $t2, 0, sumLoop #If result is not yet 0, ask for new sum
endSumLoop:
li $v0, 10
syscall
instMod: #Method to change add instruction to an and instruction发布于 2015-03-25 23:10:00
在要替换的指令处添加一个标签,例如:
instruction_to_be_replaced:
add $t2, $t0, $t1 #Adding num1 and num2 together然后在你的日常instMod中
instMod: #Method to change add instruction to an and instruction
lw $t1, instruction_to_replace
sw $t1, instruction_to_be_replaced
j sumLoop # go back to your sumLooop
instruction_to_replace:
and $t2, $t0, $t1代码在临时寄存器$t1中加载要替换的指令的内容,然后将其存储在标记为instruction_to_be_replaced的位置。
指令的“来源”在instruction_to_replace中有标签。
要做到这一点,您需要能够在代码部分上编写,否则您就不会问这个问题了。
发布于 2015-03-25 17:30:52
试试这个:
mov从步骤2到instMod部分第3步的位置为此,两个带有操作数的指令必须具有相同的长度。如果不是,请按原版或适当的nop替换。
https://stackoverflow.com/questions/29262391
复制相似问题