Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. .data
  2. msg1:.asciiz "Please insert text (max 20 characters): "
  3. msg2:.asciiz "nThe length of the text is: "
  4.  
  5. newline: .asciiz "n"
  6.  
  7. str1: .space 20
  8. .text
  9. .globl main
  10. main:
  11. addi $v0, $v0,4
  12. la $a0,msg1
  13. syscall #print msg1
  14. li $v0,8
  15. la $a0,str1
  16. addi $a1,$zero,20
  17. syscall #get string 1
  18.  
  19. la $a0,str1 #pass address of str1
  20. jal len
  21.  
  22. len:
  23. addi $t2, $zero, 0 # $t2 is what we want to return in the end -- the count of the length of the character array
  24. addi $s1, $zero, 0 # Index i for array traversing | init. to 0 | i = 0
  25.  
  26. Loop:
  27.  
  28. add $s1, $s1, $a0 # Adds the location of the index to the location of the base of the array | $t1 is now the location of: array[index]
  29. lw $t0, 0($s1)
  30.  
  31. beq $t0, $zero, exit
  32. addi $t2, $t2, 1 # Count = Count + 1
  33. addi $s1, $s1, 1 # i = i + 1
  34. j Loop
  35.  
  36. exit:
  37. la $a0,msg2
  38. li $v0,4
  39. syscall
  40. move $a0,$t0 #output the results
  41. li $v0,1
  42. syscall
  43.  
  44. li $v0,10
  45. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement