Patrick Taylor
Patrick Taylor

Reputation: 122

How do I count the number of characters entered using MIPS?

I prompt the user to enter a string of up to 40 characters. How do I count how many characters the user entered? As I count each character, I need to store the count of digits, uppercase and lowercase letters, spaces, and any other characters. How should I recognize the difference between these types of characters?

.text                # beginning of code
.globl main          # beginning of main

main:                # main procedure
   li  $v0, 4        # print_string service number
   la  $a0, prompt00 # load address of prompt
   syscall           # print prompt

   li  $v0, 8        # read_string service number
   la  $a0, buffer   # load address of buffer
   la  $a1, 40       # max length of 40
   syscall           # read_string

   li $v0, 4         # print_string service number
   la $a0, buffer    # load address of buffer
   syscall           # print buffer

   li  $v0, 10       # using service 10, terminate
   syscall           # terminate

.data                # beginning of data area

   buffer:           # container for input string
      .space  40     # max length of 40 characters
   newline:          # variable to represent a newline
      .asciiz "\n"   # a newline character

   prompt00: .asciiz "Enter up to 40 characters: "

Upvotes: 0

Views: 6252

Answers (1)

mcgov
mcgov

Reputation: 36

You can check to see if the character in the register you are looking at falls within any of the applicable ranges: ASCII Values Table.

Strings should be stored with a null character '\0' at the end, so you can look for that to find the length.

Upvotes: 2

Related Questions