I want to calculate a sequence of exponents of base 2. for example if given 3 the program would calculate (2^3 + 2^2 + 2^1 + 2^0). The problem is I cant calculate the exponent to being with.
I have tried shl
to calculate the exponent and I have tried a loop. Neither produce the correct result, my latest attempt is below.
.586
.MODEL FLAT
INCLUDE io.h
.STACK 4096
.DATA
Exponent DWORD ?
Prompt BYTE "Enter an exponent", 0
Base DWORD 2
string BYTE 40 DUP (?)
resultLbl BYTE "The solution is", 0
Solution DWORD 20 DUP (?), 0
.CODE
_MainProc PROC
input Prompt, string, 40 ; read ASCII characters (N)
atod string ; ascii to double
mov exponent, eax ; store in memory
push Exponent
push Base
call function
add esp, 8
dtoa Solution, eax ; convert to ASCII characters
output resultLbl, Solution ; output label and sum
mov eax, 0 ; exit with return code 0
ret
_MainProc ENDP
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Procedure takes base and exponent from main
Function PROC
push ebp ; stores base pointer
mov ebp, esp ; set base pointer to current position
mov ebx, [ebp + 12] ; read base (2)
mov ecx, [ebp + 8] ; read N
MainLoop: ; loop to calculate expoent
mul ebx ; multiply base by its self
add eax, ebx ; save answer in eax
dec ecx ; decrement the exponent
cmp ecx, 0 ; check if exponent is 0 yet
je exit ; Exit if exponent is 0
jg mainloop ; stay in loop if exponent is above 0
Exit:
pop ebp ; restore base pointer
ret
function ENDP
END
I want the program to produce the correct exponent result, and if you can calculate the sequence above, for example if given 3 the program would calculate (2^3 + 2^2 + 2^1 + 2^0).
mul
works. Hint: it puts result ineax
(andedx
) so youradd eax, ebx
is pointless. Pick a different target register and addeax
to it. Also you forgot to initializeeax
so you are just multiplying some random number. PS: learn to use a debugger. – Epner