jaredec18

Untitled

Sep 22nd, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. Given below is the MIPS code with output. If it helped, please don't forget to rate the answer. Thank you very much
  2.  
  3.  
  4. .data
  5. prompt: .asciiz "Enter some text: "
  6. len: .word 100
  7. str: .space 100 #maximum 100 characters
  8. charCount: .word 0
  9. wordCount: .word 0
  10. wordsmsg: .asciiz " words "
  11. charsmsg: .asciiz " characters \n"
  12. goodbye: .asciiz "Good Bye!"
  13. progEnd: .asciiz "Message: "
  14. .text
  15.  
  16. Loop:
  17. #show a dialog box to user to get string input
  18. la $a0, prompt
  19. la $a1, str
  20. lw $a2, len
  21. li $v0, 54
  22. syscall
  23. #check status in a1
  24. beq $a1, -2, EndLoop #cancel choosen
  25. beq $a1, -3, EndLoop #blank input and ok choosen
  26. #we can process now, call function count by passing address and lenght in a0 and a1 respectively
  27. la $a0, str
  28. lw $a1, len
  29. jal count
  30. #we receive count of chars and words in v0 and v1 respectively. store in memory
  31. sw $v0, charCount
  32. sw $v1, wordCount
  33. #display the string and counts
  34. la $a0, str #displaying string
  35. li $v0, 4
  36. syscall
  37. #display word count
  38. lw $a0, wordCount
  39. li $v0, 1
  40. syscall
  41. la $a0, wordsmsg
  42. li $v0, 4
  43. syscall
  44. #display char count
  45. lw $a0, charCount
  46. li $v0, 1
  47. syscall
  48. la $a0, charsmsg
  49. li $v0, 4
  50. syscall
  51. b Loop
  52. EndLoop:
  53. #display goodbye message dialog
  54. la $a0, progEnd
  55. la $a1, goodbye
  56. li $v0, 59
  57. syscall
  58. #exit
  59. li $v0, 10
  60. syscall
  61. #-------------------------------------------
  62. #function count receives addrss of string in a0 and length in a1
  63. #returns charcount in v0 and word count in v1
  64. #-------------------------------------------
  65. count:
  66.  
  67. #save s0 on stack by allocating 4 bytes
  68. addi $sp, $sp, -4
  69. sw $s0, 0($sp) #save value of s0 on stack
  70. move $s0, $a0
  71. li $t1, 0 #char count
  72. li $t2, 1 #word count
  73. Loop2:
  74. lb $t3, ($s0)
  75. beq $t3, '\n', EndLoop2 #goto endloop on seeing newline or null terminator
  76. beq $t3, '\0', EndLoop2
  77. add $t1, $t1, 1 #increment char count
  78. beq $t3, ' ', IncrWord
  79. b NextLoc
  80. IncrWord:
  81. addi $t2, $t2, 1
  82. NextLoc:
  83. addi $s0, $s0, 1
  84. b Loop2
  85. EndLoop2:
  86. #restore back s0 from stack
  87. lw $s0, 0($sp)
  88. add $sp, $sp, 4
  89. #return values in v0 and v1
  90. move $v0, $t1
  91. move $v1, $t2
  92. jr $ra
  93.  
  94. https://media.cheggcdn.com/media%2Fecd%2Fecd8ad87-7a58-4459-be3f-27d88706000d%2FphppGZzIQ.png
  95.  
  96. https://media.cheggcdn.com/media%2Fae2%2Fae2eaa89-5be2-4ed8-a357-3c721e1cccc8%2Fphp2Vtr6r.png
  97.  
  98. the lazy brown dog
  99. 4 words 18 characters
  100. good morning. how are you?
  101. 5 words 26 characters
Advertisement
Add Comment
Please, Sign In to add comment