Advertisement
foreverfugazi

find element in array

Sep 18th, 2024 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. # finding element in an array
  2. .data
  3. array: .word 3, 5, 7, 9, 11, 13, 15
  4. n: .word 7 # length of array
  5. key: .word 13
  6.  
  7. .text
  8. start:
  9. la x5, array
  10. lw x6, n # array length
  11. lw x7, key
  12. li x8, 0
  13. search:
  14. beq x8, x6, not_found
  15. lw x9, 0(x5)
  16. beq x7, x9, found
  17. addi x5, x5, 4 # jump to next element
  18. addi x8, x8, 1 # add 1 to counter
  19. j search
  20. not_found:
  21. li x1, 0 # update result = 0
  22. j end
  23. found:
  24. li x1, 1 # update result = 1
  25. j end
  26. end:
  27. nop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement