If I write code like lea eax, [2 * eax]; eax * 2
lea eax, [2 * eax]; eax * 2
is the same as shl eax, 1; eax * 2
shl eax, 1; eax * 2
My question is:
shr
instruction? like lea eax, [2... eax]
?You can't emulate a right shift in terms of a left shift, sorry. (The scale factor in x86 addressing modes is encoded in machine code as a 2-bit shift count from 0 to 3.)
Also note that LEA doesn't set FLAGS, SHL does.
AFAIK there are no tricks, not even inefficient ones. This problem has come up before for toy ISAs without a right shift; you typically need something like a lookup table or a way to shift bits from one register into another, like add eax,eax
/ adc edx,edx
to move a bit from the top of EAX to the bottom of EDX. Starting with EDX=0 and repeating that in a loop for the right number of iterations (32-n or something) will give the same result as a right shift by n
.
SHR / SAR are pretty necessary primitive operations that can't easily be emulated in terms of anything else. (Except for right-rotate + zeroing high bits by generating an AND mask.)
Right shift is division by a power of 2. Division is hard; unlike left-shift / multiplication, you can't build it out of other things easily. eg add eax,eax
shifts left by 1 because carry propagates from low to high in addition.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.