Interpreting Strings

Reading a string

                .data
string:         .byte   0:20
		.text
		li      $v0, 8
		la      $a0, string
		li      $a1, 20
		syscall

Result is stored in the memory pointed to by 'string'.
The result includes all characters until a newline is encountered or until 19 characters have been read. (The final character is used for null termination, like in C.)

Determining the type of input

  • Number: '0'<=x<='9'
  • White Space: ' ' or '\t' or '\n'
  • Quit: 'q'
  • Else: Look for an operator
  • Converting a char to an int

    Character -> value in computer
    Digits are stored in order.
    So char - '0' = int

    Converting a string to an int

    x=0
    Loop
      if next_char not in ['0',...,'9']
        break
      x = 10*x + char_to_int(next_char)