简体   繁体   English

如何处理汇编语言MIPS32中的位

[英]How do I manipulate bits in Assembly language MIPS32

I am having a little bit of trouble figuring this out for the 1st two parts with the "xxxxxxx" I have 我在用“ xxxxxxx”解决第一部分时遇到了一些麻烦

add $t0, $v0, $0        # $t0 gets copy of input x
sub $t1, $0, $t0        # $t1 gets mask1 that is "-x"

but the rest I am confused. 但是其余的我感到困惑。 I do not want an exact answer but an explanation will help immensely! 我不想要确切的答案,但是一个解释将极大地帮助您!

UPDATE: 更新:

I was able to make it work. 我能够使它工作。 Here is the solution. 这是解决方案。 Thank you! 谢谢!

**move $t0, $v0**           # $t0 gets copy of input x
**sub $t1, $zero, $t0**     # $t1 gets mask1 that is "-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 the 
                         rightmost 1"
syscall

not $t2, $a0           # $t2 gets mask2 that is "$a0 with all its 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 with the rightmost 1 
                           cleared"
syscall

li $v0, 10               # exit
syscall
    xxxxxxxxxxxxx   #$a0 gets "all bits of x cleared except the rightmost 1"

Check how two's complement works, and how the value x looks (bitwise) both in t0 and t1 (-x). 检查二进制补码的工作原理,以及值xt0t1 (-x)中的样子(按位)。 There's one very basic bitwise operation, which when applied to x and -x will produce exactly just 1 bit set (special case is the 0x80000000 (-2147483648) input, for which the negated value is out of 32 bit signed integer range, the +2147483648 is again 0x80000000 (in 32 bit unsigned integer), ie collides with -2147483648 representation of 32b signed integers. That's why 32b int has range -2147483648 to +2147483647 only). 有一种非常基本的按位运算,将其应用于x-x将只产生1位设置(特殊情况是0x80000000 (-2147483648)输入),其取反值超出了32位有符号整数范围,即+ 2147483648还是0x80000000(32位无符号整数),即与-32147带符号的32b有符号整数表示冲突。这就是为什么32b int范围为-2147483648 to +2147483647

    xxxxxxxxxxxxx   # $t2 gets mask2 that is "$a0 with all its bits toggled"

Well, just toggle each bit? 好吧,只是切换每一位? (As I don't do MIPS programming, I'm not sure which instruction can be used on MIPS, on x86 the NOT would do). (由于我不进行MIPS编程,所以我不确定可以在MIPS上使用哪条指令,在x86上, NOT )。 So check through the MIPS instruction set, bitwise operations probably. 因此,检查一下MIPS指令集,可能是按位运算。 On CPUs without some kind of built-in NOT you can do this by using xor (sometimes called eor ) and the constant with all bits set ( -1 or ~0 in C, in assemblers usually -1 works too). 在没有某种内置不是你可以用做CPU的xor (有时被称为eor ),并与所有位恒置( -1~0通常是C,在装配-1工作太)。 I think on some RISC CPUs the zero register can be flipped + used in single instruction, as source of -1 too. 我认为在某些RISC CPU上,零寄存器可以被翻转+用于单个指令中,也可以作为-1源。 I'm adding this sh*t just to show you, that you need to be a bit creative in assembly, being aware of all instructions and values in registers, to take some shortcut for some particular arithmetic result. 我添加此代码只是为了向您展示,您需要在汇编方面有点创新,知道寄存器中的所有指令和值,以便为某些特定的算术结果提供一些捷径。

    xxxxxxxxxxxx    # $a0 gets "all bits of x with the rightmost 1 cleared"

Now they ask you to clear the rightmost 1 and keep all the other bits intact, of original value x . 现在他们要求您清除最右边的1并保持其他所有原始值x不变。 I don't see how to help you with this one without revealing the solution instantly, as it's absolutely trivial, if you think about what your t0 , t1 and t2 contains at this point. 如果您考虑一下此时的t0t1t2包含的内容,那是非常琐碎的,我看不到如何在不立即揭示解决方案的情况下为您提供帮助。

Maybe you have problem to see those values in binary form, so it doesn't "jump at you" how these things works? 也许您很难以二进制形式查看这些值,所以它不会“跳到您身上”这些东西是如何工作的? Try some calculator, which can display binary form too, and check the calculations/values to see particular bits (switch back and forth between decimal, hexadecimal and binary, especially the hex<->bin is nice to understand, then you can "see" the particular bits in head just by reading the hexadecimal formatting of value), then re-read the description of basic bitwise operations ( and, or, xor ), and get back to this task. 尝试一些也可以显示二进制形式的计算器,并检查计算/值以查看特定的位(在十进制,十六进制和二进制之间来回切换,特别是hex <-> bin很容易理解,那么您可以“查看只需先读取值的十六进制格式即可,然后重新读取基本按位运算的描述( and, or, xor ),然后返回此任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM