Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. # CHAR_COUNT FUNCTION
  2. #
  3. .globl charCount
  4. charCount:
  5.  
  6. #a0 = word (word[0])
  7. #a1 = search (char)
  8. #s1 = result
  9.  
  10. #main wants to get return value from a0 and store in s1
  11. #what will return value be?
  12.  
  13. # *************** CHARCOUNT CALLEE SETUP **********************
  14. # int result;
  15.  
  16. #addi sp, sp, -16 #Make stack 16 byte aligned
  17. #sw ra, 12(sp) #Save caller's return address
  18. #sw s0, 8(sp) #Save caller's frame pointer
  19. #addi s0, sp, 16 #Set frame pointer to first arg
  20.  
  21.  
  22.  
  23. #a0 = word[0]
  24. #a1 = char we're searching for
  25.  
  26. #t1 = value of word[0], first char in word
  27. #t0 == 0. Set to 1 if char found
  28.  
  29.  
  30.  
  31. addi sp, sp, -16 #Make stack 16 byte aligned, create stack space
  32. sw ra, 12(sp) #store return adress in stack pointer
  33. sw s0, 8(sp) #store frame pointer
  34. sw s1, 4(sp) #store local var into stack (result)
  35. addi s0, sp, 12 #set frame pointer to first element
  36.  
  37. #ot sure if these lines inc
  38. andi s1, s1, 0 #Set s1 to 1, (it will be set to 1 if char is found)?
  39. lb t1, (a0) #a0 contains word[0]. load t1 with byte value at word[0] - first char in arr
  40.  
  41.  
  42. # *************** CHAR_COUNT CODE BODY ************************
  43.  
  44. #load mem address of str[0] (&str[0]) into register
  45. #load value (actual character) into register
  46. #branch if 0
  47.  
  48. # if (*str == 0)
  49.  
  50. #caller is result += Charcount
  51.  
  52. # result = 0
  53.  
  54. # else
  55. # {
  56.  
  57.  
  58. # if (*str == c)
  59. # result = 1;
  60.  
  61. # else
  62. # result = 0;
  63.  
  64. #base case
  65. beqz t1 null_char #branch is *str == 0 (null char) TODO: store result before jump
  66. bne t1, a1, not_null_char #else (below if *str == c)
  67. addi s1, s1, 1 #if this executes, *str == c. add 1 to t0 (char found)
  68. b not_null_char
  69.  
  70.  
  71. null_char:
  72. andi a0, a0, 0 #Clear a0, since it is an address.
  73. #We need to use a0 as the result of occurances
  74. sw s1, 4(sp)
  75. b return
  76.  
  77. # *************** CHARCOUNT CALLER SETUP ***********************
  78. not_null_char: #This is the
  79. sw s1, 4(sp) #Store result in stack
  80. addi a0, a0, 1 #Increment array by 1
  81. jal charCount #Repeat with new char in arr
  82.  
  83.  
  84.  
  85.  
  86.  
  87. # result += charCount(str+1, c);
  88.  
  89.  
  90.  
  91. # *************** CHARCOUNT CALLER TEARDOWN ********************
  92.  
  93.  
  94.  
  95.  
  96. # *************** RESUME CHARCOUNT *****************************
  97.  
  98. # }
  99.  
  100.  
  101. # return result
  102.  
  103.  
  104.  
  105.  
  106. #}
  107. # *************** CHARCOUNT CALLEE TEARDOWN *****************************
  108.  
  109.  
  110.  
  111. return:
  112.  
  113. lw ra, 12(sp) #Set return address to stack pointer
  114. lw s1, 4(sp) #Load t0 with result (0 or 1)
  115.  
  116. add a0, a0, s1 #Add result (0 or 1) to a0
  117. #Every time we return, result goes in a0
  118. addi sp, sp, 16 #Pop stack (Remove space created)
  119. ret
  120. #After callee teardown, stack should look like it did before caller setup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement