// hello.asm
SECTION .data ; Section containing initialised data 定义段
HelloMsg:db "Hello World!",10 ;定义一个字符串
HelloLen:equ $-HelloMsg ;求其长度
SECTION .text ; Section containing code定义代码段
global _start ; Linker needs this tofind the entry point! 定义一个程序入口,类似c语言的main函数
_start: ;接下来就是具体函数内容了
mov eax,4 ; Specify sys_writecall
mov ebx,1 ; Specify FileDescriptor 1: Standard Output
mov ecx,HelloMsg ; Pass offset ofthe message
mov edx,HelloLen ; Pass the lengthof the message
int 80H ; Make kernelcall,软中断,陷入内核态
mov eax,1 ; Code for ExitSyscall
mov ebx,0 ; Return a code ofzero
int 80H ; Make kernelcall
再给个别人的教程吧
; Hello World Program - asmtutor.com
; Compile with: nasm -f elf helloworld.asm
; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 helloworld.o -o helloworld
; Run with: ./helloworld
SECTION .data
msg db 'Hello World!', 0Ah
SECTION .text
global _start
_start:
mov edx, 13 ; number of bytes to write - one for each letter plus 0Ah (line feed character)
mov ecx, msg ; move the memory address of our message string into ecx
mov ebx, 1 ; write to the STDOUT file
mov eax, 4 ; invoke SYS_WRITE (kernel opcode 4)
int 80h
mov ebx, 0 ; return 0 status on exit - 'No Errors'
mov eax, 1 ; invoke SYS_EXIT (kernel opcode 1)
int 80h
root@BP:~# nasm -g -f elf64 hello.asm
root@BP:~# ld -o hello hello.o
root@BP:~# ./hello
Hello World!
root@BP:~# gdb hello
GNU gdb (Debian 7.12-6) 7.12.0.20161007-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from hello...done.
(gdb) l
1 SECTION .data ; Section containing initialised data
2 HelloMsg:db "Hello World!",10
3 HelloLen:equ $-HelloMsg
4 ; Section containing uninitialized data
5 SECTION .text ; Section containing code
6 global _start ; Linker needs this tofind the entry point!
7 _start:
8 mov eax,4 ; Specify sys_writecall
9 mov ebx,1 ; Specify FileDescriptor 1: Standard Output
10 mov ecx,HelloMsg ; Pass offset ofthe message
(gdb)