NASM shift operators

 ̄綄美尐妖づ 提交于 2019-12-10 15:14:55

问题


How would you go about doing a bit shift in NASM on a register? I read the manual and it only seems to mention these operators >>, <<. When I try to use them NASM complains about the shift operator working on scalar values. Can you explain what a scalar value is and give an example of how to use >> and <<. Also, I thought there were a shr or shl operators. If they do exist can you give an example of how to use them? Thank you for your time.


回答1:


<< and >> are for use with integer constants only. This is what it means by "scalar value". You can shift the value in a register using the shl or shr instructions. They are used to shift the value in a register left or right, respectively, a given number of bits.

The first line in this example shifts the value in ax left 4 bits, which is the same as multiplying it by 16. The second line shifts the value in bx right by 2 bits, which is the same as integer division by 4.

shl ax, 4
shr bx, 2

You can also use cl to indicate the number of bits to shift, instead of a constant. For more information on these and related instructions, see this page.




回答2:


Piggy-backing on ughoavgfhw's answer... to use << and >>, use them directly on constants:

MOV EAX, 1 << 2    ; Puts 4 into EAX
MOV EAX, 2 << 2    ; Puts 8 into EAX
MOV EAX, 8 >> 1    ; Puts 4 into EAX


来源:https://stackoverflow.com/questions/9960476/nasm-shift-operators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!