mipsmips32qtspim

MIPS32 read_string of unknown length


I've been experimenting with the read_string syscall (buffer in $a0, length in $a1) and found out that if I input more than length characters, only length - 1 chars will be written in memory.

Naturally, my question is: is there a way to read a string of unknown size apart from reading byte-by-byte and dynamically allocating memory in stack? I haven't found anything about this in the documentation (or anywhere, for that matter).


Solution

  • Is there a way to read a string of unknown size apart from reading byte-by-byte

    No, if you want to read strings, either without requiring a carriage return terminator -or- without statically choosing a buffer length, you'll have to read characters individually.

    and dynamically allocating memory in stack?

    The stack is one way, but since it grows toward lower addresses, and strings "grow" toward higher addresses, you'll need some adjustment like a copy after you know the length.

    The heap is another way; you'll need to keep growing the heap in whatever increments you want, but it grows in the same direction as the strings do.  The heap gives you sbrk functionality (extend my address space, not malloc/free, so when you're done with the memory you can reuse yourself but cannot "free" it as such).