Reading User Input: Storing Each Character

  • Thread starter Thread starter ACLerok
  • Start date Start date
  • Tags Tags
    Input Reading
Click For Summary
SUMMARY

This discussion focuses on reading user input in MIPS assembly language to parse mathematical expressions. The user aims to store individual characters from an input string using the read string system service (syscall 8) and is advised to allocate sufficient memory for the input. It is established that a minimum of 9 bytes is necessary to accommodate a typical expression format, including numbers and operators. The conversation emphasizes the importance of correctly indexing and storing characters in registers for further processing.

PREREQUISITES
  • MIPS assembly language syntax and structure
  • Understanding of system calls in MIPS (e.g., syscall 8)
  • Memory allocation and data segment usage in MIPS
  • Character encoding and ASCII representation
NEXT STEPS
  • Learn about MIPS assembly string manipulation techniques
  • Study how to implement character parsing in MIPS
  • Explore MIPS assembly arithmetic operations and their implementation
  • Investigate memory management in MIPS for dynamic input sizes
USEFUL FOR

Students and developers working with MIPS assembly language, particularly those interested in input handling and string processing in low-level programming environments.

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:
Technology 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.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
Replies
1
Views
8K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 4 ·
Replies
4
Views
5K
Replies
17
Views
10K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K