0
votes

I have a behind-the-scenes question about how comments in MIPS are stored generally. Mainly: why don't they affect instructions that are dependent on location? Does this have to do with how they are stored?


Lets say, for example, that I have a jump statement like

       j Label

       # I'm a comment. I don't do anything!

Label: 

That instruction for the jump might be 0x08100007 in hex. The instruction would be the same if that comment were there or not. At least, according to MARS 4.5, my MIPS simulator.

But, if I were to instead put a meaningful instruction inbetween the label and the jump statement, the hex code is incremented.

       j Label

       addi $t0, $t0, 10

Label: 

According to MARS, the instruction for the jump is actually incremented to 0x08100008 in hex.


So, how come comments don't shift the address to where the jump instruction needs to go to?

1
Comments are not present in the output. They are removed during assembling.Jester
They aren't stored in any way. They are for your convenience, MARS ignores them.Seva Alekseyev
how come comments don't shift the address to where the jump instruction needs to go to - huh? Comments don't emit any bytes into the output file so they're zero width, like .space 0 or .word with no operand. (e.g. .word 1, 2 is 8 bytes wide, .word is 0 bytes wide.) Or like labels. Putting other labels like foo: and bar: there would also not emit any bytes into the output between j Label and the label itself, because labels are zero width markers you can refer to.Peter Cordes
So TL:DR: not only do comments not have an address (you can't refer to them later, and they can appear outside any section), they wouldn't displace labels even if they could be considered to have an address. I think you have some deeper misconception about how an assembler works and this is a symptom of it. I like to think about an assembler as just emitting bytes into an output file, according to the rules given by source lines.Peter Cordes

1 Answers

1
votes

The processor doesn't know or see:

  • comments
  • data declarations
  • labels

It only sees machine code instructions — and these tell it everything it needs to know about

  • what instruction to execute next
  • how to interpret data (variables/storage locations)

As comments have no representation in machine code, they are removed by the compiler or assembler.