Number exponentiation

A program that computes the power of any number.

Program source code

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    askUserBase db "Enter the base: \0"
    askUserExp  db "\nEnter the exponent: \0"
    msgres      db "\nResult: %d\0"
    fmtBase     db "%d"
    fmtExponent db "%d"
    base        dw ?
    exponent    dw ?
    number      dw 1
data ends

code segment public                   ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc              ; Include the file with functions

    main proc                         ; Start of the main procedure
        call    SetDataSegment        ; Call the SetDataSegment procedure

        lea     ax, ds:[askUserBase]  ; Load the address of the askUserBase variable into the ax register
        call    PrintFormatString     ; Call the PrintFormatString procedure

        lea     ax, ds:[base]         ; Load the address of the base variable into the ax register
        push    ax                    ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtBase]      ; 1st: load the address of the fmtBase format string into ax
        call    ScanFormatString      ; Call the ScanFormatString procedure
        add     sp, 2                 ; Align the stack

        lea     ax, ds:[askUserExp]   ; Load the address of the askUserExp variable into the ax register
        call    PrintFormatString     ; Call the PrintFormatString procedure

        lea     ax, ds:[exponent]     ; Load the address of the exponent variable into the ax register
        push    ax                    ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtExponent]  ; 1st: load the address of the fmtExponent format string into ax
        call    ScanFormatString      ; Call the ScanFormatString procedure
        add     sp, 2                 ; Align the stack

        mov     ax, number            ; Copy the value of the number variable into the ax register
        mov     bx, base              ; Copy the value of the base variable into the bx register
        mov     cx, 1                 ; Set the counter to 1

    MainLoop:
        mul     bx                    ; Multiply the value in the ax register by the value in bx

        mov     number, ax            ; Store the result in the number variable

        inc     cx                    ; Increment the value of the counter register by 1
        cmp     cx, exponent          ; Compare the value of cx with the value in exponent
        jbe     MainLoop              ; If the value is less than or equal to exponent, jump to MainLoop

        push    number                ; 2nd: save the value of the number variable on the stack
        lea     ax, ds:[msgres]       ; 1st: load the address of the msgres variable into ax
        call    PrintFormatString     ; Call the PrintFormatString procedure
        add     sp, 2                 ; Align the stack

        xor     al, al                ; Clear the al register
        call    ExitProcess           ; Call the ExitProcess procedure
    main endp                         ; End of the main procedure

code ends                             ; End of the code segment
end main                              ; End of file. The main procedure is the entry point

Enter the base: 2
Enter the exponent: 5
Result: 32

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".