Reading User Input: Storing Each Character

  • Thread starter Thread starter ACLerok
  • Start date Start date
  • Tags Tags
    Input Reading
AI Thread Summary
The discussion focuses on writing a program that accepts a mathematical expression in the format A (operation) B (operation) C and prints the result. The user is encountering challenges in parsing individual characters from the input string after it is entered. The initial code snippet provided uses a syscall to read a string into a predefined memory location, but there are concerns about how to store and access each character correctly.To store each character, it is suggested to use a loop to read the string byte by byte, utilizing the load byte instruction (lb) to access individual characters. The importance of reserving adequate memory for the input is highlighted, as a minimum of 9 bytes is necessary to accommodate the expression format, including numbers, operators, and a null terminator. The discussion emphasizes that parsing should be done sequentially: reading numbers and operators one at a time rather than attempting to process the entire string at once. This structured approach simplifies the logic needed to evaluate the expression correctly.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top