如何在 x86 汇编中的数据段中写入常量 [英] How to write constants in the data segment in x86 assembly

查看:23
本文介绍了如何在 x86 汇编中的数据段中写入常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个汇编程序(使用 icc 作为汇编程序).我需要在数据部分写一些常量,以便在主程序中进行rip相对加载,例如以下指令:

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

然而,汇编器给出了一个错误:test1.s:140725:错误:行尾的垃圾,第一个无法识别的字符是‘0’.

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?

推荐答案

这看起来像 GNU 汇编程序 (GAS) 使用的语法.在 x86-64 上,用于组装 32 位整数数据的指令是 .int.long.4byte 中的任何一个(它们是同义词).(注意 .long 确实意味着 4 个字节,即使 C 类型 long int 在这个平台上是 8 个字节.)

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.

所以你可以写

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

还有用于 8-、16- 和 64 的 .byte.word/.2byte.quad/.8byte-位整数.

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

有关详细信息,请参阅GAS 手册.

See the GAS manual for more details.

Peter Cordes 建议的其他评论(谢谢!):

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

由于这将作为向量加载,您可能希望确保它在 32 字节边界上对齐,这可以通过在紧邻之前放置 .balign 32 指令来完成msg: 标签.

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.

如果这确实是一个常量并且不需要写在程序的其他地方,那么最好将它放在只读数据段中,通过使用 .section .rodata.data 的位置.如果多个进程同时运行您的程序,这将使其可以将其放置在共享内存中,并且还可以确保您在尝试错误编写时得到异常.

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.

这篇关于如何在 x86 汇编中的数据段中写入常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆