Inline assembly inside loops

c++, inline-assembly

Solution

I can't understand what's the problem but try to write code using clear asm code same as

asm{
   loop1:
     mov ax, this->var
     ...
     dec ax
     cmp ax, 0
     je exit
     jmp loop1
}

...

exit:

Also try to make "var" value as static may it help too.

Problem

I use inline assembly massively in a project where I need to call functions with an unknown number of arguments at compile time and while I manage myself to get it to work, sometimes, in linux (in windows I don't recall having that problem) strange things like this happen: If I have something like ``` for(int i = 1; i >= 0; i--) asm("push %0"::"m"(someArray[i])); ``` It works. If I have ``` for(int i = this->someVar; i >= 0; i--) asm("push %0"::"m"(someArray[i])); ``` and I guarantee with my life that someVar is holding the value 1 it throws segmentation fault. Also if I have ``` int x = 1; for(int i = x; i >= 0; i--) asm("push %0"::"m"(someArray[i])); ``` it works but ``` int x = this->someVar; for(int i = x; i >= 0; i--) asm("push %0"::"m"(someArray[i])); ``` does not. Also, and also strangely, I can say that while in some functions I don't have problems doing that in others I have, all in the same object. If someone can point me to some information that can clear up what's the problem there, I would appreciate. Beware that I really have to push the arguments in a for loop so avoiding it is not an option. I also tried using the inline assembly word "volatile" but nothing changed.

Original source