Question

In: Computer Science

Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...

Write a MIPS Assembly Language program to perform the following operations:

  1. Request and read two integers from the console (Dn and Up),
  2. Call a recursive function to compute the result

int RecurseFunc( int Dn, int Up )

{

if( Dn < 1 ) return 0;

return Dn * Up + RecurseFunc( Dn - 1, Up + 1 );

}

  1. Print out the results

Solutions

Expert Solution

Please find the code below::

.data
prompt: .asciiz "\nEnter first integer: "
prompt1: .asciiz "\nEnter second integer: "

.text

li $v0,4
la $a0,prompt #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $t0,$v0


li $v0,4
la $a0,prompt1 #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $t1,$v0

move $a0,$t0 #pass parameter to function
move $a1,$t1 #pass parameter to function
jal RecurseFunc #call function
move $s3,$v0 #get result
move $a0,$s3 #ready to print
li $v0,1 #print integer
syscall


li $v0,10 #terminat
syscall


RecurseFunc :
subu $sp,$sp,4 # point to the place for the new item,
sw $ra,($sp) # store the contents of $ra as the new top.
subu $sp,$sp,4 # point to the place for the new item,
sw $a0,($sp) # store the contents of $a0 as the new top.
subu $sp,$sp,4 # point to the place for the new item,
sw $a1,($sp) # store the contents of $t5 as the new top.


blt $a0,1,zeroValue #check three terminate condition

sub $a0,$a0,1 #dn-1
add $a1,$a1,1 #up+1
jal RecurseFunc
move $s0,$v0
add $a0,$a0,1 #dn-1
sub $a1,$a1,1 #up+1

mul $t0,$a0,$a1
add $v0,$s0,$t0
j ifExit

j ifExit
zeroValue:
li $v0,0 #load zero
j ifExit


ifExit:
lw $a1,($sp) # store the contents of $ra as the new top.
addu $sp,$sp,4 # point to the place for the new item,
lw $a0,($sp) # store the contents of $ra as the new top.
addu $sp,$sp,4 # point to the place for the new item,
lw $ra,($sp) # store the contents of $ra as the new top.
addu $sp,$sp,4 # point to the place for the new item,
jr $ra

output:


Related Solutions

Write a program in MIPS assembly language to perform the calculation of the following equation and...
Write a program in MIPS assembly language to perform the calculation of the following equation and save the result accordingly:    f = 5x + 3y + z Assumptions: - Registers can be used to represent variables x, y, z, and f - Initialize x, y, and z to values of your choice. f can be initialized to zero. - Use comments to specify your register usage and explain your logic
Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays...
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays the sum and the difference. In the .data section, define two variables num1 and num2 both words. Initialize num1 to 92413 10 and num2 to D4B 16 (use 0xD4B to initialize, Note that D4B is a hexadecimal number). Your main procedure/function should load the values of num1 and num2 into two temporary registers, and display them on the console window. Then add the values...
Write a MIPS Assembly language program to request a file name from the user, open the...
Write a MIPS Assembly language program to request a file name from the user, open the file, read the contents, and write out the contents to the console. This is what I have so far: .data    fileName:   .space 100    prompt1:   .asciiz "Enter the file name: "    prompt2:    .asciiz ""    prompt3:   .asciiz "\n"    buffer:    .space 4096 .text    main:        #        li $v0, 4        la $a0, prompt1       ...
Use MIPS assembly language program to swap two of the integers in an integer array. The...
Use MIPS assembly language program to swap two of the integers in an integer array. The program should include the Swap function to swap the integers and the main function to call the Swap function. The main function should: • Pass the starting address of the array in $a0. • Pass the indices of the two elements to swap in $a1 and $a2. • Preserve (i.e. push onto the stack) any T registers that it uses. • Call the Swap...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly language program to read an arbitrary number of integer pairs from a...
Write a MIPS assembly language program to read an arbitrary number of integer pairs from a file. Each pair will then be multiplied together with the results being accumulated (added together) forming a sum-of-products operation. Submit your report and code here.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT