x86_64 asm

First Post:

Last Update:

Word Count:
653

Read Time:
3 min

NASM

Hello world

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
; This is a very simple program
; Hello World Program
; Complie: nasm -f elf p1.asm
; Link : ld -m elf_i386 p1.o -o p1

SECTION.data
msg db "Hello World", 0Ah, 00h ; Crete a string, the 0Ah is a '\n'

SECTION.text
global _start
_start:
mov edx, 13 ; the length of string
mov ecx,msg ; string address
mov ebx,1 ; write this to standard io output
mov eax,4 ; SYS_write
int 80h ; syscall
mov ebx,0 ; return value
mov eax,1 ; SYS_exit
int 80h ; syscall

自动计算字符串长度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
; This program also is Hello Wrold, but it is auto calc the length of string

.data
msg db 'Hello Wrold', 0ah, 00h ;

.text
global _start
_start:
mov ebx,msg ; move address of string in ebx register
mov eax,ebx ; get address
;A loop to get length of string
nextchar:
cmp byte[eax], 0
jz finished ; if eax equle to 0, then break
inc eax ; eax increse 1
jmp nextchar ; continue loop

finished:
sub eax, ebx ; eax = eax - ebx, get length
mov edx, eax ; length
mov ecx, msg ; address of string
mov ebx, 1 ; print to standard io output
mov eax, 4 ; SYS_write
int 80h ; syscall
mov ebx, 0 ; return value
mov eax, 1 ; sys_exit
int 80h ; syscall

手动计算字符串长度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
; This program also is Hello Wrold, but it is auto calc the length of string
; It will take strlen function to calc length of string

.data
msg db 'Hello Wrold', 0ah, 00h ;

.text
global _start
_start:
mov ebx,msg ; move address of string in ebx register
call strlen ;
mov edx, eax ; length
mov ecx, msg ; address of string
mov ebx, 1 ; print to standard io output
mov eax, 4 ; SYS_write
int 80h ; syscall
mov ebx, 0 ; return value
mov eax, 1 ; sys_exit
int 80h ; syscall

; calc length of string function
strlen:
mov eax, 0
;A loop to get length of string
nextchar:
cmp byte[ebx + eax], 0
jz finished ; if eax equle to 0, then break
inc eax ; eax increse 1
jmp nextchar ; continue loop
finished:
ret ; return

分离式编译

和C语言一样,汇编程序也是可以进行分离式编译的,你可以把自己的函数写到一个文件里,而另一的文件把它包含进去,然后就可以调用它。

头文件代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
;-------------------Hello World Program(include)--------------------
; 函数头文件
; by alongwy
;-------------------------------------------------------------------


;------------------------------------------
; int slen(String message)
; 计算字符串的长度
slen:
push ebx
mov ebx, eax

nextchar:
cmp byte [eax], 0
jz finished
inc eax
jmp nextchar

finished:
sub eax, ebx
pop ebx
ret

;------------------------------------------
; void sprint(String message)
; 打印字符串
sprint:
push edx
push ecx
push ebx
push eax
call slen ;调用slen计算字符串长度

mov edx, eax
pop eax

mov ecx, eax
mov ebx, 1
mov eax, 4
int 80h

pop ebx
pop ecx
pop edx
ret

;------------------------------------------
; void exit()
; 退出程序
quit:
mov ebx, 0
mov eax, 1
int 80h
ret

主文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
;-------------------Hello World Program(include)--------------------
; by alongwy
; 编译: nasm -f elf helloworld-inc.asm
; 链接: ld -m elf_i386 helloworld-inc.o -o helloworld-inc
; 64位系统需要 elf_i386 选项
; 运行: ./helloworld-inc
;-------------------------------------------------------------------

%include 'functions.asm' ; 包含头文件
SECTION .data ; 数据段
msg1 db 'Hello World!', 0Ah, 0h ; 创建字符串,0Ah是换行符
msg2 db 'Hello NASM!', 0Ah, 0h ; 后面添加了一个0

SECTION .text ; 代码段
global _start

_start:
mov eax, msg1 ; 存入mag1的地址
call sprint ; 调用sprint打印
mov eax, msg1
call sprint
call exit ; 退出
打赏点小钱
支付宝 | Alipay
微信 | WeChat