How to write constants in the data segment in x86 assembly

0

I am writing an assembly program (with icc as the assembler). I need to write some constants in the data section for rip relative loading in the main program, for example for the following instruction:

vmovdqu msg(%rip),%ymm0

I now write the data section as this:

        .data
msg: 0x00000000 0x01000000 0x02000000 0x03000000 0x04000000 0x05000000 0x06000000 0x07000000

However, the assembler gives an error: test1.s:140725: Error: junk at end of line, first unrecognized character is `0'.

Can anybody give me an example of how to properly format this data section?

assembly
x86
x86-64
gnu-assembler
asked on Stack Overflow Sep 14, 2020 by bumpbump • edited Sep 14, 2020 by Peter Cordes

1 Answer

1

This looks like the syntax used by the GNU assembler (GAS). On x86-64, the directive for assembling 32-bit integer data is any of .int, .long or .4byte (they are synonymous). (Note .long does mean 4 bytes even though the C type long int is 8 bytes on this platform.)

Multiple values can be separated with commas.

So you could write

        .data
msg:    .int 0x00000000, 0x01000000, 0x02000000, 0x03000000 # and so on

Also available are .byte, .word / .2byte, and .quad / .8byte for 8-, 16- and 64-bit integers.

See the GAS manual for more details.

Additional comments as suggested by Peter Cordes (thanks!):

Since this is going to be loaded as a vector, you probably want to ensure that it is aligned on a 32-byte boundary, which can be done by placing a .balign 32 directive immediately before the msg: label.

If this is truly a constant and does not need to be written elsewhere in your program, then it is best to place it in the read-only data section, by using .section .rodata in place of .data. This will make it possible to place it in shared memory if multiple processes are running your program simultaneously, and will also ensure you get an exception if you try to write it by mistake.

answered on Stack Overflow Sep 14, 2020 by Nate Eldredge • edited Sep 14, 2020 by Nate Eldredge

User contributions licensed under CC BY-SA 3.0