Search code examples
assemblymipsmips32

Do I need to start a String with a null value if I reserve it's space as a .space in MIPS32 assembly?


I'm trying to reserve space to store a string in it using a .space:

.data
  myString: .space 16

I've got an example code from my teacher that says that I must start that string with a null value (0), I don't understand why though, why is this needed?

Thanks in advance!


Solution

  • It doesn't matter what's in the buffer if your code definitely writes the buffer before it ever reads it. Otherwise it does matter that it starts with 0, so it's the empty string if read as an implicit-length 0-terminated string.

    But you already are doing that: .space fills with zeros. The following would end up with exactly the same 16 bytes of zeros in memory:

    myString:
      .byte 0
      .space 15
    

    If you want to make it extra explicit in the source that there's a 0 at the start, this is worth considering. (e.g. if you pass this address to something that reads it before the first write). Even then, if you know what .space does, it's still just clutter. A comment on the .space would be sufficient.

    I'm not sure where you'd find documentation for classic MIPS assemblers like MARS that .space fills with zero, but that's explicitly the case for a Unix assembler like GNU as that's mostly compatible with classic MIPS assemblers: https://sourceware.org/binutils/docs/as/Space.html