I’m just trying to branch to a different segment of code given a number is positive or negative, using MIPS assembly, this is the short segment which is supposed to do just that:
lbu $4, digit
and $5, $4, 0x80
srl $5, $5, 31
bgt $5, 0, positive
b negative
The digit is successfully in $4, I bitwise AND it against a 1 with all following zeros in order to get whether the first bit is 1 or 0, then I shift right 31 bits so it’s in the last position, and then I check if its greater than 0, and if so, branch positive, else, branch negative.
I’m sure I’m messing up the logic somewhere, and I’m also unsure if I can use 0 like that (in bgt), or if I should be using $0
Any ideas? Thanks!
I think you are shifting your byte way out of the picture. Using your approach, you should shift it 7 bits, not 31. But…
Instead of using
lbu, you can uselb, according to http://www.ece.umd.edu/~manoj/759M/MIPSALM.html . That will copy the value of bit 7 of the byte you are loading and put in in bits 8-31 of the target register. Then you can usebgezto do the branch.But having never written a byte of MIPS assembler in my life, you should perhaps take this answer with a pinch of salt. Good Luck!