Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will implement a simple function, lookup (int arr[], int num, int cnt). The function finds the integer value (num) from the integer array (arr).

image text in transcribed

image text in transcribed

image text in transcribed

You will implement a simple function, lookup (int arr[], int num, int cnt). The function finds the integer value (num) from the integer array (arr). The last argument (cnt) is used to set the last index of array to be checked. The function returns the index of array if num is found in arr, else -1. For example, assume an array arr[] = 5 2 1 4 6 3 Return values for each function call. 1. lookup (arr, 5, 3) > 0 (the index of '5' in the array) 2. lookup (arr, 1, 3) 2 (the index of 'l' in the array) 3. lookup (arr, 7, 6) +-1 (*7' is not found in the array) 4. lookup (arr, 4, 3) > -1 (the last parameter indicates to check only first 3 values in the array) The main function will call lookup to see if the integer array (arr) stores the integer value (num). SW 3. Implementation Details 3.1. Some instruction list (including pseudo-instructions) You will need to use MIPS instructions below (but not limited to) to implement the lookup function. You can use any other MIPS instructions to complete the function. Instruction Example Description li li $t0, 1 An integer value is loaded into a register (Sto) with 1 la la $t0, sym An address is loaded into Sto with the address sym A word is loaded into $t0 from the specified address lw lw $t0, offset($t1) (offset + St1) sw $t0, offset($t1) The contents of Sto is stored at the specified address (offset + St1) add add $t0,$ti, $t2 Adds St1 and $t2 and stores the result in Sto addi addi $t0, $t1, 1 Adds Stl and a sign-extended immediate value (1) and stores the result in Sto Shifts $t1 value left by the shift amount (4) and places sll sll $t0, $ti, 4 the result in $to. Zeroes are shifted in mul mul $t0, $t1, $t2 Multiply $t1 and $t2 and stores the result in $t0 Jumps to the calculated address and stores the return jal jal target address in Sra ($31) j j target Jumps to the calculated address beq beg $t0,$t1, target Branches to target if St0 and Stl are equal blt $t0, $ti, target Branches to target if $t0 is less than St1 slt slt $t0, $ti, $t2 If $t1 is less than $t2, $t0 is set to 1,0 otherwise blt 3.2. Translating C code into MIPS code You may want to implement lookup in C first to understand how the function works clearly. You will need to know how to translate loop (for and while) statement and if statement into MIPS code. You will also need to know how to load data in memory into registers and how to store data in registers into memory. Finishing HW2 will help you to translate C code into MIPS assembly code. Some MIPS program examples are also available on Canvas. You can take a look at any other MIPS code or references from Internet or book. You can discuss with your classmates for this project to get some hints. But it is not allowed to share the solution with your classmates. I will use a tool to detect cheating. 4. Testcase and extra credit A skeleton code (prjl.asm) will be provided. The code includes a main function and some other code for initialization. The array is hard-coded in .data section in the code, arr: .word 5, 3, 1 You will need to change the numbers and/or to increase the size of array by adding more numbers like below. arr: word 7, 4, 2, 6, 5, 4 You can assume that the array stores any integer values between 0 and 9. If there is duplicated value in the array, the function will return the first index. You will also need to change the second parameter (num) and the third parameter (cnt) to test your code. li Sal, 7 # change 7 to different number to test #change 3 to different number to test li Sa2, 3 Extra credit: The second parameter is hard-coded. If you implement the main function to get the second parameter from a user, you will get 1 extra credit. You will need to print a message for user to input a number to be looked up. 5. Deliverables a. Document describing how your assembly code works. Not to exceed 1 page. b. MIPS source code You will implement a simple function, lookup (int arr[], int num, int cnt). The function finds the integer value (num) from the integer array (arr). The last argument (cnt) is used to set the last index of array to be checked. The function returns the index of array if num is found in arr, else -1. For example, assume an array arr[] = 5 2 1 4 6 3 Return values for each function call. 1. lookup (arr, 5, 3) > 0 (the index of '5' in the array) 2. lookup (arr, 1, 3) 2 (the index of 'l' in the array) 3. lookup (arr, 7, 6) +-1 (*7' is not found in the array) 4. lookup (arr, 4, 3) > -1 (the last parameter indicates to check only first 3 values in the array) The main function will call lookup to see if the integer array (arr) stores the integer value (num). SW 3. Implementation Details 3.1. Some instruction list (including pseudo-instructions) You will need to use MIPS instructions below (but not limited to) to implement the lookup function. You can use any other MIPS instructions to complete the function. Instruction Example Description li li $t0, 1 An integer value is loaded into a register (Sto) with 1 la la $t0, sym An address is loaded into Sto with the address sym A word is loaded into $t0 from the specified address lw lw $t0, offset($t1) (offset + St1) sw $t0, offset($t1) The contents of Sto is stored at the specified address (offset + St1) add add $t0,$ti, $t2 Adds St1 and $t2 and stores the result in Sto addi addi $t0, $t1, 1 Adds Stl and a sign-extended immediate value (1) and stores the result in Sto Shifts $t1 value left by the shift amount (4) and places sll sll $t0, $ti, 4 the result in $to. Zeroes are shifted in mul mul $t0, $t1, $t2 Multiply $t1 and $t2 and stores the result in $t0 Jumps to the calculated address and stores the return jal jal target address in Sra ($31) j j target Jumps to the calculated address beq beg $t0,$t1, target Branches to target if St0 and Stl are equal blt $t0, $ti, target Branches to target if $t0 is less than St1 slt slt $t0, $ti, $t2 If $t1 is less than $t2, $t0 is set to 1,0 otherwise blt 3.2. Translating C code into MIPS code You may want to implement lookup in C first to understand how the function works clearly. You will need to know how to translate loop (for and while) statement and if statement into MIPS code. You will also need to know how to load data in memory into registers and how to store data in registers into memory. Finishing HW2 will help you to translate C code into MIPS assembly code. Some MIPS program examples are also available on Canvas. You can take a look at any other MIPS code or references from Internet or book. You can discuss with your classmates for this project to get some hints. But it is not allowed to share the solution with your classmates. I will use a tool to detect cheating. 4. Testcase and extra credit A skeleton code (prjl.asm) will be provided. The code includes a main function and some other code for initialization. The array is hard-coded in .data section in the code, arr: .word 5, 3, 1 You will need to change the numbers and/or to increase the size of array by adding more numbers like below. arr: word 7, 4, 2, 6, 5, 4 You can assume that the array stores any integer values between 0 and 9. If there is duplicated value in the array, the function will return the first index. You will also need to change the second parameter (num) and the third parameter (cnt) to test your code. li Sal, 7 # change 7 to different number to test #change 3 to different number to test li Sa2, 3 Extra credit: The second parameter is hard-coded. If you implement the main function to get the second parameter from a user, you will get 1 extra credit. You will need to print a message for user to input a number to be looked up. 5. Deliverables a. Document describing how your assembly code works. Not to exceed 1 page. b. MIPS source code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Nested Relations And Complex Objects In Databases Lncs 361

Authors: Serge Abiteboul ,Patrick C. Fischer ,Hans-Jorg Schek

1st Edition

3540511717, 978-3540511717

More Books

Students also viewed these Databases questions