Built-in assembly inside loops

I use the built-in assembly massively in a project where I need to call functions with an unknown number of arguments at compile time, and while I myself can make it work, sometimes on linux (in windows I don’t remember this problem) strange things like this:

If I have something like

for(int i = 1; i >= 0; i--)
   asm("push %0"::"m"(someArray[i]));

He 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 holds the value 1, it causes a segmentation error.

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]));

no.

Besides, and also strange, I can say that although in some functions I have no problems doing this in others that I have, everything is in the same object.

If someone can tell me some information that can clarify what the problem is, I would appreciate it.

, for, , .

"volatile", .

+3
5

, , , asm, ,

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

...

:

"var" , .

+4

. , i / , , for, push , , , .

(, register), , , C/++ . , , oivoodoo.

+4

:

i this, , , 386 esp -relative memory,

mov eax,[esp+8]

this eax. , push , ( ) .

, this->someVar , esp - , , .

- ebp, . ebp esp, .

: , - , , . , , oivoodoo.

+3

-, , , , , gcc linux , . , gcc (BP x86) , . - SP BP, . , alloca , , , .

, . , , asm , , , asm. , . , , , , . (BP).

, , , ( ).

+1

If you know the limit on the number of arguments, you can simply call it with a single function call with so many arguments, matching the actual arguments to the end.

Warning: x86_64 abi uses case for some parameters, which also violates this and your code.

0
source

Source: https://habr.com/ru/post/1728279/


All Articles