[SP-pm] Algumas duvidas sobre Perl

breno breno at rio.pm.org
Wed Sep 22 16:36:52 PDT 2010


2010/9/22 Suissa <jnascimento em gmail.com>:
> sei q em C eh facilimo utilizar codigos asm
>

Sim, mas ainda assim o código assembly é compilado, não interpretado.

Caso alguém tenha ficado curioso:

---------8<----------
#include <stdio.h>

int main (void) {
   int arg1=9, arg2=16, add, sub;

   __asm__ ( "addl %%ebx, %%eax;" : "=a" (add) : "a" (arg1) , "b" (arg2) );
   __asm__ ( "subl %%ebx, %%eax;" : "=a" (sub) : "a" (arg1) , "b" (arg2) );

   printf( "%d + %d = %d\n", arg1, arg2, add );
   printf( "%d - %d = %d\n", arg1, arg2, sub );

   return 0;
}
--------->8----------

Apenas a título de curiosidade, em Perl dá até pra usar diferentes
assemblers no mesmo código ;-)

---------8<----------
say "9 + 16 = " . add(9, 16);
say "9 - 16 = " . subtract(9, 16);

use Inline ASM => 'DATA',
           AS => 'as',
           PROTO => {add => 'int(int,int)'};

use Inline ASM => 'DATA',
           AS => 'nasm',
           ASFLAGS => '-f elf',
           PROTO => {subtract => 'int(int,int)'};

__END__
__ASM__

.text
.globl    add

add:      movl 4(%esp),%eax
          addl 8(%esp),%eax
          ret
__ASM__
          GLOBAL subtract
          SECTION .text

subtract: mov eax,[esp+4]
          sub eax,[esp+8]
          ret
--------->8----------


More information about the SaoPaulo-pm mailing list