简体   繁体   中英

What is GNU AS equivalent of NASM's db (define bytes)?

Currently trying to convert this line

db 0x7F, "ELF", 1, 1, 1, 0

to GNU AS. I'm not sure what I should use.

GAS doesn't have a single directive that lets you mix strings and integers the way db does with "ELF" being equivalent to "E", "L", "F" . There are separate directives / pseudo-instruction .byte for integer operands, and .ascii for quoted literal text from the source code.

Your options for writing it include:

 .byte 0x7f, 'E', 'L', 'F', 1, 1, 1, 0

 .byte 0x7f
 .ascii "ELF"
 .byte 1, 1, 1, 0

Both are exactly equivalent: all that matters is getting the right bytes assembled into the output for the current section. Having the bytes all come from one directive is irrelevant. If source lines matter, .byte 0x7f; .ascii "ELF"; .byte... .byte 0x7f; .ascii "ELF"; .byte... .byte 0x7f; .ascii "ELF"; .byte... can put everything on the same line, since ; works as a statement separator in GAS. ( # is the comment character.)


But .ascii "\x7FELF" / .byte 1,1,1,0 doesn't work : it gets assembled to fe 4c 46... because of the inconvenient way the \x hex escape works in GAS strings : that reads it as 0x7FE and keeps the low byte of that:

\x hex-digits...
A hex character code. All trailing hex digits are combined. Either upper or lower case x works.

It does work to do .ascii "\x7F\ELF" . That even assembles without warning (in GAS 2.39.0), even though GAS's manual says it will:

\ anything-else
Any other character when escaped by \ gives a warning, but assembles as if the '\' was not present. The idea is that if you used an escape sequence you clearly didn't want the literal interpretation of the following character. However as has no other interpretation, so as knows it is giving you the wrong code and warns you of the fact.

But I wouldn't recommend that hack; it's not in general safe.
eg In .ascii "\x99\no yes" , \n would give you a newline ( 0xa ) following the non-ASCII byte, not the character 'n' ( 0x6e ).

In NASM, db 0x55 declares an initialized byte with the hex value 0x55. In GNU as, that would be .byte 0x55 . There are also.ascii.octa.float for initializing constants.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM