| | |
Checking an equality condition using BEQ in MIPS
![]() |
•
•
Join Date: May 2009
Posts: 1
Reputation:
Solved Threads: 0
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
I just use
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!
Assembly Syntax (Toggle Plain Text)
IF a!=b
I just use
Assembly Syntax (Toggle Plain Text)
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!
![]() |
Similar Threads
- Help with Classes !!! (Homework) (C++)
- data compression help! (C++)
- question (Java)
- binary file (C++)
- HELP!!! (solving task) (C)
- How to set User control=false (ASP.NET)
- Program that shows a graphical representation of hard disk surface problems? (Storage)
- Program Issues (C++)
- template issues - need expert debugger! (C++)
Other Threads in the Assembly Forum
- Previous Thread: Help..!!
- Next Thread: heeeeelp plz :( (assemply lang)
| Thread Tools | Search this Thread |





