sexta-feira, 11 de março de 2011

Hello World em Assembly

Aqui vai uma dica para quem está começando a se interessar por programação em assembly para Linux, em especial assembly para processadores x86.

Salve o código abaixo como hello.s

.section .data
output:
        .ascii "Hello World\n"
.section .text
.globl _start
_start:
        movl $4, %eax         # USAR SYSCALL 4 (SYS_WRITE)
        movl $1, %ebx         # IMPRIMIR EM STDOUT (FD 1)
        movl $output, %ecx    # PONTEIRO DO TEXTO
        movl $12, %edx        # COMPRIMENTO DO TEXTO
        int  $0x80            # CHAMA SYSCALL DO LINUX

        movl $1, %eax         # USAR SYSCALL 1 (SYS_EXIT)
        movl $0, %ebx         # SAIR COM ERROR CODE = 0
        int  $0x80            # CHAMA SYSCALL DO LINUX


Compilar:
as -gstabs -o hello.o hello.s

Linkar:
ld -o hello hello.o