Help Needed: Writing a MIPS Program w/ 2 Instructions

In summary: 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.
  • #1
johnnyrocket
8
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
  • #2
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?
 
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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
 
  • #7
update - I was thinking of chiro's post, not yours. The logical right shift will work.
 
Last edited:
  • #8
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?
 
  • #9
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:

1. What is MIPS?

MIPS (Microprocessor without Interlocked Pipeline Stages) is a reduced instruction set computer (RISC) instruction set architecture (ISA) developed by MIPS Technologies. It is commonly used in embedded systems, such as video game consoles, routers, and other devices.

2. What is a MIPS program?

A MIPS program is a set of instructions written in the MIPS assembly language that is used to control and operate a MIPS-based processor. It is typically used for low-level programming tasks, such as system calls and device drivers.

3. What is the purpose of a MIPS program with 2 instructions?

The purpose of a MIPS program with 2 instructions is to demonstrate the basic structure and functionality of a MIPS program. It is often used as a learning exercise for individuals who are new to programming with MIPS.

4. What are the 2 instructions commonly used in a MIPS program?

The two most commonly used instructions in a MIPS program are the load instruction (lw) and the store instruction (sw). The load instruction is used to load data from memory into a register, while the store instruction is used to store data from a register into memory.

5. What are some resources for learning how to write a MIPS program with 2 instructions?

There are many online resources available for learning how to write a MIPS program with 2 instructions. Some popular options include online tutorials, coding forums, and instructional videos. Additionally, many universities offer courses or resources for learning MIPS programming. It is also helpful to refer to the MIPS instruction set architecture manual for detailed information on the instructions and their usage.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
14K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top