blob: c5a729f39047c3895e2c7b853bc2d297efc85f07 [file] [log] [blame]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001; test source file for assembling to ELF
2; build with:
3; nasm -f elf elftest.asm
4; gcc -o elftest elftest.c elftest.o
5; (assuming your gcc is ELF)
6
7; This file should test the following:
8; [1] Define and export a global text-section symbol
9; [2] Define and export a global data-section symbol
10; [3] Define and export a global BSS-section symbol
11; [4] Define a non-global text-section symbol
12; [5] Define a non-global data-section symbol
13; [6] Define a non-global BSS-section symbol
14; [7] Define a COMMON symbol
15; [8] Define a NASM local label
16; [9] Reference a NASM local label
17; [10] Import an external symbol
18; [11] Make a PC-relative call to an external symbol
19; [12] Reference a text-section symbol in the text section
20; [13] Reference a data-section symbol in the text section
21; [14] Reference a BSS-section symbol in the text section
22; [15] Reference a text-section symbol in the data section
23; [16] Reference a data-section symbol in the data section
24; [17] Reference a BSS-section symbol in the data section
25
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000026 BITS 32
27 GLOBAL lrotate ; [1]
28 GLOBAL greet ; [1]
29 GLOBAL asmstr ; [2]
30 GLOBAL textptr ; [2]
31 GLOBAL selfptr ; [2]
32 GLOBAL integer ; [3]
33 EXTERN printf ; [10]
34 COMMON commvar 4 ; [7]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000035
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 SECTION .text
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000037
38; prototype: long lrotate(long x, int num);
39lrotate: ; [1]
40 push ebp
41 mov ebp,esp
42 mov eax,[ebp+8]
43 mov ecx,[ebp+12]
44.label rol eax,1 ; [4] [8]
45 loop .label ; [9] [12]
46 mov esp,ebp
47 pop ebp
48 ret
49
50; prototype: void greet(void);
51greet mov eax,[integer] ; [14]
52 inc eax
53 mov [localint],eax ; [14]
54 push dword [commvar]
55 mov eax,[localptr] ; [13]
56 push dword [eax]
57 push dword [integer] ; [1] [14]
58 push dword printfstr ; [13]
59 call printf ; [11]
60 add esp,16
61 ret
62
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000063 SECTION .data
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
65; a string
66asmstr db 'hello, world', 0 ; [2]
67
68; a string for Printf
69printfstr db "integer==%d, localint==%d, commvar=%d"
70 db 10, 0
71
72; some pointers
73localptr dd localint ; [5] [17]
74textptr dd greet ; [15]
75selfptr dd selfptr ; [16]
76
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000077 SECTION .bss
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000078
79; an integer
80integer resd 1 ; [3]
81
82; a local integer
83localint resd 1 ; [6]