Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Does some bracket matching stuff
- .data
- inputString:
- .space 42 # inputString will be null terminated and have a newline at the end
- bracketStack:
- .space 40 # the stack will only need to hold a maximum of 40 entries though
- startMess:
- .asciiz "At "
- mismatchMess:
- .asciiz ": mismatching close bracket\n"
- unexpectedClose:
- .asciiz ": unexpected close bracket\n"
- unclosedMess:
- .asciiz ": unclosed open bracket(s)\n"
- successMess:
- .asciiz ": all brackets matched\n"
- .globl main
- .text
- main:
- # =============================
- # Begin Program Code
- # =============================
- # -----------------------------
- # Initialize pointer registers
- la $s0, 0 # s0 points to where we are in the inputstring
- la $s1, -1 # s1 is our stack pointer
- # -----------------------------
- # Read the input string in
- # -----------------------------
- li $v0, 8 # Tell syscall to read a string
- la $a0, inputString # Telling it where to put the string
- li $a1, 42 # How long the string can be
- syscall # Read the string
- # --------------------------------
- # Do the bracket stuff
- # --------------------------------
- # We could be much more efficient than putting the entire char on the stack,
- # a single bit can hold all the necessary information and we *could* make use
- # of this, but for simplicity's sake we won't
- mainloop:
- lb $t0, inputString($s0) # Load next char into a temporary variable
- addi $s0, $s0, 1 # Then update the location of the next char
- beq $t0, 10, end
- beq $t0, '(', pushchar # Push the character onto the
- beq $t0, '[', pushchar # stack if it's an open bracket
- beq $t0, ')', testparen # Compare the character with the
- beq $t0, ']', testbrace # Top of the stack if it's a close bracket
- b mainloop # It's not a bracket so ignore it and continue looping
- pushchar:
- add $s1, $s1, 1 # Increment the stack pointer
- sb $t0, bracketStack($s1)
- b mainloop
- popchar:
- bltz $s1, mainloop
- add $s1, $s1, -1 # Decrement the stack pointer
- b mainloop
- testparen:
- bltz $s1, unexpected
- lb $t1, bracketStack($s1)
- beq $t1, '(', popchar
- beq $t1, '[', mismatch
- testbrace:
- bltz $s1, unexpected
- lb $t1, bracketStack($s1)
- beq $t1, '[', popchar
- beq $t1, '(', mismatch
- mismatch:
- li $s2, 1 # Tell it there was an error
- la $a1, mismatchMess
- jal printerror
- b popchar
- unexpected:
- li $s2, 1 # Tell it there was an error
- la $a1, unexpectedClose
- jal printerror
- b popchar
- unclosed:
- li $s2, 1 # Tell it there was an error
- la $a1, unclosedMess
- jal printerror
- b reallyend
- success:
- la $a1, successMess
- jal printerror
- b reallyend
- printerror: # a1 points to the error string
- li $v0, 4 # Set syscall to print string
- la $a0, startMess # The message start
- syscall # print it
- li $v0, 1 # Set syscall to print int
- move $a0, $s0 # The current offset for grabbing chars from
- syscall # print it
- li $v0, 4 # Set syscall to print string
- move $a0, $a1 # The argument given
- syscall
- jr $ra
- end:
- bgez $s1, unclosed
- beqz $s2, success
- reallyend:
- li $v0, 10
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement