+ All Categories
Home > Documents > CDA 3100

CDA 3100

Date post: 31-Dec-2015
Category:
Upload: brenden-landry
View: 23 times
Download: 1 times
Share this document with a friend
Description:
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, 10 syscall. Question 1. . data A: .word 21,3,2,9,100,22,6,15,33,90 .text . globl main main: - PowerPoint PPT Presentation
22
CDA 3100 Recitation Week 8
Transcript
Page 1: CDA 3100

CDA 3100

Recitation Week 8

Page 2: CDA 3100

Question 1.data

A: .word 21,3,2,9,100,22,6,15,33,90

.text

.globl mainmain:

la $a0, Ali $a1, 17li $a2, 10jal funct

li $v0, 10 syscall

Page 3: CDA 3100

Question 1.data

A: .word 21,3,2,9,100,22,6,15,33,90

.text

.globl mainmain:

la $a0, A # address of arrayli $a1, 17 # ???li $a2, 10 # length of arrayjal funct

li $v0, 10 syscall

Page 4: CDA 3100

Question 1funct:

li $t0, 0li $v1, 1000000

funct_L0:sll $t1, $t0, 2add $t1, $t1, $a0lw $t1, 0($t1)sub $t2, $t1, $a1bgt $t2, $0, funct_L1sub $t2, $0, $t2

funct_L1:bgt $t2, $v1, funct_L2ori $v0, $t0, 0ori $v1, $t2, 0

funct_L2:addi $t0, $t0, 1blt $t0, $a2, funct_L0jr $ra

Page 5: CDA 3100

Question 1funct:

li $t0, 0 # Counterli $v1, 1000000 # ???

funct_L0:sll $t1, $t0, 2add $t1, $t1, $a0lw $t1, 0($t1)sub $t2, $t1, $a1bgt $t2, $0, funct_L1sub $t2, $0, $t2

funct_L1:bgt $t2, $v1, funct_L2ori $v0, $t0, 0ori $v1, $t2, 0

funct_L2:addi $t0, $t0, 1 # Increment counterblt $t0, $a2, funct_L0 # Loop testerjr $ra

Page 6: CDA 3100

Question 1funct:

li $t0, 0 # Counterli $v1, 1000000 # ???

funct_L0:sll $t1, $t0, 2add $t1, $t1, $a0lw $t1, 0($t1)sub $t2, $t1, $a1bgt $t2, $0, funct_L1 # do next if $t2 < 0sub $t2, $0, $t2 # $t2 = abs($t2)

funct_L1:bgt $t2, $v1, funct_L2ori $v0, $t0, 0ori $v1, $t2, 0

funct_L2:addi $t0, $t0, 1 # Increment counterblt $t0, $a2, funct_L0 # Loop testerjr $ra

Page 7: CDA 3100

Question 1funct:

li $t0, 0 # Counterli $v1, 1000000 # ???

funct_L0:sll $t1, $t0, 2 #add $t1, $t1, $a0 # Load element from array at counterlw $t1, 0($t1) #sub $t2, $t1, $a1bgt $t2, $0, funct_L1 # do next if $t2 < 0sub $t2, $0, $t2 # $t2 = abs($t2)

funct_L1:bgt $t2, $v1, funct_L2ori $v0, $t0, 0ori $v1, $t2, 0

funct_L2:addi $t0, $t0, 1 # Increment counterblt $t0, $a2, funct_L0 # Loop testerjr $ra

Page 8: CDA 3100

Question 1funct:

li $t0, 0 # Counterli $v1, 1000000 # ???

funct_L0:sll $t1, $t0, 2 #add $t1, $t1, $a0 # Load element from array at counterlw $t1, 0($t1) #sub $t2, $t1, $a1bgt $t2, $0, funct_L1 # do next if $t2 < 0sub $t2, $0, $t2 # $t2 = abs($t2)

funct_L1:bgt $t2, $v1, funct_L2 # Update returns when ???ori $v0, $t0, 0 # Return array indexori $v1, $t2, 0 # Return ???

funct_L2:addi $t0, $t0, 1 # Increment counterblt $t0, $a2, funct_L0 # Loop testerjr $ra

Page 9: CDA 3100

Question 1funct:

li $t0, 0 # Counterli $v1, 1000000 # ???

funct_L0:sll $t1, $t0, 2 #add $t1, $t1, $a0 # Load element from array at counterlw $t1, 0($t1) #sub $t2, $t1, $a1 # ???bgt $t2, $0, funct_L1 # do next if $t2 < 0sub $t2, $0, $t2 # $t2 = abs($t2)

funct_L1:bgt $t2, $v1, funct_L2 # Update returns when ???ori $v0, $t0, 0 # Return array indexori $v1, $t2, 0 # Return ???

funct_L2:addi $t0, $t0, 1 # Increment counterblt $t0, $a2, funct_L0 # Loop testerjr $ra

Page 10: CDA 3100

Question 1funct:

li $t0, 0 # Counterli $v1, 1000000 # ???

funct_L0:sll $t1, $t0, 2 #add $t1, $t1, $a0 # Load element from array at counterlw $t1, 0($t1) #sub $t2, $t1, $a1 # Finds distance between element and parameterbgt $t2, $0, funct_L1 # do next if $t2 < 0sub $t2, $0, $t2 # $t2 = abs($t2)

funct_L1:bgt $t2, $v1, funct_L2 # Update returns when ???ori $v0, $t0, 0 # Return array indexori $v1, $t2, 0 # Return ???

funct_L2:addi $t0, $t0, 1 # Increment counterblt $t0, $a2, funct_L0 # Loop testerjr $ra

Page 11: CDA 3100

Question 1funct:

li $t0, 0 # Counterli $v1, 1000000 # Arbitrarily large distance

funct_L0:sll $t1, $t0, 2 #add $t1, $t1, $a0 # Load element from array at counterlw $t1, 0($t1) #sub $t2, $t1, $a1 # Finds distance between element and parameterbgt $t2, $0, funct_L1 # do next if $t2 < 0sub $t2, $0, $t2 # $t2 = abs($t2)

funct_L1:bgt $t2, $v1, funct_L2 # Update returns when ???ori $v0, $t0, 0 # Return array indexori $v1, $t2, 0 # Return ???

funct_L2:addi $t0, $t0, 1 # Increment counterblt $t0, $a2, funct_L0 # Loop testerjr $ra

Page 12: CDA 3100

Question 1funct:

li $t0, 0 # Counterli $v1, 1000000 # Arbitrarily large distance

funct_L0:sll $t1, $t0, 2 #add $t1, $t1, $a0 # Load element from array at counterlw $t1, 0($t1) #sub $t2, $t1, $a1 # Finds distance between element and parameterbgt $t2, $0, funct_L1 # do next if $t2 < 0sub $t2, $0, $t2 # $t2 = abs($t2)

funct_L1:bgt $t2, $v1, funct_L2 # Update returns when new distance is smallestori $v0, $t0, 0 # Return array indexori $v1, $t2, 0 # Return distance

funct_L2:addi $t0, $t0, 1 # Increment counterblt $t0, $a2, funct_L0 # Loop testerjr $ra

Page 13: CDA 3100

Question 1.data

A: .word 21,3,2,9,100,22,6,15,33,90

.text

.globl mainmain:

la $a0, A # address of arrayli $a1, 17 # find element closest to thisli $a2, 10 # length of arrayjal funct

li $v0, 10 syscall

Page 14: CDA 3100

Question 1.data

A: .word 21,3,2,9,100,22,6,15,33,90

.text

.globl mainmain:

la $a0, A # address of arrayli $a1, 17 # find element closest to thisli $a2, 10 # length of arrayjal funct # returns index and element closest to $a1

li $v0, 10 syscall

Page 15: CDA 3100

Question 1.data

A: .word 21,3,2,9,100,22,6,15,33,90

.text

.globl mainmain:

la $a0, A # address of arrayli $a1, 17 # find element closest to thisli $a2, 10 # length of arrayjal 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

Page 16: CDA 3100

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 “00000000000000000000000000011110” 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.

Page 17: CDA 3100

Question 2

• Add return instruction because function

F1:jr $ra

Page 18: CDA 3100

Question 2

• Use srl to be able to count bits

F1:srl $a0, $a0, 1jr $ra

Page 19: CDA 3100

Question 2

• Set up post test loop; loop if $a0 has a 1 bit

F1: srl $a0, $a0, 1bne $a0, $0, F1jr $ra

Page 20: CDA 3100

Question 2

• Count index position

F1: addi $v0, $v0, 1 srl $a0, $a0, 1bne $a0, $0, F1jr $ra

Page 21: CDA 3100

Question 2

• Initialize index position

F1: addi $v0, $0, -1F1_L1: addi $v0, $v0, 1

srl $a0, $a0, 1bne $a0, $0, F1_L1jr $ra

Page 22: CDA 3100

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


Recommended