Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the .quad directive work in assembly?

I'm having trouble understanding how the .quad function works in assembly.

From what I read online, it generates a 64 bit twos-complement value for each expression into the current section. I understand what twos-complement is and the fact that section is referencing the line that .quad is called to.

Where and when is .quad usually called in assembly?
Also, why use .quad to generate anything?

like image 484
Jack West Avatar asked Dec 06 '22 15:12

Jack West


1 Answers

The .quad directive is used to define 64 bit numeric value(s). In similar way how .byte directive works.

.quad 0x123456789ABCDEF0, 2, 3

will compile to 24 bytes:

F0 DE BC 9A 78 56 34 12 02 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00

(for comparison, .byte 0x12, 2, 3 will compile to three bytes 12 02 03).

Where and when is .quad usually called in assembly?

Uhm.. it's assembler directive, used during compilation, it will just produce machine code. It can't be "called". You can call/execute the machine code defined by it, but that's very rare usage pattern, to produce instructions by defining them in numeric way as opcodes, if you have at hand assembler which can produce it from the mnemonics instead.

Also, why use .quad to generate anything?

If you want to set up 64b number 1000000000000 (1e12) in data segment, it is much more convenient to define it as .quad 1000000000000 than calculating the separate byte values and defining it as .byte 0, 16, 165, 212, 232, 0, 0, 0, in the .quad case the assembler will do the parsing and splitting into bytes for you.

.quad .L3 (from comment)

.L3 is label somewhere in the code, so it is some memory address, so it is some 64 bit number (for x86 64b target platforms with flat memory mapping). If you want to have that value somewhere in memory, then using .quad .L3 is simple way how to produce 8 bytes with that value (address of .L3 label).

The switch code does it use for indirect jump, selecting particular value in memory indexed by the switch value, and then jumping to the address stored in memory in the table. Something like jmp [table + index*8], when table+index*8 points at .L3 value, then the jmp will jump to .L3 address.

like image 76
Ped7g Avatar answered Dec 28 '22 09:12

Ped7g