What is the use of .byte assembler directive in gnu assembly?

vjain27 picture vjain27 · Sep 3, 2011 · Viewed 19.1k times · Source

While going through some C code having inline assembly I came across the .byte (with a Dot at the beginning) directive.

On checking the assembly reference on web I found that it is used to reserve a byte in memory.

But in the code there was no label before the statement. So I was wondering what is use of an unlabeled .byte directive or any other data storage directive for that matter.

For e.g. if i code .byte 0x0a, how can i use it ?

Answer

Carl Norum picture Carl Norum · Sep 3, 2011

There are a few possibilities... here are a couple I can think of off the top of my head:

  1. You could access it relative to a label that comes after the .byte directive. Example:

      .byte 0x0a
    label:
      mov (label - 1), %eax
    
  2. Based on the final linked layout of the program, maybe the .byte directives will get executed as code. Normally you'd have a label in this case too, though...

  3. Some assemblers don't support generating x86 instruction prefixes for operand size, etc. In code written for those assemblers, you'll often see something like:

      .byte 0x66
      mov $12, %eax
    

    To make the assembler emit the code you want to have.