What's the difference between the .ascii and the .string assembler directives?

≡放荡痞女 提交于 2020-05-12 16:41:32

问题


I know that the .ascii directive doesn't put a null character at the end of the string. The .asciz directive is used for that purpose. However, I don't know whether the .string directive puts a null character at the end of the string. If it does, then what's the difference between the .asciz and the .string directives? To me, having both .ascii and .string seems redundant.


回答1:


Just so this question no longer shows up in "Unanswered"...

According to the binutils docs:

.ascii (Here for reference)

.ascii expects zero or more string literals separated by commas. It assembles each string (with no automatic trailing zero byte) into consecutive addresses.

.asciz

.asciz is just like .ascii, but each string is followed by a zero byte. The “z” in ‘.asciz’ stands for “zero”.

.string

Copy the characters in str to the object file. You may specify more than one string to copy, separated by commas. Unless otherwise specified for a particular machine, the assembler marks the end of each string with a 0 byte.

...

The variants string16, string32 and string64 differ from the string pseudo opcode in that each 8-bit character from str is copied and expanded to 16, 32 or 64 bits respectively. The expanded characters are stored in target endianness byte order.

They all support escape sequences and accept multiple arguments. As for the difference between the two you mentioned:

  • In certain particular architectures, .string will not add the null byte, when .asciz always will. You can do this:
    • echo '.string ""' | gcc -c -o stdout.o -xassembler -; objdump -sj .text stdout.o.
    • If the first byte is 00, then the null character was inserted.
  • .string also has suffixes to expand characters to certain widths (16, 32, or 64).

As stated in the comments to the question, in most cases there isn't going to be a difference other than semantics, but technically, the two pseudo-ops are different.

Addendum:

As it turns out, the docs do mention two architectures that behave differently:

  • HPPA (HP Precision Architecture) - does not add 0, but has a special .stringz directive for that.
  • TI-C54X (Some DSP chip from Texas Instruments) - zero-fills upper 8 bits of each word (2 bytes). Has a related .pstring directive that packs the characters and zero-fills unused space.

Digging through the source code in the gas/config folder, we can confirm this and find one more:

  • IA64 (Intel Architecture) - .string and .stringz behave like HPPA.


来源:https://stackoverflow.com/questions/36854078/whats-the-difference-between-the-ascii-and-the-string-assembler-directives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!