I am working on a C program that parses code from a high level language and generates MIPS code. I have a question about the logic to test for (in)equality in an IF statement. For example, my code generation for a statement like
IF a!=b
I just use
else if(reltoken == neqsym) // !=
{
setcode(SUB, topregister-2, topregister-2, topregister-1);
setcode(OR, topregister-2, 10, topregister-2);
}
I'll explain what I'm doing here...
Basically, I use a "register stack" so topregister-1 would be the register holding the most recent value pushed onto the "stack" and topregister-2 would be the one pushed before that, etc. By subtracting these two values, I will know they are equal if the subtraction results in a 0. I then OR this value with the $zero register (which i just call 10 in my C program) If the result of the OR is a 0, then the 2 values compared are equal and the BEQ will then branch because the BEQ compares this result with the $zero register, and the if condition was false. But this test isn't the problem...My != test works but my = test does not.
Keep in mind I know that using BNE would be the "easy way" rather than using BEQ, but I am required to use BEQ to somehow do this = test. I am trying to use the same code as above, but just replacing the OR with NOR, but it's not working. When I check the hex value in the register using PCSpim (PCSpim is what I use to test my MIPS code) It shows something weird like 0xfffffe when it should be 1 or 0. I must be doing something wrong here...but I thought NOR gives you the opposite answer as OR...which is what I assume I need here because = is the opposite of !=. I hope my question makes sense.
Any help is greatly appreciated!