Help with MIPS

  • Thread starter ACLerok
  • Start date
195
0
I'm trying to write a program that will have the user enter in an expression in the form: A (operation) B (operation) C and print out the result. After the user enters in the expression, I want to parse out each individual digit or expression by reading the ASCII code of each individual character by storing each digit or operation in its own register. I'm having trouble reading each seperate character after the user enters the expression.

li $v0, 8
la $a0, expr
li $a1, 20
syscall

How do I store each character? Something like the code below? (I know the code below will not work.) Am i supposed to store the string in a new register before reading each seperate byte?

sb 0(expr), $t0



Also, since i'm only working with numbers between 20 and 99, and the operations + and *, is it ok to reserve only 5 bytes for the entire expression since 1 byte can represent one ASCII character?
 
Last edited:
32,736
4,468
The OP hasn't been around for 11+ years, but others might be interested.
I'm trying to write a program that will have the user enter in an expression in the form: A (operation) B (operation) C and print out the result. After the user enters in the expression, I want to parse out each individual digit or expression by reading the ASCII code of each individual character by storing each digit or operation in its own register. I'm having trouble reading each seperate character after the user enters the expression.

li $v0, 8
la $a0, expr
li $a1, 20
syscall
You're apparently trying to input the expr string from the keyboard, using the read string system service, so you need memory somewhere that's capable of holding this string.

Here's a minimal way you could do this:
Code:
    .data
expr:  .asciiz  space 12

    .text
    la $a0, expr
    li $v0, 8        # Read string service
    syscall
It would be better to issue a prompt telling the user what needs to be entered, with maybe an example.
ACLerok said:
How do I store each character? Something like the code below? (I know the code below will not work.) Am i supposed to store the string in a new register before reading each seperate byte?

sb 0(expr), $t0
In my example above, the entire string is store in the user data segment, beginning at the address of of the expr label.

If $t1 held the index of the current character, you could load it into $t2 with this code:
Code:
lb $t2, expr($t1)
ACLerok said:
Also, since i'm only working with numbers between 20 and 99, and the operations + and *, is it ok to reserve only 5 bytes for the entire expression since 1 byte can represent one ASCII character?
No, that's not enough. At minimum, an expression such as DD+DD+DD will take 9 bytes, two for each number, and two for the operators, plus one more for the terminating null character, and doesn't account for extra spaces entered by the user, as in 12 + 34 - 56.

That's really the easy part, assuming program reads the string in a single operation. You would then have to parse each character of the string, determining whether the first and possibly the second character are ASCII codes for numerals, then determine what operation is intended, and repeat for the 2nd number, 2nd operation, and 3rd number.

It's probably a lot easier to
Read a number (which could be 1 or 2 digits)
Read an operator (such as +, -, etc.)
Read another number
Read another operator
Read the last number

At that point your code could apply the operations to the number and get the final result.
 

Want to reply to this thread?

"Help with MIPS" You must log in or register to reply here.

Related Threads for: Help with MIPS

Replies
3
Views
2K
  • Posted
Replies
5
Views
5K
Replies
1
Views
4K
Replies
4
Views
14K
Replies
5
Views
7K
Replies
1
Views
5K
  • Posted
Replies
1
Views
2K

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top