Home > Back-end >  What does .word 0 mean in ARM assembly?
What does .word 0 mean in ARM assembly?

Time:08-13

I'm writing a C state machine for Cortex-M4.
I use ARM GCC 11.2.1 (none).
I'm making a comparison between C and C output assembly.

I have the following C code godbolt link

struct State
{
    virtual void entry(void) = 0;
    virtual void exit(void) = 0;
    virtual void run(void) = 0;
};

struct S1 : public State
{
    void entry(void)
    {
    }

    void exit(void)
    {
    }

    void run(void)
    {
    }
};

S1 s1;

The assembly output is:

S1::entry():
  bx lr
S1::exit():
  bx lr
S1::run():
  bx lr
vtable for S1:
  .word 0
  .word 0
  .word S1::entry()
  .word S1::exit()
  .word S1::run()
s1:
  .word vtable for S1 8

The only difference from C version of this code is the 2 lines .word 0:

vtable for S1:
      .word 0
      .word 0

What does that mean and what does it do?

Here's the C version of the code above I wrote. godbolt link

CodePudding user response:

The C ABI for ARM and the GNU C ABI define which entries must appear in the virtual table.

In the case of your code, the first two entries are the offset to the top of the vtable and the typeinfo pointer. These are zero for now, but may be overwritten if required (eg: if a further derived class is made).

CodePudding user response:

It means the assembler should output the word number 0.

The assembler basically goes through the file from top to bottom, and for each instruction, it calculates the bytes for that instruction (always 4 bytes on ARM), and writes it into the output file.

.word just tells it to output a particular number as 4 bytes. In this case 0.

  • Related