asm(".intel_syntax noprefix"); //Use of NASM
int main(void){
clear();
asm("mov rax, 0x1"); //I try to change the default value of Rax
// which is 0 on Qemu
long rax;
rax = readRax();
char * str;
str = itoa(rax);
print(str);
while(1){};
return 1;
}
Though you cannot rely on inline asm (especially for *ax, *cx, and *dx ["caller save" registers]), the only possible issue I see with the actual code is:
asm(".intel_syntax noprefix"); //Use of NASM
...
asm("mov rax, 0x1"); //I try to change the default value of Rax
...
asm("mov %%rax, %0"
:"=r"(rax)
);
...
Why are you mixing Intel and AT&T syntax? Does the asm routine in readRax know it is AT&T syntax?
Also use volatile prefix on inline asm to keep compiler from optimizing out the ASM. (it may think rax should be 0, so the second asm instruction may be changed to "rax = 0;" by the compiler)
You probably do read the value of rax. It's jus that the question doesn't make any sense because you can't know what the value in rax means at that point. The compiler is using the register to run your C code, so it has long been changed since you originally set it. You can only assume that the register stays the same within the same asm() block. You also need to specify each register that you change either in the output list or in the clobber list so that the compiler knows that you're going to overwrite its data.
My best bet would be that it's somewhere on the stack. The calling convention afaik is that ax is saved and restored by whatever code that is calling your function.
Edit: Oh I overlooked that your changing the value in the function itself with inline assembly. It seems C has it's own plans with that register. You might want to disassemble that code to see whats really happening. 'objdump -d file.o > file.list'
"Always code as if the guy who ends up maintaining it will be a violent psychopath who knows where you live." - John F. Woods
The problem is that I was compiling with gcc's -masm=intel option.
Furthermore it is effectively not a good idea to use a system register, except if you change it in the same asm block.
Here is a solution :