user3245435
user3245435

Reputation: 11

Assembly - Comparing two integers at same index in 2 arrays

I need some help comparing two elements of different arrays at the same index and counting if how many pairs equal each other. The program is suppose to count how many times the two arrays contain the same integer at the same index. I can't figure out how to include the size (if one is bigger than the other) and also how to print the count of pairs.

Thanks

      .globl  main

main: 
      li      $t1,0 #arrays index
      li      $t2,0 #counter
      li      $t4,0 #count pairs

loop: 
      beq     $t2,10,end
      lw      $v0,arrayA($t1)
      lw      $v1,arrayB($t1)

      beq     $v0, $v1, equal
      addi    $t1,$t1,4
      addi    $t2,$t2,1
      b loop

equal: 
      addi    $t4,$t4,1
      addi    $t1,$t1,4
      addi    $t2,$t2,1
      b loop

end:   
      lw      $a0,($t4)
      li      $v0,1
      syscall

      li      $v0,10
      syscall #Halt

        .data
sizeA:  .word 10
arrayA: .word -1, 0, 3, 6, 8, 10, 21, 11, 14, 10
sizeB:  .word 10
arrayB: .word -2, 0, 7, 2, 12, 9, 2, 19, 20, 15

# end of program

Upvotes: 0

Views: 2144

Answers (1)

Jester
Jester

Reputation: 58792

$t4 isn't an address, so lw $a0,($t4) doesn't make sense. You just want to transfer it into $a0 for printing. You can do that using move $a0, $t4 or you could simply do the counting in $a0 directly.

To consider the length of the arrays, just check that the index is within range of both arrays, or first calculate the common length and use that for the loop.

Upvotes: 1

Related Questions