In: Computer Science
Write a MIPS program that will ask the user to enter two numbers at the console and compute xy
The MIPS assembly code for the problem is provided below, please comment if any doubts:
Note: The code is tested using MARS and the screenshot of the output and code is provided at the end,
MIPS code:
#the data segment
.data
msg1: .asciiz "\nInput the x: "
msg2: .asciiz "\nInput the y: "
prdct: .asciiz "\n x^y =: "
#the text segment
.text
.globl main
#the main
main:
#display the prompt 1
li $v0, 4
la $a0, msg1
syscall
#read the number from console
li $v0, 5
syscall
#store the the first number to regitster
t3
add $t3, $v0, $zero
#display the prompt 1
li $v0, 4
la $a0, msg2
syscall
#read the number from console
li $v0, 5
syscall
#store the read number in t4 register
add $t4, $v0, $zero
#perform the multiplication
addi $t0,$zero,1
addi $s5,$zero,0
#the loop to perform the power computation
loop:
beq $s5,$t4, result
mul $t0,$t0,$t3
addi $s5,$s5,1
j loop
result:
#display the result message
li $v0, 4
la $a0, prdct
syscall
#display the higher part of the result
li $v0, 1
mfhi $a0
syscall
#display the lower part of result
li $v0, 1
mflo $a0
syscall
exit_code:
li $v0, 10
syscall
Output:
Code screenshot: