bit manipulation - How do I manipulate bits in Assembly language MIPS32 -
i having little bit of trouble figuring out 1st 2 parts "xxxxxxx" have
add $t0, $v0, $0 # $t0 gets copy of input x sub $t1, $0, $t0 # $t1 gets mask1 "-x"
but rest confused. not exact answer explanation immensely!
update:
i able make work. here solution. thank you!
**move $t0, $v0** # $t0 gets copy of input x **sub $t1, $zero, $t0** # $t1 gets mask1 "-x" li $v0, 1 move $a0, $t0 syscall li $v0, 4 la $a0, outlab1 syscall # print output label 1 li $v0, 1 **and $a0, $t0, $t1** # $a0 gets "all bits of x cleared except rightmost 1" syscall not $t2, $a0 # $t2 gets mask2 "$a0 bits toggled" li $v0, 4 la $a0, outlab2 syscall # print output label 2 li $v0, 1 **and $a0, $t0, $t2** # $a0 gets "all bits of x rightmost 1 cleared" syscall li $v0, 10 # exit syscall
xxxxxxxxxxxxx #$a0 gets "all bits of x cleared except rightmost 1"
check how two's complement works, , how value x
looks (bitwise) both in t0
, t1
(-x). there's 1 basic bitwise operation, when applied x
, -x
produce 1 bit set (special case 0x80000000
(-2147483648) input, negated value out of 32 bit signed integer range, +2147483648 again 0x80000000 (in 32 bit unsigned integer), i.e. collides -2147483648 representation of 32b signed integers. that's why 32b int
has range -2147483648 +2147483647
only).
xxxxxxxxxxxxx # $t2 gets mask2 "$a0 bits toggled"
well, toggle each bit? (as don't mips programming, i'm not sure instruction can used on mips, on x86 not
do). check through mips instruction set, bitwise operations probably. on cpus without kind of built-in not can using xor
(sometimes called eor
) , constant bits set (-1
or ~0
in c, in assemblers -1 works too). think on risc cpus 0 register can flipped + used in single instruction, source of -1
too. i'm adding sh*t show you, need bit creative in assembly, being aware of instructions , values in registers, take shortcut particular arithmetic result.
xxxxxxxxxxxx # $a0 gets "all bits of x rightmost 1 cleared"
now ask clear rightmost 1 , keep other bits intact, of original value x
. don't see how 1 without revealing solution instantly, it's absolutely trivial, if think t0
, t1
, t2
contains @ point.
maybe have problem see values in binary form, doesn't "jump @ you" how these things works? try calculator, can display binary form too, , check calculations/values see particular bits (switch , forth between decimal, hexadecimal , binary, hex<->bin nice understand, can "see" particular bits in head reading hexadecimal formatting of value), re-read description of basic bitwise operations (and, or, xor
), , task.
Comments
Post a Comment