Reading User Input: Storing Each Character

  • Thread starter Thread starter ACLerok
  • Start date Start date
  • Tags Tags
    Input Reading
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 4K views
ACLerok
Messages
194
Reaction score
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 separate 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 separate 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:
Physics news on Phys.org
The OP hasn't been around for 11+ years, but others might be interested.
ACLerok said:
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 separate 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 separate 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.