问题描述
我想对Objective-C程序中的变量执行ROR和ROL操作.但是,我无法管理它-我不是组装专家.
I would like to perform ROR and ROL operations on variables in an Objective-C program. However, I can't manage it – I am not an assembly expert.
这是我到目前为止所做的:
Here is what I have done so far:
uint8_t v1 = ....;
uint8_t v2 = ....; // v2 is either 1, 2, 3, 4 or 5
asm("ROR v1, v2");
我得到的错误是:
我该如何解决?
该代码不需要使用内联汇编.但是,我还没有找到使用Objective-C/C ++/C指令执行此操作的方法.
The code does not need to use inline assembly. However, I haven't found a way to do this using Objective-C / C++ / C instructions.
推荐答案
要在标准C语言中执行此操作,您可以执行以下操作:
To do this in standard C, you can do:
var = (var << shift) | (var >> (sizeof(var)*CHAR_BIT-shift))
大多数编译器都会识别该模式并将其优化为一条指令(如果目标支持的话).
Most compilers will recognise that pattern and optimise it to a single instruction (if the target supports it) anyway.
您可以在此处了解更多信息: http://en.wikipedia.org/wiki/Circular_shift# Implementing_circular_shifts
You can read more here: http://en.wikipedia.org/wiki/Circular_shift#Implementing_circular_shifts
这篇关于在Objective-C中使用内联汇编对变量进行ROL/ROR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!