首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自修改MIPS码

自修改MIPS码
EN

Stack Overflow用户
提问于 2015-03-25 17:24:33
回答 2查看 1.9K关注 0票数 1

我试图用MIPS编写一个程序,它不断提示输入两个整数,并打印和,直到和为0。诀窍是,如果和为13,我需要调用一个方法来更改组装的MIPS代码,以便

代码语言:javascript
复制
add $t2, $t0, $t1

变成了

代码语言:javascript
复制
and $t2, $t0, $t1

循环的所有后续运行都使用和指令。

我有一个求和循环,这样当13是和时,方法instMod就会被调用,我想修改这个指令。不幸的是,我不知道从哪里开始,在网上找不到任何例子。我假设我需要从集合的代码中获取加法的十六进制代码,并将其替换为and的十六进制代码,但我不知道如何做到这一点,或者这是否是正确的行动方针。

代码语言:javascript
复制
# 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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-25 23:10:00

在要替换的指令处添加一个标签,例如:

代码语言:javascript
复制
instruction_to_be_replaced:
  add $t2, $t0, $t1   #Adding num1 and num2 together

然后在你的日常instMod中

代码语言:javascript
复制
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中有标签。

要做到这一点,您需要能够在代码部分上编写,否则您就不会问这个问题了。

票数 3
EN

Stack Overflow用户

发布于 2015-03-25 17:30:52

试试这个:

  1. 将所需的指令组装到一个对象文件中。
  2. 提取等效机器代码的十六进制。
  3. 在您需要更改的代码前面放置一个标签
  4. mov从步骤2到instMod部分第3步的位置

为此,两个带有操作数的指令必须具有相同的长度。如果不是,请按原版或适当的nop替换。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29262391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档