How to implement NOT operation in MIPS?

jianjieluo picture jianjieluo · Mar 22, 2017 · Viewed 14.6k times · Source

I am new in MIPS, and I am trying to judge whether each char in a string is an alpha. I used the ASCII code to help me to judge it, while I found there is no instruction representing larger than meaning. So I try to implement a not operation from the instructions I have known. Here's part of my code:

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

However, I could not get the result I want. I set a breakpoint and found that my not operation seems to have some problems: After judge

In my exception, $t1 should be 1 and $t2 should be 0, while the reality is not.

Where's the wrong place in my code? Is there any way to implement not operation in MIPS? Or is there a better way to implement larger than meaning in MIPS? Thanks in advance.

Answer

sfi picture sfi · Mar 22, 2017

You have a not operation here:

nor $t1, $t1, $t1

Often you can just type the not mnemonic and your assembler will interpret it as a pseudo instruction

I think you want an exclusive or operation to tell you if your input is just one of less than 123 and less than 97.

Something like this (totally untested)

isAlpha:
  sltiu $t0, $s2, 123
  sltiu $t1, $s2, 97
  xor $t0, $t0, $t1

  sltiu $t2, $s2, 107
  sltiu $t3, $s2, 81
  xor $t2, $t2, $t3

  or $t0, $t0, $t2

  bne $t0, $zero, countAlpha
  jr $ra