80 likes | 240 Views
CDA 3100. Recitation Week 5. Write a MIPS Program to do the Following:. Count the number of times a value v appears in an array A of n elements. Use the following: A = [12, 34, 67, 1, 45, 90, 11, 33, 67, 19] n = 10 v = 67. Answer: SPIM Setup. .text . globl main main:
E N D
CDA 3100 Recitation Week 5
Write a MIPS Program to do the Following: • Count the number of times a value v appears in an array A of n elements. • Use the following: • A = [12, 34, 67, 1, 45, 90, 11, 33, 67, 19] • n = 10 • v = 67
Answer: SPIM Setup .text .globl main main: done: li $v0, 10 syscall
Answer: Array Data .data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 .text .globl main main: done: li $v0, 10 syscall
Answer: Init Variables .data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 .text .globl main main: la $s0, A # base address of A li $s1, 10 # n li $s2, 67 # v ori $s3, $zero, 0 # accumulator ori $t0, $zero, 0 # index in A done: li $v0, 10 syscall
Answer: Setup Loop .data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 .text .globl main main: la $s0, A #base address of A li $s1, 10 #n li $s2, 67 #v ori $s3, $zero, 0 # accumulator ori $t0, $zero, 0 # index in A loop: addi $t0, $t0, 1 beq $t0, $s1, done j loop done: li $v0, 10 syscall
Answer: Load Values From Array .data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 .text .globl main main: la $s0, A #base address of A li $s1, 10 #n li $s2, 67 #v ori $s3, $zero, 0 # accumulator ori $t0, $zero, 0 # index in A loop: sll $t1, $t0, 2 add $t1, $s0, $t1 lw $t2, 0($t1) addi $t0, $t0, 1 beq $t0, $s1, done j loop done: li $v0, 10 syscall
Answer: Increment Accumulator .data A: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 .text .globl main main: la $s0, A #base address of A li $s1, 10 #n li $s2, 67 #v ori $s3, $zero, 0 # accumulator ori $t0, $zero, 0 # index in A loop: sll $t1, $t0, 2 add $t1, $s0, $t1 lw $t2, 0($t1) bne $t2, $s2, noinc addi $s3, $s3, 1 noinc: addi $t0, $t0, 1 beq $t0, $s1, done j loop done: li $v0, 10 syscall