#include <stdio.h>
int main(int argc, char **argv)
{
// Criando variáveis para interagir com assembly:
int foo, bar, var;
// O código abaixo mudará o valor de foo usando assembly.
// NOTA: A sintaxe assembly depende do compilador.
// No caso do GCC, usa-se o estilo AT&T por padrão,
// mas é possível usar o estilo Intel também.
// Exemplo 1:
// Em C, seria:
// foo = 1;
// bar = 2;
// var = 3;
asm volatile ("movl $1, %0;" // código assembly
"movl $2, %1;"
"movl $3, %2;"
: "=g" (foo), "=g" (bar), "=g" (var) // variáveis de saída
);
printf("EXEMPLO 1:\nfoo = %d\nbar = %d\nvar = %d\n\n", foo
, bar
, var
);
// Exemplo 2:
// Em C, seria:
// bar = foo * 2;
asm volatile ("movl $2, %%eax;" // eax = 2
"imul %%ebx, %%eax;" // eax * ebx
"movl %%eax, %0;" // faz bar igual ao resultado.
: "=r" (bar) // variáveis de saída
: "b" (foo) // variáveis de entrada (ebx = foo)
);
printf("EXEMPLO 2:\nbar = %d \n\n", bar
);
// Exemplo 3:
// Em C, seria:
// var = bar;
asm volatile ("movl %0, %%eax;"
"movl %%eax, %1;"
: "=r" (var)
: "b" (bar)
: "%eax"
);
printf("EXEMPLO 3:\nvar = %d \n\n", var
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbihpbnQgYXJnYywgY2hhciAqKmFyZ3YpCnsKICAgIC8vIENyaWFuZG8gdmFyacOhdmVpcyBwYXJhIGludGVyYWdpciBjb20gYXNzZW1ibHk6CiAgICBpbnQgZm9vLCBiYXIsIHZhcjsKICAgIAogICAgLy8gTyBjw7NkaWdvIGFiYWl4byBtdWRhcsOhIG8gdmFsb3IgZGUgZm9vIHVzYW5kbyBhc3NlbWJseS4KICAgIC8vIE5PVEE6IEEgc2ludGF4ZSBhc3NlbWJseSBkZXBlbmRlIGRvIGNvbXBpbGFkb3IuCiAgICAvLyAgICAgICBObyBjYXNvIGRvIEdDQywgdXNhLXNlIG8gZXN0aWxvIEFUJlQgcG9yIHBhZHLDo28sCiAgICAvLyAgICAgICBtYXMgw6kgcG9zc8OtdmVsIHVzYXIgbyBlc3RpbG8gSW50ZWwgdGFtYsOpbS4KICAgIAogICAgLy8gRXhlbXBsbyAxOgogICAgLy8gICAgRW0gQywgc2VyaWE6CiAgICAvLyAgICAgICAgZm9vID0gMTsKICAgIC8vICAgICAgICBiYXIgPSAyOwogICAgLy8gICAgICAgIHZhciA9IDM7CiAgICBhc20gdm9sYXRpbGUgKCJtb3ZsICQxLCAlMDsiICAvLyBjw7NkaWdvIGFzc2VtYmx5CiAgICAgICAgICAgICAgICAgICJtb3ZsICQyLCAlMTsiCiAgICAgICAgICAgICAgICAgICJtb3ZsICQzLCAlMjsiCiAgICAgICAgICAgICAgICAgIDogIj1nIiAoZm9vKSwgIj1nIiAoYmFyKSwgIj1nIiAodmFyKSAvLyB2YXJpw6F2ZWlzIGRlIHNhw61kYQogICAgICAgICAgICAgICAgICApOwogICAgICAgICAgICAgICAgICAKICAgIHByaW50ZigiRVhFTVBMTyAxOlxuZm9vID0gJWRcbmJhciA9ICVkXG52YXIgPSAlZFxuXG4iLCBmb28sIGJhciwgdmFyKTsKICAgICAgICAgICAgICAgICAgCiAgICAvLyBFeGVtcGxvIDI6CiAgICAvLyAgICBFbSBDLCBzZXJpYToKICAgIC8vICAgICAgICBiYXIgPSBmb28gKiAyOwogICAgYXNtIHZvbGF0aWxlICgibW92bCAkMiwgJSVlYXg7IiAgICAgIC8vIGVheCA9IDIKICAgICAgICAgICAgICAgICAgImltdWwgJSVlYngsICUlZWF4OyIgICAvLyBlYXggKiBlYngKICAgICAgICAgICAgICAgICAgIm1vdmwgJSVlYXgsICUwOyIgICAgICAvLyBmYXogYmFyIGlndWFsIGFvIHJlc3VsdGFkby4KICAgICAgICAgICAgICAgICAgOiAiPXIiIChiYXIpICAgIC8vIHZhcmnDoXZlaXMgZGUgc2HDrWRhCiAgICAgICAgICAgICAgICAgIDogImIiIChmb28pICAgICAvLyB2YXJpw6F2ZWlzIGRlIGVudHJhZGEgKGVieCA9IGZvbykKICAgICAgICAgICAgICAgICAgKTsgCiAgICAgICAgICAgICAgICAgICAgICAKICAgIHByaW50ZigiRVhFTVBMTyAyOlxuYmFyID0gJWQgXG5cbiIsIGJhcik7CiAgICAKICAgIC8vIEV4ZW1wbG8gMzoKICAgIC8vICAgIEVtIEMsIHNlcmlhOgogICAgLy8gICAgICAgIHZhciA9IGJhcjsKICAgIGFzbSB2b2xhdGlsZSAoIm1vdmwgJTAsICUlZWF4OyIKICAgICAgICAgICAgICAgICAgIm1vdmwgJSVlYXgsICUxOyIKICAgICAgICAgICAgICAgICAgOiAiPXIiICh2YXIpCiAgICAgICAgICAgICAgICAgIDogImIiIChiYXIpCiAgICAgICAgICAgICAgICAgIDogIiVlYXgiCiAgICAgICAgICAgICAgICAgICk7IAoKICAgIHByaW50ZigiRVhFTVBMTyAzOlxudmFyID0gJWQgXG5cbiIsIHZhcik7CgogICAgcmV0dXJuIDA7Cn0K