自修改 MIPS 代码
Self-Modifying MIPS Code
我正在尝试用 MIPS 编写一个程序,不断提示输入两个整数并打印总和,直到总和为 0。诀窍是如果总和为 13,我需要调用一个方法来更改汇编的 MIPS 代码使得
add $t2, $t0, $t1
变成
and $t2, $t0, $t1
循环的所有后续运行都使用 and 指令。
我有求和循环工作,所以当 13 是总和时,调用方法 instMod,我想修改指令。不幸的是,我不知道从哪里开始,也找不到任何在线示例。我假设我需要以某种方式从汇编代码中获取 add 的十六进制代码,并将其替换为 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
试试这个:
- Assemble目标文件所需的指令
- 提取等效机器码的十六进制
- 在您需要更改的代码前面放置一个标签
mov
第 2 步中的十六进制数到 instMod
部分第 3 步中的位置
要实现此功能,带有操作数的两条指令必须具有相同的长度。如果不是,请根据需要用 nop
填充原件或替换件。
在要替换的指令处添加标签,例如:
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
的位置。
指令的 "source" 标记为 instruction_to_replace。
要做到这一点,您需要能够在我假设您已经拥有的代码部分进行书写,否则您不会问这个问题。
我正在尝试用 MIPS 编写一个程序,不断提示输入两个整数并打印总和,直到总和为 0。诀窍是如果总和为 13,我需要调用一个方法来更改汇编的 MIPS 代码使得
add $t2, $t0, $t1
变成
and $t2, $t0, $t1
循环的所有后续运行都使用 and 指令。
我有求和循环工作,所以当 13 是总和时,调用方法 instMod,我想修改指令。不幸的是,我不知道从哪里开始,也找不到任何在线示例。我假设我需要以某种方式从汇编代码中获取 add 的十六进制代码,并将其替换为 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
试试这个:
- Assemble目标文件所需的指令
- 提取等效机器码的十六进制
- 在您需要更改的代码前面放置一个标签
mov
第 2 步中的十六进制数到instMod
部分第 3 步中的位置
要实现此功能,带有操作数的两条指令必须具有相同的长度。如果不是,请根据需要用 nop
填充原件或替换件。
在要替换的指令处添加标签,例如:
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
的位置。
指令的 "source" 标记为 instruction_to_replace。
要做到这一点,您需要能够在我假设您已经拥有的代码部分进行书写,否则您不会问这个问题。