I'm trying to convert code from C++ to ASM based on AT&T code to convert:
void func() {
int num = 1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
/* code to swap */
std::cout <<"before: " << num;
num = num << 3;
std::cout << " after: " << num << std::endl;
/* end code to swap */
}
}
}
After the change the code looks like this:
void func() {
int num = 1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
std::cout <<"before: " << num;
asm (
"mov -4(%rbp), %eax \n"
"sall $3, %eax \n"
"mov %eax, -4(%rbp) \n"
);
std::cout << " after: " << num << std::endl;
}
}
}
The same example without the use of both loops works correctly, but when you add them, it does not work anymore. While 1 example gives correct results, the second code after changing the code prints only number 1, I assume that after adding 2 loops, the location of the variable changes from -4(%rbp) to another address.
My assumptions:
int num is in -4(%rbp)
int i is in -8(%rbp)
int j is in -12(%rbp)
Im using GCC Compiler with Dev C++