Help Needed: Writing a MIPS Program w/ 2 Instructions

Click For Summary

Discussion Overview

The discussion revolves around writing a MIPS program that utilizes only two instructions to determine if a signed integer, provided in register $a0, is negative. Participants explore various approaches and MIPS instructions that could achieve this task, focusing on the constraints of using only two true operations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants suggest using one instruction to load the value into $a0 and another to set $v0 based on whether $a0 is negative.
  • There is a proposal to use a bitwise AND operation to check the most significant bit of the signed integer, which would indicate if it is negative.
  • Others mention that an arithmetic shift right could produce a 0 for positive numbers and -1 for negative numbers, but this would require additional instructions.
  • One participant suggests using a logical right shift to isolate the sign bit, questioning how this would work with a 32-bit value.
  • Another participant notes the potential use of the SLT (set on less than) instruction with an immediate value of 0 as an alternative method to determine negativity.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to solve the problem, with no consensus reached on a single method. Some methods proposed may require more than two instructions, leading to further debate on the constraints of the task.

Contextual Notes

Participants highlight the limitations of the task, particularly the requirement to use only two true operations and the implications of using different MIPS instructions. There is also uncertainty regarding the behavior of specific instructions when applied to signed integers.

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

  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
15K
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
14K
  • · Replies 1 ·
Replies
1
Views
3K