Here is some code that I tried:
#include <iostream>
using namespace std;
int a;
int main(){
asm("\nmov dword [rsp],70\n");
cout << a;}
In the code above, I try to write to my integer (a) by writing to the location pointed to by esp
(the stack pointer). This should work since that is where the variable is actually located. But when I try to compile the program using the command
g++ prog.cpp -masm=intel
I get the following error:
prog.cpp: Assembler messages:
prog.cpp:6: Error: ambiguous operand size for `mov'
I have also tried the following code:
#include <iostream>
using namespace std;
int a=5;
int main(){
asm(
"\npop rax\n"
"inc rax\n"
"push rax\n"
);
cout << a;}
The main difference between this program and the previous one is that instead of using esp
to access the stack, I used push
and pop
. This code compiles. But when I run it, it just returns 5 – the same number we started with!
Can somebody please explain to me what I'm doing wrong in the programs above?