Jump to content

MIPS Assembly problem

- - - - -

  • Please log in to reply
5 replies to this topic

#1
srose

srose

    Newbie

  • Members
  • Pip
  • 3 posts
Hi
I have to create a program in MIPS which parses brackets meaning that it checks that for every open bracket there is a closing bracket. It will check this on both round and square brackets in a string.

It has do display the following errors:
At <c>: mismatching close bracket
At <c>: unexpected close bracket
At <c>: unclosed open bracket(s)
for any corresponding problem in the inputted string. the c is replaced with the number of characters along the error occured.
For example:
the input string “ab]cde(fgh])(ij”, would output
At 3: unexpected close bracket
At 11: mismatching close bracket
At 12: unexpected close bracket
At 16: unclosed open bracket(s)


I am not sure how I should implement this. Would I load the ASCII codes for each type of bracket and then check if each inputted character matches any of the ASCII codes and if it does add one to a counter for that particular bracket. At the end it would compare the counters and if they were not equal an error would occur?

I'll show you the code I have written anyway but I think I migt be going about this all wrong.

I am new to assembly so any help would be appreciated.

	.data


error1msg: 	.ascii "At <c>: mismatching close bracket.\n"

error2msg: 	.ascii "At <c>: unexpected close bracket.\n"

error3msg:	.ascii "At <c>: unclosed open bracket(s).\n"


str_input:

	.space 40

	.text


# ASCII for '(' = 40

# ASCII for ')' = 41

# ASCII for '[' = 91

# ASCII for ']' = 93



	.text

main:

	li	$t0, 40			# load ( ASCII code into $t0

	li	$t1, 41			# load ) ASCII code into $t1

	li      $t2, 91			# load [ ASCII code into $t2

	li	$t3, 93			# load ] ASCII code into $t3

	

	

	li 	$v0, 8			# load read string system call command

	la 	$a0, str_input 		# put string address in $a0

	li	$a1, 40			# set length to 40 characters

	syscall				# store inputted string


	li	$t4, 0			# push 0

	la	$s0, ($a0)		# set custom stack pointer to point to string

	subu	$s0, 4			# go down 4 in stack

	sw      $t4,($s0)   		# to signal its bottom

   	li      $t5, 0      		# index of first char in str buffer

	

charcheck:	

	li	$v0, 12			# read char

	la  	$a2, ($t5)		# load char into $a2

	beqz 	$a2, endstr		# if end of string goto endstr

	

	beq	$a2, $t0, openrndbracket 	#If open round bracket goto openrndbracket

	beq	$a2, $t1, closerndbracket	#If close round bracket goto closerndbracket	

	beq	$a2, $t2, opensqrbracket 	#If open square bracket goto opensqrbracket	

	beq	$a2, $t3, closesqrbracket 	#If close square bracket goto closerndbracket


	addi $s0, $s0, 4	# Next word in stack

	addi $t5, $t5, 1	# increment char index

	j charcheck		# loop


	bne	$t4, $t5, error1 # if unequal open and close round brackets then goto error1.

	sw	$ra, $s0	# Store return address.

endstr:

	li	$t1, 0		# index of first char in str buffer

	

	

	

openrndbracket:

	li	$t4, 0			# opening bracket counter

	addi $t4, 1			# increment counter

	j	charcheck		# return to charcheck

	

	

closerndbracket:

	li	$t5, 0			# closing bracket counter

	addi $t5,1			# increment counter

	j charcheck			# return to charcheck


opensqrbracket:

	li	$t6, 0			# opening sqrbracket counter

	addi $t6, 1			# increment counter

	j	charcheck		# return to charcheck


closesqrbracket:

	li	$t7, 0			# closing sqrbracket counter

	addi $t7, 1			# increment counter

	j	charcheck		# return to charcheck

	

error1:

	la    $a0,error1		# load "mismatching close bracket" error message

  	li    $v0,4			# load print message syscall command

	syscall				# write "mismatching close bracket" error message

	jr	$ra			#  "jump register"


error2:

	la    $a0,error2		# load "unexpected close bracket" error message

    	li    $v0,4			# load print message syscall command

	syscall				# write "unexpected close bracket" error message

	jr	$ra			#  "jump register"

	

error3:

	la    $a0,error3		# load "unclosed open bracket" error message	

 	li    $v0,4			# load print message syscall command

	syscall				# write "unclosed open bracket" error message

	jr	$ra			#  "jump register"



#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Do they have to be in order? For example, is [ ( ] ) legal, or does it have to be ( [] )? If it's the latter you should use a stack to keep track of things.
sudo rm -rf /

#3
srose

srose

    Newbie

  • Members
  • Pip
  • 3 posts
Hi
Brackets have to be properly nested so it would be the latter.

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Ok, so go with a stack then. As long as you have an empty stack at EOF or don't mismatch braces on a pop you won't need counters.
sudo rm -rf /

#5
srose

srose

    Newbie

  • Members
  • Pip
  • 3 posts
ok I'll try. Thanks for the help.

EDIT: Ok quick question. If the string is valid and therefore the stack is empty, how do I check if the stack is empty?

Edited by srose, 24 October 2010 - 02:37 PM.


#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
See if the pointer to the top of the stack is the same as the pointer to the bottom.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users