例子來自IBM開發者網站http://www.ibm.com/developerworks/cn/linux/l-gas-nasm.htmlhtml
程序功能:接受用戶名做爲輸入,並在終端返回一句問候語linux
1、nasm網站
myhello.asmcode
section .data prompt_str db 'Enter your name: ' ; $ is the location counter STR_SIZE equ $ - prompt_str greet_str db 'Hello ' GSTR_SIZE equ $ - greet_str section .bss ; Reserve 32 bytes of memory buff resb 32 ; A macro with two parameters ; Implements the write system call %macro write 2 mov eax, 4 mov ebx, 1 mov ecx, %1 mov edx, %2 int 80h %endmacro ; Implements the read system call %macro read 2 mov eax, 3 mov ebx, 0 mov ecx, %1 mov edx, %2 int 80h %endmacro section .text global _start _start: write prompt_str, STR_SIZE read buff, 32 ; Read returns the length in eax push eax ; Print the hello text write greet_str, GSTR_SIZE pop edx ; edx = length returned by read write buff, edx _exit: mov eax, 1 mov ebx, 0 int 80h
彙編、連接、運行htm
$ nasm -f elf myhello.asm ci
$ ld -m elf_i386 -o myhello myhello.o開發
$ ./myhello it
Enter Your Name: wxwio
Hello wxwasm
2、gas(as)
myhello.s
.section .data prompt_str: .ascii "Enter Your Name: " pstr_end: .set STR_SIZE, pstr_end - prompt_str greet_str: .ascii "Hello " gstr_end: .set GSTR_SIZE, gstr_end - greet_str .section .bss // Reserve 32 bytes of memory .lcomm buff, 32 // A macro with two parameters // implements the write system call .macro write str, str_size movl $4, %eax movl $1, %ebx movl \str, %ecx movl \str_size, %edx int $0x80 .endm // Implements the read system call .macro read buff, buff_size movl $3, %eax movl $0, %ebx movl \buff, %ecx movl \buff_size, %edx int $0x80 .endm .section .text .globl _start _start: write $prompt_str, $STR_SIZE read $buff, $32 // Read returns the length in eax pushl %eax // Print the hello text write $greet_str, $GSTR_SIZE popl %edx // edx = length returned by read write $buff, %edx _exit: movl $1, %eax movl $0, %ebx int $0x80
$ as --32 -o myhello.o myhello.s
$ ld -m elf_i386 -o myhello myhello.o
$ ./myhello
Enter Your Name: wxw
Hello wxw