Question

In: Computer Science

Create a MIPS assembly program to calculate x^y using a recursive subprogram. You may assume that...

Create a MIPS assembly program to calculate x^y using a recursive subprogram. You may assume that y is not negative.

Solutions

Expert Solution

.data
str1: .asciiz "Give integer X from 1 to 20 "
str2: .asciiz "Give integer Y from 0 to 5 "
errormsg: .asciiz "Out of range.\n"
nline: .asciiz "\n"
result1: .asciiz " raised to "
result2: .asciiz " gives: "
.text

error:
li $v0,4 #print string1
la $a0,errormsg
syscall
beq $s2,$zero,getX
j getY

.globl main
main:
addi $s0,$zero,21 #s0=21
addi $s1,$zero,6 #s1=6

getX:
addi $s2,$zero,0 #s2=0 to input x and 1 to input y
li $v0,4
la $a0,str1
syscall #print string1
li $v0,5
syscall #read int
slt $s3,$v0,$s0 #$s3=($v0<$s0) if(x<21) $s1=1
beq $s3,$zero,error #if $s3=0 goto error
blez $v0,error #if ($v0<=0) goto error
move $t0,$v0

getY:
addi $s2,$zero,1
li $v0,4
la $a0,str2
syscall #print string2
li $v0,5
syscall #read int
slt $s3,$v0,$s1 #$s3=($v0<$s1) if(x<6) $s3=1
beq $s3,$zero,error #if $s3=0 goto error
bltz $v0,error #if ($v0<0) goto error
move $t1,$v0

beq $t1,$zero,else #if (t1=0) t2=1, t1=y,t2=result
addi $t2,$zero,1
addi $s4,$zero,0

loop: #if s4<t1
slt $s5,$s4,$t1 #$s5=($s4<$t1) if(x<21) $s5=1
beq $s5,$zero,printresult
#if $s1=0 goto printresult(s4=t1)
mul $t2,$t2,$t0 #t2=t2*t0
addi $s4,$s4,1
j loop

else:
addi $t2,$zero,1
j printresult

printresult:
li $v0,1
move $a0,$t0
syscall #print X
li $v0,4
la $a0,result1
syscall #print " raised to "
li $v0,1
move $a0,$t1
syscall #print Y
li $v0,4
la $a0,result2
syscall #print " gives "
li $v0,1
move $a0,$t2
syscall #print result(t2)
li $v0,10
syscall #exit


Related Solutions

Write a MIPS assembly program to calculate the Fibonacci numbers from 1..n using the recursive method....
Write a MIPS assembly program to calculate the Fibonacci numbers from 1..n using the recursive method. The definition of a Fibonacci number is F(n) = F(n-1) + F(n-2). The implementation must follow the following guidelines: Prompt the user for a number n Allocate heap memory to hold the exact number of elements in the Fibonacci sequence for n Implement recursive Fibonacci method as a subprogram Print the Fibonacci sequence array
write mips assembly mips assembly (x+a) (bx^2+cx+d) using the pesedo- code with loop ,program should claclute...
write mips assembly mips assembly (x+a) (bx^2+cx+d) using the pesedo- code with loop ,program should claclute six T[i] and display aproper message about roots .the the program should store all T[i] as memory array a = [1, 1, 1, 1, 1, 1]; b = [1, 2, 4, 8, 16, 32]; c = [-6, -4, -2, 2, 4, 6]; d = [-1, -3, -5, -7, -9, -11]; for (i=0 ; i<=6; i++) { T[i] =-4 *b [i] * d[i] +c[i] *...
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user toinput an...
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user to input an integer and then prints out a string that shows how that integer should be encoded using 16 bits. Your program should handle both positive and negative valued inputs. Your program should also print out an error message if the given input cannot be expressed as a 16 bit signed integer.As an example, if the input is 12, your program should output “0000000000001100”. If the input...
Using MIPS assembly language In this program, you should define an array of 10 elements in...
Using MIPS assembly language In this program, you should define an array of 10 elements in your data segment with these values: ? = {11, 12,−10, 13, 9, 12, 14, 15,−20, 0} a. Write a function which finds the maximum value of this array. b. Write another function which calculates the summation of this array. c. Call these functions in your main program, and print the outputs of these functions to the user i. “The maximum is 15” ii. “The...
Write a MIPS Assembly program function to calculate the factorial of an input number. Analyze the...
Write a MIPS Assembly program function to calculate the factorial of an input number. Analyze the program for the value 10 and compute the Execution time in a MIPS processor at 2.4GHz. The CPI is 1.
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS)...
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS) which works as a simple calculator. The program will get two integer numbers, and based on the requested operation, the result should be shown to the user. a. The program should print a meaningful phrase for each input, and the result. i. “Enter the first number” ii. “Enter the second number” iii. “Enter the operation type” iv. “The result is” b. The user should...
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers....
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers. If x=2, y=4, the method pow should return 16. Java answers only please.
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the following declaration: void towers(int n, char source, char dest, char spare); The function outputs a message describing each move. The source, destination, and spare poles are indicated with a character identifier of your choosing ('A', 'B', 'C' are common). Write a MIPS assembly language program that demonstrates the Towers of Hanoi procedure. Your program should ask the user for the number of disks. The...
Write a MIPS assembly language program that implements the following pseudo-code operation: result = x +...
Write a MIPS assembly language program that implements the following pseudo-code operation: result = x + y – z + A[j] x and y should be in reserved memory words using the .word directive and labeled as x and y. Initialize x=10 and y=200. Read in z from the console. Input the value -8. This is the value for z, not for –z. Store this value in memory with the label z. To begin, you could just initialize z to...
I'm trying to code in MIPS (MIPS Assembly Language) to calculate the hamming distance between two...
I'm trying to code in MIPS (MIPS Assembly Language) to calculate the hamming distance between two integers. Ideally, the program would ask for the user to type in the two integers. Then, the program would calculate the hamming distance. Afterward, it would ask if you'd like to find another hamming distance. If the user says yes, it would loop back to the beginning and ask for two new integers. Below is the code that I've created so far. Guidance with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT