Search code examples
assemblyx86masmirvine32exponentiation

x86 Assembly - finding powers using addition


The program will accept two numbers from the user and display the sum, product, and power (a^b) of those two numbers. Here is the catch, however...

The program MUST: Use an AddNumbers function Use that AddNumbers function in a MultiplyNumbers function Use that MultiplyNumbers function in a CalculatePower function

I cannot figure out how to find the Product of the two numbers by implementing the multiplication through addition function. I cannot grasp the logic behind it. I'm obviously doing something wrong, but I'm not sure what. (Please ignore the inefficiency of the code, I just want to know what I'm doing wrong as far as finding the product of the two)

My code so far..

INCLUDE Irvine32.inc
.data
str1 BYTE "Enter a positive integer: ",0
str2 BYTE "The sum is: ",0
str3 BYTE "The product is: ",0
str4 BYTE "The power result is: ",0

.code
main PROC

call GetInteger
call Crlf
mov eax, ebx
call AddNumbers

mov edx, OFFSET str2
call WriteString
call WriteInt
call Crlf
mov eax, 0
mov ecx, edi
call MultiplyNumber
mov eax, edx
mov edx, OFFSET str3
call WriteString
call WriteInt
call Crlf
call CalculatePower
mov eax, esi
mov edx, OFFSET str4
call WriteString
call WriteInt
call Crlf

exit
main ENDP

GetInteger PROC 
mov edx, OFFSET str1
call WriteString
call ReadInt
mov ebx, eax
call WriteString
call ReadInt
mov edi, eax
ret
GetInteger ENDP

CalculatePower PROC USES edi ebx 

mov ecx, edi
mov esi, 0
L2:

    call MultiplyNumber
    add esi, edx
    loop L2
    ret
CalculatePower ENDP

MultiplyNumber PROC USES ebx edi ecx

mov edx, 0
L1:
    mov edi, ebx
    mov eax, 0
    call AddNumbers
    add edx, eax
    loop L1
    ret
MultiplyNumber ENDP

AddNumbers PROC 

    add eax, edi

ret
AddNumbers ENDP

END main

Solution

  • Try this one. Your code is complicated to analyze because you have different loops in different procedures without initializing ecx again etc.

    OPTION CASEMAP:NONE
    
    INCLUDE Irvine32.inc
    
    ExitProcess proto, dwExitCode:dword
    
    .data
        str1 BYTE "Enter a positive integer: ",0
        str2 BYTE "The sum is: ",0
        str3 BYTE "The product is: ",0
        str4 BYTE "The power result is: ",0
        num1 DWORD 0
        num2 DWORD 0
        sum  DWORD 0
        prod DWORD 0
        pow  DWORD 0
        tmp  DWORD 0
    
    .code
        main PROC
    
            mov edx, OFFSET str1    ;// Input
            call WriteString
            call ReadInt
            mov [num1], eax
            mov edx, OFFSET str1
            call WriteString
            call ReadInt
            mov [num2], eax
    
            call doSum              ;// Calculations
            call doMul
            call doPow
    
            mov edx, OFFSET str2    ;// Output
            call WriteString
            mov eax, [sum]
            call WriteInt
            call Crlf
    
            mov edx, OFFSET str3
            call WriteString
            mov eax, [prod]
            call WriteInt
            call Crlf
    
            mov edx, OFFSET str4
            call WriteString
            mov eax, [pow]
            call WriteInt
            call Crlf
    
            invoke ExitProcess, 0
    
        main ENDP
    
        doSum PROC              ;// Sum
            mov eax, [num1]
            add eax, [num2]
            mov [sum], eax
            ret
        doSum ENDP
    
        doMul PROC              ;// Multiply: Add num1 x num2 times
            xor eax, eax
            mov ecx, [num2]
            ADD_LOOP:
                add eax, [num1]
            loop ADD_LOOP
            mov [prod], eax
            ret
        doMul ENDP
    
        doPow PROC              ;// Power: Add num1 x num2 times and 
            mov eax, [num1]     ;// add result x num2 times till ebx=0
            mov [tmp], eax
            mov ebx, [num2]
            dec ebx
            POW_LOOP:
                xor eax, eax
                mov ecx, [num1]
                ADDPOW_LOOP:
                    add eax, [tmp]
                loop ADDPOW_LOOP
                mov [tmp], eax
                dec ebx
            jnz POW_LOOP
            mov [pow], eax
            ret
    
        doPow ENDP
    
    END main
    

    Modified version with call of addition proc from multiplication and power proc:

    OPTION CASEMAP:NONE
    
    INCLUDE Irvine32.inc
    
    ExitProcess proto, dwExitCode:dword
    
    .data
        str1 BYTE "Enter a positive integer: ",0
        str2 BYTE "The sum is: ",0
        str3 BYTE "The product is: ",0
        str4 BYTE "The power result is: ",0
        num1 DWORD 0
        num2 DWORD 0
        sum  DWORD 0
        prod DWORD 0
        pow  DWORD 0
        tmp  DWORD 0
    
    .code
        main PROC
    
            mov edx, OFFSET str1    ;// Input
            call WriteString
            call ReadInt
            mov [num1], eax
            mov edx, OFFSET str1
            call WriteString
            call ReadInt
            mov [num2], eax
    
            mov eax, [num1]         ;// Calculations
            mov ebx, [num2]
            mov ecx, 1
            call doSum          
            mov [sum], eax
            call doMul
            call doPow
    
            mov edx, OFFSET str2    ;// Output
            call WriteString
            mov eax, [sum]
            call WriteInt
            call Crlf
    
            mov edx, OFFSET str3
            call WriteString
            mov eax, [prod]
            call WriteInt
            call Crlf
    
            mov edx, OFFSET str4
            call WriteString
            mov eax, [pow]
            call WriteInt
            call Crlf
    
            invoke ExitProcess, 0
    
        main ENDP
    
        doSum PROC              ;// Sum: (add ebx to eax) * ecx times
            SUM_LOOP:           ;// when ecx = 1 => simple addition a+b
            add eax, ebx
            loop SUM_LOOP
            ret
        doSum ENDP
    
        doMul PROC              ;// Multiply: Add num1 x num2 times
            xor eax, eax
            mov ebx, [num1]
            mov ecx, [num2]
            call doSum          ;// call Sum proc
            mov [prod], eax
            ret
        doMul ENDP
    
        doPow PROC              ;// Power: Add num1 x num2 times and 
            mov eax, [num1]     ;// add result x num2 times till end
            mov [tmp], eax
            mov esi, [num2]
            dec esi
            POW_LOOP:
                xor eax, eax
                mov ebx, [tmp]
                mov ecx, [num1]
                call doSum      ;// Call Sum proc
                mov [tmp], eax
                dec esi
            jnz POW_LOOP
            mov [pow], eax
            ret
    
        doPow ENDP
    
    END main