0

So i have two variables a and b and I need to raise a to the power b in assembly code.

I know I can do this by multiplying a by b times in a loop (it only needs to work on 16 bits) but I dont know how to do it with variables.

Thanks in advance :)

  • Looping a variable number of times is about the same as looping a constant number of times. – Peter Cordes Jan 14 '22 at 17:21
  • [function Power(x,y) in assembly ia32 max 32 bit](https://stackoverflow.com/q/53341606) has a 32-bit implementation using inputs in registers. ( You could obviously load your inputs into registers to start with.) It uses `cmov` and IDK if TASM supports a `.686` option to enable PPro instructions, otherwise you'd have to port it to use a branch. – Peter Cordes Jan 14 '22 at 17:31
  • @PeterCordes Ok but whats the difference when u loop a variable and when u loop a constant, because if I try to do it the same way as I would loop a constant it gives me an error. – David Pupaza Jan 14 '22 at 22:16
  • Then you're doing it wrong :P but you haven't shown how you tried to do it. A normal loop with `mov si, var` ahead of the loop, and `dec si` / `jnz looptop` at the bottom, works fine whether for a variable, another register, or an immediate constant. Pick any convenient register, doesn't have to be `si`. – Peter Cordes Jan 14 '22 at 22:19
  • @PeterCordes So this is how i tried to do it `.DATA a db 3,4 b db 3,4 .CODE START: mov ah, 0ah mov dx, offset a ;a int 21h mov ah, 0ah mov dx, offset b ;b int 21h add bl,a add cl,b mov bx,'5' ; a mov cx,'2' ; b mov ax,'1' ; Initial result, i.e. m^0 power: jcxz power_done mul bx loop power power_done: ; ax now contains m^n` And the error I get is Operand types do not match Sorry I cant make it look ok on the website – David Pupaza Jan 15 '22 at 22:42
  • [edit] your question if you want to post an attempt and turn this into a debugging question, with a [mcve] including which line produced that error. (You don't need `jcxz` *inside* a loop, only optionally ahead of the loop. That's not a correctness problem, though, just performance.) – Peter Cordes Jan 16 '22 at 01:13

0 Answers0