I try to write a simple MIPS program that accepts user input which is a string, then checks if the user does not input anything to quit the program. I try to use beq to compare the user's input and an empty string, but I do not know how to assign an empty string to a register.
.data userinput: .space 8 .txet loop: li $v0,8 la $a0,userinput syscall li $v0, 4 la $a1, // try to assgin an empty string to $a1 beq $a0,$a1, exit j loop exit: li $v0 10 syscall
asked Feb 27, 2021 at 1:06
xinrui cheng xinrui cheng
1 1 1 bronze badge
I believe it will have a newline followed by a nul character. I don't think any user input using syscall #8 could legitimately have any further character data after the newline, so I think for the empty input string test, it would be sufficient to look for the newline as the first character of the string.
A truly empty string would start (and finish) with a single NUL terminating byte, and that's the character we would look for instead of looking for the newline character, but MARS syscall #8 appends the newline character (that the user types to complete the input) into the characters of input, so there's a difference then of looking for a truly empty string and looking for the minimal possible (empty) input from syscall #8.