1 / 6

MIPS Assembly Language Programming

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

Rita
Download Presentation

MIPS Assembly Language Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CDA 3101 Discussion Section 06 MIPS Assembly Language Programming

  2. 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.

  3. 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.

  4. 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.

  5. 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); }

  6. 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

More Related