This is another sample MIPS program that computes the power of X^Y. In this program, X and Y are hardcoded variables that can be easiy changed for further testing. The code is fully documented.
Launch EzMIPS, the MIPS assembler simulator, copy the following MIPS code and paste it into EzMIPS. Assemble, Run.
# -------- Function to compute power (ie, X to Y power) -------- #
# --------------------- Data Declarations ---------------------- #
.data
X: .word 4
Y: .word 11
Answer: .word 0
szTo: .asciiz " ^ "
szEquals: .asciiz " = "
szLF: .asciiz "\n"
# ------------------------- Main routine ----------------------- #
# Call simple procedure to add two numbers
.text
# ............................. main ........................... #
main:
lw $a0, X # pass argument to function
lw $a1, Y # pass argument to function
jal power # call power(int X, int Y) function
sw $v0, Answer # store result of power(int X, int Y)
# into Answer variable
# ............................ print ........................... #
# print integer X
li $v0, 1 # print integer code
lw $a0, X # copy X to $a0
syscall # print it!
# print string " ^ "
li $v0, 4 # print string code
la $a0, szTo # load address of szTo
syscall # print it!
# print integer Y
li $v0, 1 # print integer code
lw $a0, Y # copy Y to $a0
syscall # print it!
# print string " = "
li $v0, 4 # print string code
la $a0, szEquals # load address of szEquals
syscall # print it!
# print integer answer
li $v0, 1 # print integer code
lw $a0, Answer # copy Answer to $a0
syscall # print it!
# ............................ terminate ....................... #
li $v0, 10
syscall # exit program
# ................... power(int X, int Y) function ............. #
# function to find and return X^Y
# Arguments
# $a0 – X
# $a1 – Y
# Returns
# $v0 = X^Y
power:
li $v0, 1 # initialize $v0 = 1
li $t0, 0 # initialize $t0 = 0
loop:
mul $v0, $v0, $a0 # $v0 = $v0 * $a0 (remember $a0 = X)
addi $t0, $t0, 1 # $t0 = $t0 + 1
blt $t0, $a1, loop # if $t0 < $a1, loop (remember $a1 = Y)
jr $ra # return to caller (ie retutn to main()
# -------------------------------------------------------------- #
Launch EzMIPS, the MIPS assembler simulator, copy the following MIPS code and paste it into EzMIPS. Assemble, Run.
# -------- Function to compute power (ie, X to Y power) -------- #
# --------------------- Data Declarations ---------------------- #
.data
X: .word 4
Y: .word 11
Answer: .word 0
szTo: .asciiz " ^ "
szEquals: .asciiz " = "
szLF: .asciiz "\n"
# ------------------------- Main routine ----------------------- #
# Call simple procedure to add two numbers
.text
# ............................. main ........................... #
main:
lw $a0, X # pass argument to function
lw $a1, Y # pass argument to function
jal power # call power(int X, int Y) function
sw $v0, Answer # store result of power(int X, int Y)
# into Answer variable
# ............................ print ........................... #
# print integer X
li $v0, 1 # print integer code
lw $a0, X # copy X to $a0
syscall # print it!
# print string " ^ "
li $v0, 4 # print string code
la $a0, szTo # load address of szTo
syscall # print it!
# print integer Y
li $v0, 1 # print integer code
lw $a0, Y # copy Y to $a0
syscall # print it!
# print string " = "
li $v0, 4 # print string code
la $a0, szEquals # load address of szEquals
syscall # print it!
# print integer answer
li $v0, 1 # print integer code
lw $a0, Answer # copy Answer to $a0
syscall # print it!
# ............................ terminate ....................... #
li $v0, 10
syscall # exit program
# ................... power(int X, int Y) function ............. #
# function to find and return X^Y
# Arguments
# $a0 – X
# $a1 – Y
# Returns
# $v0 = X^Y
power:
li $v0, 1 # initialize $v0 = 1
li $t0, 0 # initialize $t0 = 0
loop:
mul $v0, $v0, $a0 # $v0 = $v0 * $a0 (remember $a0 = X)
addi $t0, $t0, 1 # $t0 = $t0 + 1
blt $t0, $a1, loop # if $t0 < $a1, loop (remember $a1 = Y)
jr $ra # return to caller (ie retutn to main()
# -------------------------------------------------------------- #
Please let me know of any suggestions or bugs regarding the code above.
Regards,
Antonis
No comments:
Post a Comment