1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Short MIPS Program

  1. Dec 5, 2011 #1
    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.
     
  2. jcsd
  3. Dec 6, 2011 #2

    Mark44

    Staff: Mentor

    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?
     
  4. Dec 6, 2011 #3

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  5. Dec 6, 2011 #4

    chiro

    User Avatar
    Science Advisor

    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.
     
  6. Dec 6, 2011 #5

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  7. Dec 6, 2011 #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
     
  8. Dec 6, 2011 #7

    rcgldr

    User Avatar
    Homework Helper

    update - I was thinking of chiro's post, not yours. The logical right shift will work.
     
    Last edited: Dec 7, 2011
  9. Dec 6, 2011 #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 im left with a 1(with 31 0's in front of it to make it 32 bits) right?
     
  10. Dec 7, 2011 #9

    chiro

    User Avatar
    Science Advisor

    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.
     
  11. Dec 7, 2011 #10

    rcgldr

    User Avatar
    Homework Helper

    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: Dec 7, 2011
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Short MIPS Program
  1. Mips subroutines (Replies: 1)

  2. MIPS help (Replies: 1)

  3. Relocation Table MIPS (Replies: 4)

Loading...