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

Time:10-06

Currently trying to convert this line

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

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

CodePudding user response:

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 ... 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, e.g. .ascii "\x99\no yes" would give you a 0xa newline following the non-ASCII byte, , not a 0x6e 'n'.

CodePudding user response:

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.

  • Related