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

In summary: Remember, ANDing something with a 1 leaves it unchanged, but ANDing it with 0 sets it to 0, and it's the same for the destination register.
  • #1
basketball853
18
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
  • #2
Would perhaps the ORI instruction be of use?
 
  • #3
how could i implement that though? good suggestion ... like an ex..
 
  • #4
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 :)
 
  • #5
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"
 
  • #6
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."
 
  • #7
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.
 
  • #8
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!
 
  • #9
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?
 

What does it mean to "set bits 18, 19, 20, 21 to 1" in MIPS?

Setting bits 18, 19, 20, 21 to 1 in MIPS means that we are changing the binary representation of a specific register or memory location by turning on those specific bits. This is typically done in order to store a specific value or perform a certain operation.

How do I set bits 18, 19, 20, 21 to 1 in MIPS?

To set bits 18, 19, 20, 21 to 1 in MIPS, you will need to use a bitwise OR operation. This operation will combine the current value of the register or memory location with a binary number that has 1s in the 18, 19, 20, and 21 positions. This will effectively turn on those bits without affecting any other bits.

Why would I need to set bits 18, 19, 20, 21 to 1 in MIPS?

There are many reasons why you may need to set bits 18, 19, 20, 21 to 1 in MIPS. One common reason is for setting control bits that determine the behavior of a specific instruction or operation. These bits may need to be turned on in order for the instruction to function correctly.

What happens if I set bits 18, 19, 20, 21 to 1 in MIPS and they were already 1?

If the bits were already 1, setting them again will not have any effect on the value or behavior of the register or memory location. This is because the bitwise OR operation will only turn on bits that are currently off. If you want to ensure that these bits are set to 1, you can use a bitwise AND operation to clear any other bits and then use a bitwise OR to set the desired bits.

Is there a way to set bits 18, 19, 20, 21 to 1 in MIPS without using a bitwise operation?

No, there is not a way to set specific bits in MIPS without using a bitwise operation. This is because the MIPS architecture does not have a dedicated instruction for setting individual bits. However, there may be higher-level languages or libraries that provide functions or methods for setting specific bits without directly using bitwise operations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
Back
Top