Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. # Does some bracket matching stuff
  2. .data
  3. inputString:
  4. .space 42 # inputString will be null terminated and have a newline at the end
  5. bracketStack:
  6. .space 40 # the stack will only need to hold a maximum of 40 entries though
  7. startMess:
  8. .asciiz "At "
  9. mismatchMess:
  10. .asciiz ": mismatching close bracket\n"
  11. unexpectedClose:
  12. .asciiz ": unexpected close bracket\n"
  13. unclosedMess:
  14. .asciiz ": unclosed open bracket(s)\n"
  15. successMess:
  16. .asciiz ": all brackets matched\n"
  17.  
  18. .globl main
  19.  
  20. .text
  21. main:
  22. # =============================
  23. # Begin Program Code
  24. # =============================
  25. # -----------------------------
  26. # Initialize pointer registers
  27. la $s0, 0 # s0 points to where we are in the inputstring
  28. la $s1, -1 # s1 is our stack pointer
  29.  
  30. # -----------------------------
  31. # Read the input string in
  32. # -----------------------------
  33. li $v0, 8 # Tell syscall to read a string
  34. la $a0, inputString # Telling it where to put the string
  35. li $a1, 42 # How long the string can be
  36. syscall # Read the string
  37.  
  38. # --------------------------------
  39. # Do the bracket stuff
  40. # --------------------------------
  41. # We could be much more efficient than putting the entire char on the stack,
  42. # a single bit can hold all the necessary information and we *could* make use
  43. # of this, but for simplicity's sake we won't
  44.  
  45. mainloop:
  46. lb $t0, inputString($s0) # Load next char into a temporary variable
  47. addi $s0, $s0, 1 # Then update the location of the next char
  48. beq $t0, 10, end
  49. beq $t0, '(', pushchar # Push the character onto the
  50. beq $t0, '[', pushchar # stack if it's an open bracket
  51. beq $t0, ')', testparen # Compare the character with the
  52. beq $t0, ']', testbrace # Top of the stack if it's a close bracket
  53. b mainloop # It's not a bracket so ignore it and continue looping
  54.  
  55. pushchar:
  56. add $s1, $s1, 1 # Increment the stack pointer
  57. sb $t0, bracketStack($s1)
  58. b mainloop
  59.  
  60. popchar:
  61. bltz $s1, mainloop
  62. add $s1, $s1, -1 # Decrement the stack pointer
  63. b mainloop
  64.  
  65. testparen:
  66. bltz $s1, unexpected
  67. lb $t1, bracketStack($s1)
  68. beq $t1, '(', popchar
  69. beq $t1, '[', mismatch
  70.  
  71. testbrace:
  72. bltz $s1, unexpected
  73. lb $t1, bracketStack($s1)
  74. beq $t1, '[', popchar
  75. beq $t1, '(', mismatch
  76.  
  77. mismatch:
  78. li $s2, 1 # Tell it there was an error
  79. la $a1, mismatchMess
  80. jal printerror
  81. b popchar
  82.  
  83. unexpected:
  84. li $s2, 1 # Tell it there was an error
  85. la $a1, unexpectedClose
  86. jal printerror
  87. b popchar
  88.  
  89. unclosed:
  90. li $s2, 1 # Tell it there was an error
  91. la $a1, unclosedMess
  92. jal printerror
  93. b reallyend
  94.  
  95. success:
  96. la $a1, successMess
  97. jal printerror
  98. b reallyend
  99.  
  100. printerror: # a1 points to the error string
  101. li $v0, 4 # Set syscall to print string
  102. la $a0, startMess # The message start
  103. syscall # print it
  104. li $v0, 1 # Set syscall to print int
  105. move $a0, $s0 # The current offset for grabbing chars from
  106. syscall # print it
  107. li $v0, 4 # Set syscall to print string
  108. move $a0, $a1 # The argument given
  109. syscall
  110. jr $ra
  111.  
  112.  
  113. end:
  114. bgez $s1, unclosed
  115. beqz $s2, success
  116. reallyend:
  117. li $v0, 10
  118. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement