0

My code consists in obtaining the power of a number using only 8-bit registers for the base and the exponent, but giving a result in 16 bts ( my teacher wants it that way) , my problem is that when for example I calculate 3 ^ 10, after obtaining 729, the following result would have to be 2187 but instead the result is 651. I don't know how to solve this.

   org 100h

   mov cl, pot
   mov al, bas
   mov ah, 0
   mov bh, 0
   mov ch, 0


   loopmul:
   mov bl,bas
   mul bl
   loop loopmul

   mov res,ax
   ret

   pot db 10  ;exponent
   bas db 3  ;base
   res dw 0
halfer
  • 19,824
  • 17
  • 99
  • 186
MERSP
  • 1
  • 1
  • You need to extend the 8-bit value to 16 bits before multiplying (by setting bh to 0) and then use a 16-bit multiply (“mul bx”). But if the assignment is to not use 16-bit registers at all, then you need to implement a multiple precision multiply, which is much more involved. – prl Oct 17 '19 at 19:43
  • Here’s a reference for multi-precision multiply: http://www.plantation-productions.com/Webster/www.artofasm.com/Windows/HTML/AdvancedArithmetica2.html#100761 from an answer to this question: https://stackoverflow.com/questions/87771/how-can-i-multiply-two-64-bit-numbers-using-x86-assembly-language – prl Oct 17 '19 at 19:50
  • @prl: if you use an O(log N) algorithm where the last multiply is probably squaring, the inputs to that only need to be 8-bit if the result fits in 16. Only with unequal inputs does truncating the inputs to 8-bit cause a problem when the correct total wouldn't overflow 16 bits. e.g. 3^10 = `(3^5) ^ 2`. So it's like multiplying by shift-and-add, except you exponentiate by shift-and-mul. – Peter Cordes Oct 17 '19 at 19:59

0 Answers0