Set bits 18, 19, 20, 21 to 1 ( Mips)

  • Thread starter Thread starter basketball853
  • Start date Start date
  • Tags Tags
    Bits Mips Set
Click For Summary

Discussion Overview

The discussion revolves around a homework problem requiring participants to set specific bits in a MIPS register ($v0) to 1 while ensuring other bits remain unchanged. The conversation explores various MIPS instructions and strategies to achieve this in two steps, as well as a follow-up question about manipulating additional bits in the same register.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in starting the problem and considers using the 'slt' opcode.
  • Another suggests the use of the 'ORI' instruction to set the bits.
  • A participant proposes using 'lui' to load an immediate value into a temporary register before using 'or' to modify $v0.
  • There is a discussion about the limitations of the 'or' instruction, which only affects the lower 16 bits, and the necessity of 'lui' to manipulate higher bits.
  • Participants clarify the concept of masking, explaining how 'AND' and 'OR' operations work in setting and clearing bits.
  • One participant shares their understanding of the binary representation and the need for multiple instructions to achieve the desired bit manipulation.
  • Another participant questions how to set and clear additional bits in $v0, seeking guidance on the number of steps required.
  • There is a clarification about the terminology of setting and clearing bits, emphasizing the need for both 'OR' and 'AND' operations.
  • One participant attempts to outline a solution but realizes it does not meet the required number of steps.
  • Another participant corrects the approach, explaining the need for multiple instructions to achieve the task correctly.

Areas of Agreement / Disagreement

Participants express various strategies and approaches to the problem, with some disagreement on the best method to achieve the desired outcome. The discussion remains unresolved regarding the specific implementation details for the follow-up question about manipulating additional bits.

Contextual Notes

Participants mention the need for multiple instructions due to the limitations of certain MIPS operations, but the exact implementation steps remain unclear and are subject to further exploration.

basketball853
Messages
18
Reaction score
0

Homework Statement



Set bits 18, 19, 20, and 21 to 1 in register $v0. $v0's other bits should not change. This can be done in two MIPS instructions. Do not use any pseudo-instructions or load any data from memory. You may use any registers that you wish.

Homework Equations

Here is more on the idea: if $v0 did look like this:

0b0000 0000 0000 0000 0000 0000 0000 0000 = 0x00000000
then afterwards, it should look like (counting from bit 0):
0b0000 0000 0011 1100 0000 0000 0000 0000 = 0x003C0000

Basically, just notice that bits 18 through 21 have been turned "on."

The Attempt at a Solution



I am having trouble even coming to a possible solution in 2 steps... i know that the target is register $v0 and i have to turn 18, 19, 20, 21 into 1111 ( or F in hex) but i do not know where to start... Also i know that i maybe able to use the slt opcode ? which is set if less than to 1

any thoughts ?
 
Physics news on Phys.org
Would perhaps the ORI instruction be of use?
 
how could i implement that though? good suggestion ... like an ex..
 
First, you lui into a temp register for example

lui $t0, 0000 0000 0011 1100

then, you or that with v0.

or $v0, $v0, $t0

Easy enough :)
 
basketball853 said:
how could i implement that though? good suggestion ... like an ex..
Well, let's say you've got this binary number:

1001 1101

And now you want to set these bits to 1:

0011 0000

OR them together, you get:

1011 1101

OR is pretty much how bits are set, in general. While I'm on the topic, in case you don't know, AND works as a bit mask. ANDing something with a 1 leaves it unchanged, but ANDing it with 0 sets it to 0. Knowing those uses of AND and OR are very important, so I'm just mentioning it in case you didn't know or forgot.

You've been told about the register containing the number in which you want to set the bits. The ORI instruction has this syntax:

ori $t, $s, imm

The source ($s) you want is $v0, as is the target ($t). The immediate part should be just the number with only the bits you wish to make into 1s set to 1.

P.S. Not sure what you mean by "like an ex"
 
You couldn't simply just or it, because that would only allow you to change the lower 16 bits. By using the lui into another temp, it allows you to manipulate bits 16-31.

also: I think by "like an ex" he meant "like an example."
 
Tyzall said:
You couldn't simply just or it, because that would only allow you to change the lower 16 bits. By using the lui into another temp, it allows you to manipulate bits 15-31.
I totally missed that. Thanks, nice catch.
 
Thank You Guys for all of the help! i see how simple it is now... i totally forgot about lui lol it slipped right passed me, and i definitely didn't know about masking! i will read up on it more!

THANK YOU SOOOOOO SOOOOO SOOOOO SOOOO MUCH!
 
Oh! and guys how could i possibly go about having that same register $v0, set bits 31 and 30 to 0 and 1 respectively. Additionally, set bits 4 and 5 to 1 and 0 respectively.

In six steps ? should i lui again to manipulate 16-31?
 
  • #10
Hahahah... This is quite ironic, I had the exact same homework questions...
 
  • #11
Well i know that before $v0 = 0b1000 0000 0000 0000 0000 0000 0010 1111
and after $v0 = 0b0100 0000 0000 0000 0000 0000 0001 1111

so would i just lui for the 32 bits and then ori ? for the rest
 
  • #12
No, it's a lot different than the first.

lui allows you to modify the upper 16 bits, but makes the lower 16 bits 0.
 
  • #13
hmmm okay... ill try to figure it out
 
  • #14
Basketball,
You must have the same homework as me. Not only do I have the same two problems, but it's due today. I'm still trying to figure out the last one.
 
  • #15
basketball853 said:
Oh! and guys how could i possibly go about having that same register $v0, set bits 31 and 30 to 0 and 1 respectively. Additionally, set bits 4 and 5 to 1 and 0 respectively.

In six steps ? should i lui again to manipulate 16-31?

The usual terminology is that you are setting bits 30 and 4, and clearing bits 5 and 31. Setting a bit means putting a 1 there. Clearing a bit means putting a 0 there.

To set a bit, OR the register with an immediate value with a 1 in the right position. To clear a bit, AND the register with an immediate value with a 0 in the position.

I'll leave you to work out the details of working with the lower and upper 16 bits.
 
  • #16
ok so in order to acomplish this i simply wrote out 32 bits...
0000 0000 0000 0000 0000 0000 0000 0000
and do i count from right to left with 0 ? soo that being
0100 0000 0000 0000 0000 0000 0001 0000
right?
if this is the case then can't i do:
lui $t1, 0x4000
ori $t1, 0x0010

and that's it ?

it seems easy enough? but i know that's incorrect because it asks for 6 steps
 
  • #17
basketball853 said:
if this is the case then can't i do:
lui $t1, 0x4000
ori $t1, 0x0010

and that's it ?

it seems easy enough? but i know that's incorrect because it asks for 6 steps

Nope, not quite that simple. Also, the format of your ORI instruction is wrong. Ok, you need to use an OR type operation to set bits to 1, right? And you need an AND type instruction to set bits to 0.

For the high part, you'll need 2 instructions for the ORing and 2 instructions for the ANDing since you must use LUI, so that's 4 instructions. Then you need to do so on the lower 16 bits, which requires 1 instruction for ORing and 1 for ANDing, since you don't need to LUI anything, but can use immediate mode. That's 6 instructions.

You already know how to set bits to 1, I believe. So how would you go about setting bits to 0 with AND type instructions?
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
Replies
3
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
9
Views
30K