两个汇编文件的调用

1.asm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
section .bss
resb 2*32

section file1data ; 自定义数据段,未使用“传统”的.data

strHello db "Hello, World", 0Ah
STRLEN equ $-strHello

section file1text ; 自定义的代码段,未使用”传统“的.text
extern print ; 声明此函数在别的文件中
; 告诉编译器在编译文件时,找不到此符号也没关系,在链接时会找到
global _start ; 连接器把_start作为程序的入口

_start:
push STRLEN ; 传入参数,字符长度
push strHello ; 传入参数,待打印的字符串
call print ; 此函数定义在2.asm中

; 返回系统
mov ebx, 0; 返回值4
mov eax, 1; 系统调用号1:sys_exit
int 0x80 ; 系统调用

2.asm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
section .text
mov eax, 0x10
jmp $

section file2data ; 自定义数据段

file2var db 3

section file2text ; 自定义代码段

global print ; 导出print供其他模块使用

print:
mov edx, [esp+8] ; 字符串长度
mov ecx, [esp+4] ; 字符串

mov ebx, 1
mov eax, 4 ; sys_write
int 0x80 ; 系统调用
ret

编译、连接、执行:

1
2
3
4
5
nasm -f elf 1.asm -o 1.o
nasm -f elf 2.asm -o 2.o
ld 1.o 2.o -o 12 -m elf_i386
./12
Hello, World