0

I am writing a program in which user inputs a string. This string is stored in a register $v0 by default, but I want to copy the data inside $v0 to String label, so that if I use $v0 for other purpose in the program, the user input doesn't get corrupt. I get figure out how to do it. I will be very glad if someone could tell me how to do it.

Regards

Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
user1698102
  • 127
  • 2
  • 4
  • 9

1 Answers1

0

In Spim, the service to read a string from the user is service 8, which takes as argument:

  • $a0: a pointer to the buffer where the string will be written
  • $a1: the size of the buffer

Upon return of the syscall, the buffer will be ovewritten with the input text (up to a maximum of $a1 bytes), $v0 is unaffected

Example usage:

.data
buffer: .space 128

.text
  la $a0, buffer
  li $a1, 128 # maximum number of bytes to be read
  li $v0, 8
  syscall
gusbro
  • 22,357
  • 35
  • 46
  • What is li $a1,128 is doing? Where did you copied the user input from $v0 to buffer? – user1698102 Oct 05 '12 at 09:10
  • @user1698102: The `syscall` to read a string from keyboard receives in `$a0` a pointer to the buffer where the string will be stored and in `$a1` the size of that buffer (that is, the maximum number of bytes to be read from console). You don't have to use `$v0` to read a string from keyboard. Note that `$v0` is just another register (32 bits wide) so it really cannot hold the actual string read. – gusbro Oct 05 '12 at 14:16