如何在MIPS中实现NOT操作?

如何在MIPS中实现NOT操作?,mips,mips32,Mips,Mips32,我是MIPS新手,我正在尝试判断字符串中的每个字符是否都是alpha。我使用ASCII码来帮助我判断它,但我发现没有任何指令表示的大于的含义。因此,我尝试根据已知的指令实现not操作。以下是我的部分代码: isAlpha: sltiu $t0, $s2, 123 sltiu $t1, $s2, 97 nor $t1, $t1, $t1 and $t0, $t0, $t1 sltiu $t2, $s2, 107 sltiu $t3, $s2, 81 nor $t3,

我是MIPS新手,我正在尝试判断字符串中的每个字符是否都是alpha。我使用ASCII码来帮助我判断它,但我发现没有任何指令表示的
大于
的含义。因此,我尝试根据已知的指令实现
not
操作。以下是我的部分代码:

isAlpha:
  sltiu $t0, $s2, 123
  sltiu $t1, $s2, 97
  nor $t1, $t1, $t1
  and $t0, $t0, $t1

  sltiu $t2, $s2, 107
  sltiu $t3, $s2, 81
  nor $t3, $t3, $t3
  and $t2, $t2, $t3

  or $t0, $t0, $t2

  bne $t0, $zero, countAlpha
  jr $ra
然而,我无法得到我想要的结果。我设置了一个断点,发现我的
not
操作似乎有一些问题:

在我的例外情况中,$t1应该是1,$t2应该是0,而事实并非如此


我的密码哪里错了?有没有办法在MIPS中实现
not
操作?或者有没有更好的方法来实现MIPS中的
大于
的含义?提前感谢。

您在这里没有操作:

nor $t1, $t1, $t1
通常,您只需键入
而不是
助记符,您的汇编程序就会将其解释为

我认为您需要一个异或操作来告诉您输入是否只是小于123和小于97中的一个

类似这样的(完全未经测试)


通过将参数反转为
slt*
,可以获得
gt
的效果:

    sltu   $v0,$t0,$t1             # v0 = $t0 < $t1 (what you have)
    sltu   $v0,$t1,$t0             # v0 = $t0 > $t1 (what you want)

我为此创建了一个答案,但在发布之前,我找到了你的答案。我几乎不打算发布我的[它有一个额外的
指令],因为我开始认为你的已经足够了——并且投票给你的。然后,我意识到我担心的边缘案件是真的。但是,不管怎样,对你来说+1。看到我的答案了吗/why@Craig埃斯蒂:你在回答中说“两个零的异或等于1”,你确定吗?我想那天我睡眠不足:-)
    sltu   $v0,$t0,$t1             # v0 = $t0 < $t1 (what you have)
    sltu   $v0,$t1,$t0             # v0 = $t0 > $t1 (what you want)
# isAlpha -- decide if char is alpha
#
# RETURNS:
#   v0 -- 1=is alpha
#   s6 -- count of alpha chars
#
# arguments:
#   s2 -- character to test
#
# registers:
#   t0 -- holds lt 'a/A'? value
#   t1 -- holds lt 'a/A' + 1? value
#   t2 -- bool for within 'a-z'
#   t3 -- bool for within 'A-Z'
isAlpha:
    # within a-z range
    sltiu   $t0,$s2,0x61            # lt 'a'? (i.e. not a-z)
    sltiu   $t1,$s2,0x7B            # lt 'z' + 1? (i.e. le 'z')
    xor     $t2,$t0,$t1             # want one but not both
    and     $t2,$t2,$t1             # want only lt 'z' + 1

    # within A-Z range
    sltiu   $t0,$s2,0x41            # lt 'A'? (i.e. not A-Z)
    sltiu   $t1,$s2,0x5C            # lt 'Z' + 1? (i.e. le 'Z')
    xor     $t3,$t0,$t1             # want one but not both
    and     $t3,$t3,$t1             # want only lt 'Z' + 1

    or      $v0,$t2,$t3             # decide if alpha
    add     $s6,$s6,$v0             # increment alpha count
    jr      $ra