Question

I wrote this simple program in MIPS assembly:

.data
.text
main:
li $v0, 12 # read_char
syscall
move $a0, $v0
li $v0, 11 # print_char
syscall
j main # repeat forever

When I run it in QtSpim it works correctly: it takes a character from the console and it prints it as soon as it is entered, then repeats. QtSpim's console, though, doesn't allow me to paste the input for my program (I can do it, but it won't be read). So I tried running it in spim for Linux (command line), version 9.1.8. It works, but it doesn't react immediately when a character is entered; it always waits for a newline, even when entering characters without cut and paste. So I expect this behavior:

(spim) load "echo.asm"
(spim) run
hheelllloo

but I get this one instead:

(spim) load "echo.asm"
(spim) run
hello [I press Enter here]
hello

Any ideas? Thanks in advance.

Was it helpful?

Solution

This is because the terminal is, by default, line buffered. A read from the terminal will not return until an entire line has been read. This is to allow things like backspace to work with programs that do not use a specialized terminal handling library (e.g. ncurses, readline). You can disable this buffering by running the command

stty -icanon

and then run spim and the program will echo the characters as they are typed. When you're done you should re-enable buffered input with

stty icanon

as the unbuffered mode might cause problems with other programs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top