[英]What do andi and ori do in this program?
.global main # makes label "main" globally known
.text # Instructions follow
.align 2 # Align instructions to 4-byte words
main: movi r16,0x47 # Load the hexadecimal value 41
# to register r16
loop: mov r4,r16 # Copy to r4 from r16
nop # (later changed to call hexasc)
nop # (later changed to mov r4,r2)
movia r8,putchar # copy subroutine address to a register
callr r8 # call subroutine via register
addi r16, r16,1 # Add 1 to register r16
andi r16, r16, 0x7f # mask with 7 bits
ori r16, r16, 0x20 # set a bit to avoid control chars
br loop # Branch to loop
.end # The assembler will stop reading here
foo bar bletch # comes after .end - ignored
I can understand everything, I think, except how the two instructions andi
and ori
work in this case. 我想,除了两个指令
andi
和ori
在这种情况下如何工作之外,我能理解一切。 ori appears to make so that the ASCII 20 positions forward is printed but why and how? ori似乎使得ASCII 20位置向前打印,但为什么以及如何?
andi
with 0x7f removes the most significant bit, which is not used by ASCII (which only uses 7 bits, or 0-128 to maps characters). 带有0x7f的
andi
删除最高有效位,ASCII不使用它(仅使用7位,或0-128来映射字符)。 0x7f is 0111 1111 in binary. 0x7f是二进制的0111 1111。 Since anything AND with 0 (the most significant bit is 0, as you can see) is 0, anything AND with 1 stays the same, the operation removes the most significant bit.
由于任何AND与0(最高有效位为0,如您所见)为0,任何AND与1保持不变,操作将删除最高有效位。
ori
with 0x20 will only set the 6th bit (2 5 ). 带有0x20的
ori
只会设置第6位(2 5 )。 0x20 is 0010 0000 in binary. 0x20是二进制的0010 0000。 Since anything OR with 1 (the 6th bit, as you can see) is 1, and anything OR with 0 stays the same, it results in setting the 6th bit.
因为任何OR与1(第6位,如你所见)是1,任何OR与0保持相同,它导致设置第6位。
As the comment says, just in case r16 originally is less than 32, or in case r16 >= 128 and < 160, it will make the number >= 0x20. 正如评论所说,只是在r16最初小于32的情况下,或者在r16> = 128且<160的情况下,它将使数字> = 0x20。 It doesn't mean that it will always add 0x20, though (eg r16 originally is 32 --addi--> 33 --andi--> 33 --ori--> 33).
但这并不意味着它总是会添加0x20(例如r16最初是32 --addi - > 33 --andi - > 33 --ori - > 33)。
As side note, AND'ing with a constant (which is also called mask ) is usually for extracting certain bit out of the original data. 作为旁注,与常量(也称为掩码 )的AND'通常用于从原始数据中提取某些位。 And it will extract at whichever bit that the corresponding bit in the mask is 1.
并且它将提取掩码中相应位为1的任何位。
OR'ing with constant is usually for set certain bit(s) to 1. It will set the bit whose corresponding bit in the mask is 1. 与常数进行“或”运算通常是将某些位设置为1.它将设置掩码中相应位为1的位。
By the way, to set certain bit to 0, you can AND with constant that is all 1 except the bit that you want to set. 顺便说一下,要将某个位设置为0,除了要设置的位之外,您可以使用常量除1之外的常量。 To toggle certain bits, you can XOR with the constant with corresponding bit 1 and the rest 0.
要切换某些位,可以使用相应位1和其余0的常量进行异或。
Using and
in assembly is a way to ensure a bit is off. 使用
and
组装是一种确保关闭的方法。 Consider: 考虑:
1101 and
0111
---------
0101
Only in the second and the last columns do the 1s exist in the top AND
the bottom, effectively ensuring that the first bit is turned off. 仅在第二列和最后一列中,1s存在于顶部
AND
底部,有效地确保第一位被关闭。
Using 'or' in assembly is a way to ensure that a bit is on. 在汇编中使用'或'是确保某个位开启的一种方法。 consider:
考虑:
1101 or
0111
----------
1111
The 1s on the bottom row ensure that no matter what the first number is, the last three bits will be on. 底行的1s确保无论第一个数字是什么,最后三位都将打开。
andi and ori are both bitwise operators: andi和ori都是按位运算符:
To see the difference, concider" 为了看到差异,concider“
and $rd, $rs, $rt
or $rd, $rs, $rt
versus 与
andi $rt, $rs, immed
ori $rt, $rs, immed
http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/bitwise.html http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/bitwise.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.