0

I have the following problem when designing this problem is ASM: I have an array and a number x. I have to compute the sum of the elements which are greater than x and they have at least two odd digits. I managed to check for the first condition but don't know how to check for the second...

.data
    array:      .word 11776, 117760, 341504, 1177600, 11776000, 117760000
    length:     .word 6
    sum:        .word 0
    x:          .word 16
    
.text
    main:
        la $t0, array   # Base address
        li $t1, 0       # i = 0 
        lw $t2, length  # $t2 = length
        li $t3, 0       # sum = 0
        li $t5, x       # x = 0
        syscall
        
        sumLoop:
            lw $t4, ($t0)           # $t4 = array[i]
            $IF($t4 > $t5)
                add $t3, $t3, $t4       # sum = sum + array[i]
            
            add $t1, $t1, 1         # i = i + 1
            add $t0, $t0, 4         # Updating the array address
            blt $t1, $t2, sumLoop   # If i < lenght the loop again
        sw $t3, sum
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • What architecture is this? – 500 - Internal Server Error Jun 16 '21 at 09:57
  • @500-InternalServerError x86 – Matei Anghel Jun 16 '21 at 10:00
  • This is not x86. x86 has registers like EAX, EDI, etc, and [its add instruction is 2-operand](https://www.felixcloutier.com/x86/add), like `add dst, src` does `dst += src`. From the register names and syntax, this looks like MIPS, where you have `add dst, src1, src2` that does `dst = src1 + src2`. – Peter Cordes Jun 16 '21 at 10:30
  • Anyway, you want to jump *over* that `add` when the condition is false, i.e. when the opposite condition is true. – Peter Cordes Jun 16 '21 at 10:31
  • 1
    Oh wait, `$IF($t4 > $t5)` is real syntax, not some placeholder you were trying to implement? To break a number into its base-10 (decimal) digits, see [How to print the first digits of a number in MIPS Assembly Programming?](https://stackoverflow.com/q/26088332). (`odds += digit & 1;` will count how many odd digits you find. Since you need to know you've found at least 2, you can't just OR or ADD them together without clearing high garbage.) – Peter Cordes Jun 16 '21 at 10:36

0 Answers0