Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

gcc - Retrieving x64 register values with inline asm

I was wondering if there was any way that would allow me to specify anything other than eax, ebx, ecx and edx as output operands.

Lets say I want to put the content of r8 in a variable, Is it possible to write something like this :

  __asm__ __volatile__ (""  
                        :"=r8"(my_var) 
                        : /* no input */                             
                        );            
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It should be possible, based on the answer here: https://stackoverflow.com/a/43197401/3569229

#include <stdint.h>

uint64_t getsp( void )
{
    uint64_t sp;
    asm( "mov %%r8, %0" : "=rm" ( sp ));
    return sp;
}

You can find a list of register names here: https://www3.nd.edu/~dthain/courses/cse40243/fall2015/intel-intro.html

So your code above would be changed to:

__asm__ __volatile__ ("mov %%r8, %0"  
                        :"=rm"(my_var) 
                        : /* no input */                             
                        );            

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...