Saturday, April 11, 2020

MIPS power of x^n calculation

This is MIPS assembler code that, given a base number (x) and an exponent (n), it calculates the power of xn. The code is fully commented.

Launch EzMIPSthe MIPS assembler simulator, copy the following MIPS code and paste it into EzMIPS. Assemble, Run.

# ----------------- power(x^n) function in MIPS ------------------ #

# $t0 = x   (base)
# $t1 = n   (exponent - must be greater than or equal to 0)
# $t2 = x^n (result)

# ---------------------------------------------------------------- #

.data

szEnterBase:     .asciiz "Enter base (x)     :"
szEnterExponent: .asciiz "Enter exponent (n) :"    # greater or equal to 0
szResult:        .asciiz "Result: "

# ---------------------------------------------------------------- #

.text

main:

# ................ print "Enter base (x): " ...................... #

    li $v0, 4                         # print string
    la $a0, szEnterBase               # load string address
    syscall                           # print it!

# ................. read base from user .......................... #

    li $v0, 5                         # read integer
    syscall                           # read it!
    move $t0, $v0                     # keep base in $t0

# ................ print "Enter exponent (n): " .................. #
exponent:

    li $v0, 4                         # print string
    la $a0, szEnterExponent           # load string address
    syscall                           # print it!

# ................. read exponent from user ...................... #

    li $v0, 5                         # read integer
    syscall                           # read it!
    blt $v0, $zero, exponent          # exponents greater or equal to 0
    move $t1, $v0                     # keep exponent in $t1

# ..................... $t2 = power($t0^$t1) ..................... #

    li $t2, 1                         # $t2 = result = 1
    beq $t1, $zero, exit_pow          # if (n==0)

while:

    mul $t2, $t2, $t0                 # x = x*x
    addi $t1, $t1, -1                 # n--
    bne $t1, $zero, while             # while (n>0)

exit_pow:

# ...................... print "Result: " ........................ #

    li $v0, 4                         # print string
    la $a0, szResult                  # load string address
    syscall                           # print it!

# ..................... print integer result ..................... #
    
    li $v0, 1                         # print integer
    move $a0, $t2                     # integer to print must be in $a0
    syscall                           # print it!

# ....................... exit program ........................... #

    li $v0, 10                        # return to OS
    syscall                           # do it!

# ---------------------------------------------------------------- #

Please let me know of any suggestions or bugs regarding the code above.

Regards,

Antonis

No comments:

Post a Comment