MIPS overwriting/storing 变量中的值

MIPS overwriting/storing values in variables

我在 MIPS 程序中设置值时遇到问题。 if x != y: z = 78 w = 5//转换为MIPS的指令

lw $t1,x
lw $t2,y
bne $t1,$t2, label
label: 
lw $t3,z
lw $t4,w      
li $t4,5
li $t3,78

一切正常,直到我需要分别将 78 和 5 分配给 z 和 w。然而,这些变量中存储了一些值,所以当我尝试将它们设置为 78 或 5 时,它只是将 78 或 5 添加到其中的任何内容。谁能指出我做错了什么的正确方向?

  • 您没有将新值存储到它们各自的变量中,您也不需要读取它们,因为您不需要它来计算新值(它在您的样本中是一个常量)。
  • 你的分支也是错误的,两种可能的结果都只是执行下一条指令。您必须使用相反的比较并分支到 then 部分之后的位置。

即:

  lw $t1,x
  lw $t2,y
  beq $t1,$t2, skip # branch if condition not met
  li $t4,5
  li $t3,78
  sw $t3,z
  sw $t4,w      
skip: