asked 98.6k views
1 vote
MIPS assembly language program

The square of an integer N is equal to Triangle( N ) + Triangle( N-1 ).
Recursive function using the Triangle function from part 1 to calculate Square(N).
Your program has to prompt the user for a positive integer N and then print the result of Square( N ).
The definition of a square number is:
Square( N <= 1 ) = 1
Square( N > 1 ) = Triangle( N ) + Triangle( N-1 )
Call Square using a full stack frame, and have Square call Triangle twice, passing its argument on the stack.
For the recursion, c r e a t e automatic local variables on the stack and save both recursive results in the variables, then retrieve the values to add together to return to the caller.
Use addu to ignore the errors caused by potential overflow. You may assume that the user will enter a reasonable value (greater than or equal to 1). Test with several values, including a large value like 100.

2 Answers

4 votes

Final answer:

The objective is to write a MIPS assembly language program to calculate the square of an integer N using a recursive function and the Triangle function.

Step-by-step explanation:

The objective is to write a MIPS assembly language program to calculate the square of an integer N using a recursive function and the Triangle function. The program should prompt the user for a positive integer N and print the result of Square(N).

To implement the Square function, create a full stack frame and call the Triangle function twice, passing the argument on the stack. Save the recursive results in automatic local variables on the stack, retrieve the values, and add them together to return to the caller. Use the addu instruction to ignore potential overflow errors.

Here is the pseudocode for the Square function:

  1. Prompt the user for a positive integer N
  2. Call the Square function with N as the argument
  3. Print the result of Square(N)
answered
User Lygia
by
7.9k points
7 votes

Here is an MIPS assembly language program to calculate the square of a positive integer N using recursion:

The MPS Program

# Recursive function to calculate the square of N

# Square( N ) = Triangle( N ) + Triangle( N - 1 )

.text

.globl main

main:

li $v0, 4 # Prompt user for input

la $a0, prompt # Load address of prompt string

syscall

li $v0, 5 # Read user input

syscall

move $t0, $v0 # Store user input in $t0

jal Square # Call Square function

move $a0, $v0 # Move result to $a0 for printing

li $v0, 1 # Print the result

syscall

li $v0, 10 # Exit

syscall

Square:

addi $sp, $sp, -8 # Create stack frame

sw $ra, 0($sp) # Save return address

sw $a0, 4($sp) # Save argument N

li $v0, 1 # Base case: Square( N <= 1 ) = 1

blez $a0, square_end # Return 1 if N <= 1

addi $a0, $a0, -1 # Calculate N - 1

jal Triangle # Call Triangle( N - 1 )

move $t0, $v0 # Store result in $t0

lw $a0, 4($sp) # Retrieve original N

jal Triangle # Call Triangle( N )

addu $v0, $v0, $t0 # Add results for Square( N )

square_end:

lw $ra, 0($sp) # Restore return address

addi $sp, $sp, 8 # Restore stack pointer

jr $ra # Return

Triangle:

# Implementation of Triangle function (not provided in this snippet)

# Calculate Triangle( N )

# (Code for Triangle function goes here)

answered
User Nezam
by
8.2k points