blob: fcfb362b77e3ee07e04573306804d45722413afd [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
H. Peter Anvin77a036e2002-05-17 04:51:10 +000025; [18] Define a non-global rodata-section symbol
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000026
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000027 BITS 32
28 GLOBAL lrotate ; [1]
29 GLOBAL greet ; [1]
30 GLOBAL asmstr ; [2]
31 GLOBAL textptr ; [2]
32 GLOBAL selfptr ; [2]
33 GLOBAL integer ; [3]
34 EXTERN printf ; [10]
35 COMMON commvar 4 ; [7]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000037 SECTION .text
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000038
39; prototype: long lrotate(long x, int num);
40lrotate: ; [1]
41 push ebp
42 mov ebp,esp
43 mov eax,[ebp+8]
44 mov ecx,[ebp+12]
45.label rol eax,1 ; [4] [8]
46 loop .label ; [9] [12]
47 mov esp,ebp
48 pop ebp
49 ret
50
51; prototype: void greet(void);
52greet mov eax,[integer] ; [14]
53 inc eax
54 mov [localint],eax ; [14]
55 push dword [commvar]
56 mov eax,[localptr] ; [13]
57 push dword [eax]
58 push dword [integer] ; [1] [14]
59 push dword printfstr ; [13]
60 call printf ; [11]
61 add esp,16
62 ret
63
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000064 SECTION .data
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000065
66; a string
67asmstr db 'hello, world', 0 ; [2]
68
69; a string for Printf
70printfstr db "integer==%d, localint==%d, commvar=%d"
71 db 10, 0
72
73; some pointers
74localptr dd localint ; [5] [17]
75textptr dd greet ; [15]
76selfptr dd selfptr ; [16]
77
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078 SECTION .bss
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000079
80; an integer
81integer resd 1 ; [3]
82
83; a local integer
84localint resd 1 ; [6]
H. Peter Anvin77a036e2002-05-17 04:51:10 +000085
86 SECTION .rodata
87readonly dd readonly ; [18]