自修改MIPS代码 [英] Self-Modifying MIPS Code

查看:164
本文介绍了自修改MIPS代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用MIPS编写一个程序,该程序连续提示输入两个整数并打印总和,直到总和为0.诀窍是,如果总和为13,我需要调用一个方法来更改已组装的MIPS这样的代码

I'm trying to write a program in MIPS that continuously prompts for two integers and prints the sum until the sum is 0. The trick is that if the sum is 13, I need to call a method to change the assembled MIPS code so that

add $t2, $t0, $t1

成为

and $t2, $t0, $t1

和随后的所有循环运行均使用and指令.

and all subsequent runs of the loop use the and instruction.

我有一个求和循环,因此当总和为13时,将调用要修改指令的instMod方法.不幸的是,我不知道从哪里开始,也找不到在线的任何示例.我假设我需要以某种方式从汇编代码中获取add的十六进制代码,并用和的十六进制代码替换它,但是我不知道该怎么做,或者这是否是正确的做法. /p>

I have the summation loop working so that when 13 is the sum the method instMod is called which I want to modify the instruction. Unfortunately, I have no idea where to start and can't find any examples of this online. I assume I need to somehow get the hex code of the add out of the assembled code and replace it with the hex code for the and but I do not know how to do that or if that is the right course of action to take.

# 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

推荐答案

在要替换的指令上添加标签,例如:

Add a label at the instruction you want to replace, e.g:

instruction_to_be_replaced:
  add $t2, $t0, $t1   #Adding num1 and num2 together

然后在您的常规instMod

then in your routine 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的位置.

The code loads in temporary register $t1 the contents of the instruction you want to replace, and then stores it in the location labelled instruction_to_be_replaced.

该指令的源"标记在"instruction_to_replace"中.

The "source" of the instruction goes labelled in instruction_to_replace.

要做到这一点,您需要能够在代码段中编写(我认为您已经编写过),否则您将不会问这个问题.

To do this, you need to be able to write on the code section which I assume you have otherwise you would not be asking this question.

这篇关于自修改MIPS代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆