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

## 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.

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 ?

Related Engineering and Comp Sci Homework Help 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 :)

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." 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?

Hahahah... This is quite ironic, I had the exact same homework questions...

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

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.

hmmm okay... ill try to figure it out

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.

Mark44
Mentor
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. 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 thats it ? it seems easy enough? but i know thats incorrect because it asks for 6 steps if this is the case then can't i do: lui$t1, 0x4000
ori \$t1, 0x0010

and thats it ?

it seems easy enough? but i know thats 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?