-3

Does anybody know how to find the power of a value in x86 assembly,

Is it possible to shift by the number of times in a value?

For example:

  mov x, 30 

  shl eax, x

For my example I would like to find 2^32,2^31,2^30.....2^0.

Any help would be much appreciated.

Thank you.

Alex
  • 47
  • 1
  • 9
  • http://stackoverflow.com/questions/12680303/power-of-in-x86-assembly , http://stackoverflow.com/questions/23412780/power-of-2-with-shr-and-shl-assembly – Jose Manuel Abarca Rodríguez Jan 09 '17 at 13:51
  • To find powers of two, just use bitwise shifts. – Jester Jan 09 '17 at 13:52
  • 1
    Possible duplicate of [power of in x86 assembly](http://stackoverflow.com/questions/12680303/power-of-in-x86-assembly) – Jose Manuel Abarca Rodríguez Jan 09 '17 at 13:53
  • is a bitwise shift, shr and shl? thanks – Alex Jan 09 '17 at 14:05
  • hi, i have read them before i asked this question, i cannot simply shl ax,13 as i need to update the value i'm shifting by so it would have to be like shl ax,value but i am not allowed to do this in x86 assembly – Alex Jan 09 '17 at 14:12
  • 2
    Do you mean something like `shl ax, cl`? You can use `CL` to hold a shift amount and use it to shift. – Margaret Bloom Jan 09 '17 at 14:26
  • Your question is not clear. Please incorporate some of the things you've said in response to clarification-seeking comments into your question. – Cody Gray - on strike Jan 09 '17 at 14:43
  • Also always make sure to read instruction description when not sure about its details. For example `shl r/m,#imm8/cl` will mask the shift-count to only 5 bits, so it's doing `shl eax, (cl mod 32)` (as shifting by 32 on 32b register doesn't make much sense anyway). – Ped7g Jan 09 '17 at 15:04
  • yes Margaret bloom, this is exactly what im looking for! thank you very much. Apologizes for the poor wording of my question, I am a newbie to x86. – Alex Jan 09 '17 at 15:27
  • is it possible to move a dword into CL? I would like to keep a counter dword, the maximum value it will have is 32. – Alex Jan 09 '17 at 15:35
  • @Alex Just move the least significant byte into `cl` or overwrite all of `ecx`. – fuz Jan 09 '17 at 17:06

1 Answers1

-3

with a shx instruction you can only use 1 because rapresent the moviment of bit.

for shl you put out last bit on the left and insert a bit on the right. for this you multiplay a byte per 2.

while shr you put out last bit on the right and insert a bit on the left. for this you divide a byte per 2.

so if you want storage a powr you must move a calculate data into other register. example 32bit assembler: 2^32

$ mov eax,2

mov ecx,number of power

xor ebx,ebx

loop_power: shl eax,1

add ebx,eax

loop loop_power $ you ha power of 2 in ebx

  • That is not true. x86 has an instruction to shift by a variable amount of bits. One such instruction is e.g. `shl r/m32,cl`. – fuz Jan 09 '17 at 17:05