0

I am working on an assignment for a class, and the instructor has been about as clear as mud about how we are supposed to "clear a bit." The assignement is:

"clr0 takes its parameter value, clears bit zero in that value, and returns the result. e.g. 1011 becomes 1010"

I have tried:

 clr0:
   andi $v0, $a0, 0
   jr $ra

But the value does not set the 0th bit to 0. What am I doing wrong?

Michael
  • 57,169
  • 9
  • 80
  • 125
tomkin
  • 11
  • 1
  • 2
  • you're clearing the whole `$a0` [How do you set, clear and toggle a single bit in C/C++?](http://stackoverflow.com/q/47981/995714) – phuclv Apr 28 '15 at 14:54

3 Answers3

0

Let's look at what and does. From Wikipedia:

and $d,$s,$t    $d = $s & $t

So $v in your code will be set to $a0 ANDed with 0. Well, anything ANDed with 0 is 0. So if you have 32 bit registers, wouldn't you want something like

clr0:   lui $a1, 65535 # start constructing the constant, so upper bits of v1 are all 1
        ori $a1, $a1, 65534 # finish constructing the constant, so that only 0th bit is 0

        and $v0, $a0, $a1 #mask out the 0th bit of a0

        jr $ra

That would mean that all of the bits, except for the 0th bit, are left as is, and the 0th bit is forced to 0.

I just edited this to take into account that andi only takes a 16 bit constant - so first we construct the constant in a free register, then AND it. I don't have a compiler handy, nor do I remember which MIPS registers are free for the function to party on, but something very similar to this should get the job done.

And here's a small driver program to call clr0

main:   li $a0, 5
        and $v0, $v0, 0
        jal clr0 #after this, investigate the value of $v0.  

        jr $ra          # retrun to caller
dsolimano
  • 8,870
  • 3
  • 48
  • 63
0
li   $t0, 1         # $t0 = 0x00000001   
not  $t0, $t0       # $t0 = 0xfffffffe
and  $v0, $a0, $t0  # clear bit 0 of $v0

The first two instructions set all bits in $t0 to 1, except bit 0. And-ing a bit with 1 leaves that bit unchanged. And-ing a bit with 0 sets that bit to 0. Thus all bits of $a0 are moved to $v0 unchanged, except for bit 0 which is set to 0.

markgz
  • 6,054
  • 1
  • 19
  • 41
0
#Here is the program
#A Program to clear bit. Suppose we have 11 = 1011 (in binary) & 
#when we apply bit manipulation for a clearing last bit, 
#it changes from 1011 to 1010 #that is 10(decimal)
#To do this we need a mask. 
.data
.text
main:
    li $v0, 5
    syscall
    move $a0, $v0
    jal bitmanipulate

#when bitmanipulate function ends, the $v0 still have 
#the result of the   bitmanipulate.
    move $s0, $v0
    li $v0, 1
    move $a0, $s0
    syscall

    li $v0, 10          #terminating the program      
    syscall

    bitmanipulate:
    #making mask, following two line is the making of mask.
        addi $s0, $zero, -1         #s0 = -1, so it will be 
    #presented as ...11111111 (32 times 1, if presenting number in 32 bit)
        sll $s0, $s0, 1             #now $s0 = ...11111110, 
        #shifting one bit to left

        and $v0, $a0, $s0           #
        #$v0 store the result of the function 
        jr $ra
Yash
  • 1,787
  • 1
  • 22
  • 23
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yivi Mar 20 '18 at 15:20
  • Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written – WhatsThePoint Mar 20 '18 at 15:22
  • Thanks for such beautiful tips. – Yash Mar 22 '18 at 04:49