Arrays and recursion in mips assembly language

In summary, this program is written in MIPS assembly language and prompts the user to enter an integer between 1 and 10. If the input is not within this range, the user is prompted to enter a new number until a valid one is entered. Depending on the input, the program executes different cases: - If the input is between 1 and 6, the program calculates and displays the value of F(n) using a recursive function call. - If the input is between 7 and 9, the program prompts the user to enter a sequence of 10 characters and displays them in reverse order. - If the input is 10, the program displays a joke. The program is also well-documented with comments
  • #1
rubenhero
42
2

Homework Statement


Write a MIPS assembly language program that accomplishes the following tasks:
1. The program will prompt the user to enter an Integer between 1 and 10.
If the entered number doesn’t satisfy the above condition, use a loop and prompt the
user for a new entry (until a valid number is entered)
2. Depending on the n value implement the following cases:
case 1: if 1< = n < = 6 compute F(n): if (n=1 or n=2) then F(n) = 10
else F(n) = 2n*F(n-1);
Display a result_message together with the numeric value of the result.
NOTE: use recursive function call.
case 2: if 7 <= n < 10
1. Declare an array of 10 elements.
2. Prompt the user to enter one by one a sequence of 10 characters. For each entered
character:
a. Read in the character.
b. Display the character.
3. Display the contents of the array in reverse order.
Your program should be well documented with comments.
Your console output should include helpful prompts for the user.
Case 3: if n = 10
Display a joke.


Homework Equations





The Attempt at a Solution


Code:
	    .text
	    addi $s0, $zero, 0	# register to transfer the user input.
	    addi $s1, $zero, 10	# register to hold integer value 10.
	    addi $s2, $zero, 2	# register to hold integer value 2.
	    addi $s3, $zero, 0	# initialize register for store of multiplication.
	    addi $s4, $zero, 0	# initialize register for case1b result.
	    addi $t0, $zero, 0	# initialize register for store of lower 32 bits of multiplication.
	    addi $t1, $zero, 0	# initialize register for store of lower 32 bits of multiplication  2nd time.
	    addi $t9, $zero, 0	# initialize register for array.		
LOOP0:	
	    la $a0, Prompt		# load Prompt string into $a0.
	    li $v0, 4		# system call to print Prompt string.
	    syscall
	
	    li $v0, 5		# system call to read integer from user.
	    syscall
	
	    bgt $v0, 10, LOOP0	# branch if greater than 10, force user to enter lower number.
	    blt $v0, 1, LOOP0	# branch if less than 1, force user to enter a positive number.
	    ble $v0, 6, CASE1	# branch to CASE1 if input is less than or equal to value 6.
	    blt $v0, 10, CASE2	# branch to CASE2 if input is less than value 10.
	    beq $v0, 10, CASE3	# branch to CASE3 if input value is equal to value 10.
	
CASE1: 				# CASE1
	    move $s0, $v0		# move the input value from $v0 to $s0.
	    ble $s0, 2, CASE1A	# branch to CASE1A if $s0 (the input value) is less than or equal to 2.
	    ble $s0, 6, CASE1B	# branch to CASE1B if s$0 is less than or equak to 6.

CASE1A:				# CASE1A
	    la $a0, MSG1		# Load MSG1 string to $a0.
	    li $v0, 4		# system call to print MSG1 string.
	    syscall
	    la $a0, 0($s1)		# Load $s1 to $a0, the value is 10.
	    li $v0, 1		# system call to print the integer.
	    syscall
	    j EXIT			# jump to EXIT.

CASE1B:
	    mult $s0, $s2		# multiply $s0 by $s2.
	    mfhi $v0		# move the upper 32 bits to $v0.
	    mflo $v1		# move the lower 32 bits to $v1.
	    move $t0, $v1		# move $v1 to $t0.
	    addi $s3, $s0, -1	# $s3 = $s0 - 1 or n-1.
	
	    mult $s3, $s2		# multiply $s3 by $s2 or 2(n-1).
	    mfhi $v0		# move the upper 32 bits to $v0.
	    mflo $v1		# move the lower 32 bits to $v1.
	    move $t1, $v1		# move $v1 to $t1.
	
	    mult $t0, $t1		# multiply $t0 by $t1.
	    mfhi $v0		# move the upper 32 bits to $v0.
	    mflo $v1		# move the lower 32 bits to $v1.
	    move $s4, $v1		# move $v1 to $s4
	
	    la $a0, MSG1		# Load MSG1 string into $a0.
	    li $v0, 4 		# system call to print MSG1 string.
	    syscall
	    la $a0, 0($s4)		# Load $s4 into $a0.
	    li $v0, 1		# system call to print integer.
	    syscall
	    j EXIT			# jump to EXIT.
	
CASE2:
	    la $s7, ARRAY		# Load base address of array into $t9
	    li $s6, 36		# Initialize loop counter to 36

LOOP:	
	    la $a0, Prompt2		# Load Prompt2 string into $a0.
	    li $v0, 4		# system call to print string Prompt2.
	    syscall
	
	    li $v0, 12 		# read character input.	
	    syscall
	    move $s6($s7), $v0	# move input character to an array element.
	
	    la $a0, $s6($s7)	# load array element into $a0.
	    li $v0, 11		# system call to print array element.
	    syscall
	    addi $s6, $s6, -4	# decrement loop counter.
	    bgtz $s6, LOOP
	
	    la $a0, MSG2		# load MSG2 string into $a0.
	    li $v0, 4		# system call to print MSG2 string.
	    syscall	
LOOP2:
	    la $a0, $s6($s7)	# load element of array into $a0.
	    li $v0, 11		# system call to print char.
	    addi $s6, $s6, 4	# increment $t8.
	    blt $s6, 36, LOOP2 	# branch if $t8 is less than 36
	    j EXIT			# when $t8 reaches 36 jump to EXIT.
		
CASE3:
	    la $a0, JOKE		# Load JOKE string into $a0.
	    li $v0, 4		# system call to print JOKE string.
	    syscall
		
EXIT:				# EXIT
	    li $v0, 10		# system call to exit the program.
	    syscall
	
	.data
Prompt: .asciiz "Enter an integer between 1 and 10: "
MSG1: 	.asciiz "\nThe result is: "
MSG2: 	.asciiz "\nThe reverse is: \n"
Prompt2:.asciiz "\nEnter a character: "
JOKE: 	.asciiz "\nAdam and Eve Virus: Takes a couple of bytes out of your Apple\n"
ARRAY:	.space 10 		# 10 bytes of storage to hold an array of 10 characters

I got the program to respond to inputs 1,2,10 but 3,4,5,6,7,8,9 aren't producing the outputs that i need.
I think it might be the part where i move the input into the array. The recursive function part has been difficult for me.
Any suggestions and help would be greatly appreciated.
 

Attachments

  • mip1.txt
    3.8 KB · Views: 529
Physics news on Phys.org
  • #2



Hi there! Your MIPS assembly language program looks like a good start. I can offer a few suggestions for improving it:

1. In your loop for prompting the user to enter an integer between 1 and 10, consider using a different register for storing the user input instead of $s0. This is because $s0 is used later on in your program and may cause issues if it is overwritten with the user input.

2. In your recursive function for computing F(n), you are using the value of $s0 as the input for the function. However, in your CASE1A and CASE1B sections, you are overwriting the value of $s0 with the user input. This may cause issues when trying to recursively call the function again. Instead, consider using a different register to store the input for the recursive function.

3. For the array part of your program, it looks like you are using the same loop counter for both prompting the user to enter characters and displaying the contents of the array in reverse order. This may cause issues as the loop counter will be different when trying to display the array in reverse. Consider using a separate loop counter for each loop.

4. In your recursive function, it looks like you are not taking into account the base case of n = 1 or n = 2. In these cases, you should return a value of 10 instead of using the recursive formula. This may be why your program is not producing the correct output for inputs 3-9.

I hope these suggestions are helpful in debugging your program. Keep up the good work!
 

Related to Arrays and recursion in mips assembly language

What is an array in MIPS assembly language?

An array in MIPS assembly language is a data structure that allows for the storage of multiple elements of the same data type in a contiguous block of memory. Each element in the array can be accessed using its index, which represents its position in the array.

How do you declare an array in MIPS assembly language?

To declare an array in MIPS assembly language, you first need to allocate memory for the array using the .space directive. Then, you can initialize the array by storing values in each element using the sw instruction.

What is recursion in MIPS assembly language?

Recursion in MIPS assembly language is a programming technique where a function calls itself until a certain condition is met. This allows for the solution of complex problems by breaking them down into smaller, more manageable subproblems.

How do you implement recursion in MIPS assembly language?

To implement recursion in MIPS assembly language, you need to define a base case that will end the recursive calls and a recursive case that will call the function again with a smaller input. It is important to keep track of the function's state, such as the return address and the values of the function's variables, by using the stack.

What are the benefits of using arrays and recursion in MIPS assembly language?

Arrays and recursion in MIPS assembly language allow for the efficient storage and manipulation of large amounts of data and the solution of complex problems. They also promote code reusability and can lead to more elegant and concise solutions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
12K
Back
Top