Presentation is loading. Please wait.

Presentation is loading. Please wait.

CDA 3100 Recitation Week 8. Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A li $a1, 17 li $a2, 10 jal funct li $v0,

Similar presentations


Presentation on theme: "CDA 3100 Recitation Week 8. Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A li $a1, 17 li $a2, 10 jal funct li $v0,"— Presentation transcript:

1 CDA 3100 Recitation Week 8

2 Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A li $a1, 17 li $a2, 10 jal funct li $v0, 10 syscall

3 Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A# address of array li $a1, 17# ??? li $a2, 10# length of array jal funct li $v0, 10 syscall

4 Question 1 funct: li $t0, 0 li $v1, funct_L0: sll $t1, $t0, 2 add $t1, $t1, $a0 lw $t1, 0($t1) sub $t2, $t1, $a1 bgt $t2, $0, funct_L1 sub $t2, $0, $t2 funct_L1: bgt $t2, $v1, funct_L2 ori $v0, $t0, 0 ori $v1, $t2, 0 funct_L2: addi $t0, $t0, 1 blt $t0, $a2, funct_L0 jr $ra

5 Question 1 funct: li $t0, 0# Counter li $v1, # ??? funct_L0: sll $t1, $t0, 2 add $t1, $t1, $a0 lw $t1, 0($t1) sub $t2, $t1, $a1 bgt $t2, $0, funct_L1 sub $t2, $0, $t2 funct_L1: bgt $t2, $v1, funct_L2 ori $v0, $t0, 0 ori $v1, $t2, 0 funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

6 Question 1 funct: li $t0, 0# Counter li $v1, # ??? funct_L0: sll $t1, $t0, 2 add $t1, $t1, $a0 lw $t1, 0($t1) sub $t2, $t1, $a1 bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2 ori $v0, $t0, 0 ori $v1, $t2, 0 funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

7 Question 1 funct: li $t0, 0# Counter li $v1, # ??? funct_L0: sll $t1, $t0, 2# add $t1, $t1, $a0# Load element from array at counter lw $t1, 0($t1)# sub $t2, $t1, $a1 bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2 ori $v0, $t0, 0 ori $v1, $t2, 0 funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

8 Question 1 funct: li $t0, 0# Counter li $v1, # ??? funct_L0: sll $t1, $t0, 2# add $t1, $t1, $a0# Load element from array at counter lw $t1, 0($t1)# sub $t2, $t1, $a1 bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2# Update returns when ??? ori $v0, $t0, 0# Return array index ori $v1, $t2, 0# Return ??? funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

9 Question 1 funct: li $t0, 0# Counter li $v1, # ??? funct_L0: sll $t1, $t0, 2# add $t1, $t1, $a0# Load element from array at counter lw $t1, 0($t1)# sub $t2, $t1, $a1# ??? bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2# Update returns when ??? ori $v0, $t0, 0# Return array index ori $v1, $t2, 0# Return ??? funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

10 Question 1 funct: li $t0, 0# Counter li $v1, # ??? funct_L0: sll $t1, $t0, 2# add $t1, $t1, $a0# Load element from array at counter lw $t1, 0($t1)# sub $t2, $t1, $a1# Finds distance between element and parameter bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2# Update returns when ??? ori $v0, $t0, 0# Return array index ori $v1, $t2, 0# Return ??? funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

11 Question 1 funct: li $t0, 0# Counter li $v1, # Arbitrarily large distance funct_L0: sll $t1, $t0, 2# add $t1, $t1, $a0# Load element from array at counter lw $t1, 0($t1)# sub $t2, $t1, $a1# Finds distance between element and parameter bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2# Update returns when ??? ori $v0, $t0, 0# Return array index ori $v1, $t2, 0# Return ??? funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

12 Question 1 funct: li $t0, 0# Counter li $v1, # Arbitrarily large distance funct_L0: sll $t1, $t0, 2# add $t1, $t1, $a0# Load element from array at counter lw $t1, 0($t1)# sub $t2, $t1, $a1# Finds distance between element and parameter bgt $t2, $0, funct_L1# do next if $t2 < 0 sub $t2, $0, $t2# $t2 = abs($t2) funct_L1: bgt $t2, $v1, funct_L2# Update returns when new distance is smallest ori $v0, $t0, 0# Return array index ori $v1, $t2, 0# Return distance funct_L2: addi $t0, $t0, 1# Increment counter blt $t0, $a2, funct_L0# Loop tester jr $ra

13 Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A# address of array li $a1, 17# find element closest to this li $a2, 10# length of array jal funct li $v0, 10 syscall

14 Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A# address of array li $a1, 17# find element closest to this li $a2, 10# length of array jal funct# returns index and element closest to $a1 li $v0, 10 syscall

15 Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A# address of array li $a1, 17# find element closest to this li $a2, 10# length of array jal funct# returns index and element closest to $a1 # nothing changes the array, but there was # probably a swap here that should have li $v0, 10 syscall

16 Question 2 (10 points) Write a complete function called “F1” that takes in $a0 a number and return in $v0 the location of the most significant 1. In this problem, the number passed in $a0 will never be 0. For example, if the $a0 is 30, it should return 4 because 30 in binary is “ ” and we index the locations as bit 0 to bit 31. Please note that You do not have to save $a0 in your function. If more than 5 instructions are used for this function, 2 points will be deducted for each additional instruction until no more points are left. Keep in mind that some pseudo instructions may result in more than one real instruction and the number of real instructions will be counted.

17 Question 2 Add return instruction because function F1: jr $ra

18 Question 2 Use srl to be able to count bits F1: srl $a0, $a0, 1 jr $ra

19 Question 2 Set up post test loop; loop if $a0 has a 1 bit F1: srl $a0, $a0, 1 bne $a0, $0, F1 jr $ra

20 Question 2 Count index position F1:addi $v0, $v0, 1 srl $a0, $a0, 1 bne $a0, $0, F1 jr $ra

21 Question 2 Initialize index position F1:addi $v0, $0, -1 F1_L1:addi $v0, $v0, 1 srl $a0, $a0, 1 bne $a0, $0, F1_L1 jr $ra

22 Question 2 Hints: – Don’t forget about initialization – Use correct version of branch to limit use of unconditional jump instructions – Don’t forget pseudo instructions are expanded out when it’s being graded – Good chance it will use a post test loop – Good chance it will use either sll / srl – Typically, don’t worry about saving addresses, $ra, ect – It’s only a couple point deduction for each extra instruction; don’t worry about it too much if you go over by one or two instructions


Download ppt "CDA 3100 Recitation Week 8. Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A li $a1, 17 li $a2, 10 jal funct li $v0,"

Similar presentations


Ads by Google