How does the .quad directive work in assembly?
Asked Answered
A

1

8

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?

Aaron answered 24/3, 2017 at 17:12 Comment(13)
Any reference? The only one I could find is telling nothing about 2's complement.Cruise
docs.oracle.com/cd/E26502_01/html/E28388/eoiyg.html reference I usedAaron
It is just evaluating the expressions given (in compile time) and storing the result (in 2's complement format, which is really not that important) in that memory.Cruise
It's same as .byte, just the values produced are 64b, not 8b. (64b value on x86 is stored in little endian way, so 0x123456789ABCDEF0 is stored as bytes F0 DE BC 9A 78 56 34 12).Precursor
@EugeneSh. lets say for example, the expression is a whole line, is does it go through the line as normal?Aaron
Line? What is line?Cruise
@EugeneSh. sorry new to assembly the code would look like '.quad .L3'Aaron
And how would that be related to C? Don't spam tags!Claymore
@Olaf the way Im learning assembly is by translating from C, and the other way around. .quad .L3 comes from a segment of c code where there is a switch.Aaron
.L3 is a label, so it resolves to some address, which is a value that you can storeDerisive
Switch on listing files in compiler options, so you can see yourself how particular lines are translated into machine code, then .quad .L3 would be as 8 bytes, and if you would check the actual value, you would find it equal to the address of .L3 label (ie. value of .L3 symbol in symbol table). BTW, that's not PIC/PIE compatible technique of compiling switch, I thought most of the 64b targets on x86 are enforced to be PIE, apparently I was wrong. (you could have provided some minimal examples in the question, like that .L3 label, that would explain tags + what is the actual question)Precursor
@JackWest: That does not justify the C tag. Please read the FAQ and take the tour.Claymore
@JackWest there are lots of assemblers from different 'vendors' and you should consult the documentation for the exact assembler that you are using. It is also possible some assemblers support .quad and some don't. Even amongst the assemblers that support .quad, it might not universally mean the same thing. So which assembler are you using?Sailboat
P
22

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.

Precursor answered 24/3, 2017 at 18:22 Comment(1)
Related runnable example: github.com/cirosantilli/linux-kernel-module-cheat/blob/…Considerate

© 2022 - 2024 — McMap. All rights reserved.