0
votes

How can I input unsigned integer 16 (uint16, halfword) from console and store it in MIPS? Should I load it as string or int? I know I can use syscalls with different trap codes for input, but how can I input uint16?

read_int        5   
read_float      6   
read_double     7 
read_string     8
1
If this is analogy with DOS/BIOS system calls on x86, I have to say that MIPS is designed for modern systems that have their own console drivers and delegate much of the functionality to OS. So this question must be OS-specific or you need to write your own console drivers.Dmytro Sirenko
I am using SPIM MIPS simulator. Thank you.user2275785
just read as int and use the low 16 bitsphuclv

1 Answers

1
votes

As Luu Vinh Phuc suggested, you can use the read int syscall to get an unsigned 16-bit integer. The syscall will actually read in a 32-bit signed integer, but for the entire range of 16-bit unsigned integers, the low 16 bits will be the same. You can manually check that the high 16 bits are zero to verify that there is no overflow.

li $v0, 5                    # this is the code for reading an integer
syscall                      # execute the syscall, and read the int into $v0
srl $t0, $v0, 16             # get just the high 16 bits
bne $t0, $0, input_overflow  # branch somewhere else if you have an overflow

This will leave you with an unsigned 16 bit integer in register $v0.