Help Needed: Writing a MIPS Program w/ 2 Instructions

AI Thread Summary
The discussion focuses on writing a MIPS program using only two instructions to determine if a signed integer in register $a0 is negative. Participants suggest using a logical right shift to isolate the sign bit, which would return 1 for negative numbers and 0 for positive ones. An alternative method discussed is using the SLT (set on less than) instruction with an immediate value of 0, which can also achieve the desired result. The conversation emphasizes the importance of understanding MIPS opcodes and the limitations of using only two true operations. Ultimately, the logical right shift is favored for its broader applicability across different processors.
johnnyrocket
Messages
8
Reaction score
0
I need to write a MIPS program (using only 2 true-ops) to take in 1 signed integer argument into register $a0, and return (into $v0) a 1 if the input was negative, and a 0 (into $v0) otherwise. Can anyone offer help. I'm not sure since I can only use 2 instructions.
 
Physics news on Phys.org
johnnyrocket said:
I need to write a MIPS program (using only 2 true-ops) to take in 1 signed integer argument into register $a0, and return (into $v0) a 1 if the input was negative, and a 0 (into $v0) otherwise. Can anyone offer help. I'm not sure since I can only use 2 instructions.

You must have available the op codes you can use. What are the op codes you could use to 1) move a value into a register and 2) test to see if it is negative?
 
I'm assuming it takes one instruction to load the value into $a0. That leaves you one instuction to set $v0 to 0 or 1 if $a0 is negative or not. Look through the list of instructions for the MIPS to see if you can find an instruction that will accomplish this.
 
Hey johnnyrocket and welcome to the forums.

A signed integer will have one of its bits set to 1 (usually the most significant bit), so you should be able to do a bitwise AND which will return 0 if positive or a non-zero number if negative.
 
chiro said:
A signed integer will have one of its bits set to 1 (usually the most significant bit), so you should be able to do a bitwise AND which will return 0 if positive or a non-zero number if negative.
That will take an extra instruction. So will an arithmetic shift right of 15 bits that will produce a 0 for positive or -1 for a negative number. There's are a couple of MIPS instructions that will do what he needs in a single instruction once $a0 has the value to be tested. This is more of a search exercise than some clever usage of math oriented instructions.
 
thanks for the reply. If I use 1 instruction to put the immediate value into the register, and the 2nd value to shift, that should do it right?

li $a, value
srl $v,$a,31
 
update - I was thinking of chiro's post, not yours. The logical right shift will work.
 
Last edited:
if the $a0 register has a 32 bit value in it, how will shifting it right 31 bits produce anything other than a 1 or 0 (the 1st bit)?

If I have 10000000000000000000000000000000 and I shift right 31 bits I am left with a 1(with 31 0's in front of it to make it 32 bits) right?
 
rcgldr said:
That will take an extra instruction. So will an arithmetic shift right of 15 bits that will produce a 0 for positive or -1 for a negative number. There's are a couple of MIPS instructions that will do what he needs in a single instruction once $a0 has the value to be tested. This is more of a search exercise than some clever usage of math oriented instructions.

What about a test instruction? In normal Intel assembly there is a test function that tests whether something is zero or not by performing an AND operation and using the result.
 
  • #10
johnnyrocket said:
if the $a0 register has a 32 bit value in it, how will shifting it right 31 bits produce anything other than a 1 or 0 (the 1st bit)?

If I have 10000000000000000000000000000000 and I shift right 31 bits I am left with a 1(with 31 0's in front of it to make it 32 bits) right?
I was thinking of chiro's post not yours. The logical right shift will work.

You also could have used SLT (set on less than) with an immediate value of 0, but the logical right shift is a more generic solution since many processors don't have an instruction like SLT.
 
Last edited:

Similar threads

Back
Top