davide1409

ese2.asm

Jan 14th, 2021 (edited)
217
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. num_pow2:
  2.     addi $sp, $sp, -16      # alloco lo stack
  3.     sw $ra, 12($sp)         # salvo $ra
  4.     sw $s0, 8($sp)
  5.     sw $s1, 4($sp)
  6.     sw $s2, 0($sp)
  7.    
  8.     add $s0, $a0, $0        # $s0 = &A[0]
  9.     add $s1, $0, $0         # $s1 = 0 = pow2_counter
  10.     sll $s2, $a1, 2         # $s2 = m*4
  11.     add $s2, $a0, $s2       # $s2 = ( &A[0] + (m*4) ) = &A[m]
  12.    
  13. for:    beq $s0, $s2, exit      # iff $s0 == &A[m] goto exit
  14.     lw $a0, 0($s0)          # $a0 = A[i]
  15.     jal pow2
  16.     beq $v0, $0, else       # iff $v0 == 0 ==> A[i] non è una potenza di due
  17.     addi $s1, $s1, 1        # iff $v0 != 0 ===> A[i] è una potenza di due e allora: pow2_counter++
  18.    
  19. else:   addi $s0, $s0, 4        # A++
  20.     j for
  21.    
  22. exit:   add $v0, $0, $s1        # return $v0 = pow2_counter
  23.    
  24.     lw $s2, 0($sp)
  25.     lw $s1, 4($sp)
  26.     lw $s0, 8($sp)
  27.     lw $ra, 12($sp)         # ripristino $ra
  28.     addi $sp, $sp, 16       # dealloco stack
  29.     jr $ra
  30.  
RAW Paste Data