60 likes | 487 Views
CDA 3101 Discussion Section 06. MIPS Assembly Language Programming. Problem 1. Write a function MinMax(&X, N) to find the minimum and maximum of an array X of N integers. The address of the array is passed in $a0, and the number of words in the array
E N D
CDA 3101 Discussion Section 06 MIPS Assembly Language Programming
Problem 1 Write a function MinMax(&X, N) to find the minimum and maximum of an array X of N integers. The address of the array is passed in $a0, and the number of words in the array is passed in $a1. The minimum and maximum are returned in registers $v0 and $v1 respectively. Also, write a short main program that • Prompts the user for the value of N. • Dynamically allocates memory for storing array X of N integers. (Remember, 4 bytes per integer!) • Prompts user to enter N integers one by one to fill the array X. • Calls the MinMax function to find and return the minimum and maximum of the array X. • Prints the minimum and the maximum value.
Problem 1 Write a function MinMax(&X, N) to find the minimum and maximum of an array X of N integers. The address of the array is passed in $a0, and the number of words in the array is passed in $a1. The minimum and maximum are returned in registers $v0 and $v1 respectively. Also, write a short main program that • Prompts the user for the value of N. • Dynamically allocates memory for storing array X of N integers. (Remember, 4 bytes per integer!) • Prompts user to enter N integers one by one to fill the array X. • Calls the MinMax function to find and return the minimum and maximum of the array X. • Prints the minimum and the maximum value.
Problem 2 • Implement a recursive function fib(n) to compute Fibonacci numbers. Also write a short main function that prompts the user to enter the integer N, calls the function fib(N) and prints the value returned by the fib(N) function.Note that: • fib(0)=0; fib(1)=1; • fib(n)=fib(n-1)+ fib(n-2), n>1.
Problem 2 • Implement a recursive function fib(n) to compute Fibonacci numbers. Also write a short main function that prompts the user to enter the integer N, calls the function fib(N) and prints the value returned by the fib(N) function. int fib(int N) { if(N == 0) return 0; else if(N == 1) return 1; else return fib(N-1) + fib(N-2); }
Key Points • Dynamically allocate memory li $v0, 9 # Memory allocation service li $a0, <int> # Allocate <int> bytes of mem. syscall move $t0, $v0 # Move address of array to safety. • Recursion self: … addi $sp, $sp, -4 # Allocate stack space sw $ra, 0($sp) # Save old return address jal self # Jump to self lw $ra, 0($sp) # Load old return address addi $sp, $sp, 4 # Restore stack to old state