开发者

Need to convert old 32-bit GAS code to a current GAS assembler (pushfl/popl)

I am in the process of trying to compile an old project on my modern machine. I know this old project used an old (2.x) version of GCC/GAS so I need to clean it up so that I can compile it with a current version.

The code in question:

__get_flags:
    pushfl
    popl %eax
    ret

gas complains that p开发者_如何学运维ushfl is not an instruction. From what I read this is an old deprecated GNU mnemonic, but there must be some sort of x86_64 equivalent, surely?

In addition to answering how to convert this code to x86_64 gas, it would be great if someone could explain where mnemonics like pushfl and pushfq and defined and documented. I am quite sure that are GAS creations...

Note: I want to compile this on gcc 4.6.1 and gas 2.21


The following:

static inline u16 __get_flags(void) {
    u16 flags;
    asm ("pushf\n\t"
         "pop %0" : : "+r"(flags) : "cc");
    return flags;
}

should actually be portable for both 64/32bit, x64/x86. But it only gets you the condition codes, not the high parts of the state register. If you require the entire contents of the flags register when running in 64bit (long) mode, use the PUSHFQ instruction name, with a u64 (or other 64bit quantity) operand.


I can't add a comment to the answer because I don't have 50 reputation. Anyway:

In the answer:

"pop %0" : : "+r"(flags) : "cc");

Should read:

"pop %0" : "=r"(flags) : : "cc");

Because the syntax is asm( code : outputs : inputs : clobbers ) and what you want is flags to be a register that's output from the asm block, not used as input. (I tested this and it worked for me, I also used 64-bit ints)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜