summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2002-04-30 20:51:32 +0000
committerH. Peter Anvin <hpa@zytor.com>2002-04-30 20:51:32 +0000
commitea6e34db64c7da7cb885197316c6b5e7d048bdb9 (patch)
tree78e728348f8fe09e394a51c3617e6261de0f4001 /test
downloadnasm-ea6e34db64c7da7cb885197316c6b5e7d048bdb9.tar.gz
NASM 0.91nasm-0.91
Diffstat (limited to 'test')
-rw-r--r--test/Makefile2
-rw-r--r--test/aouttest.asm83
-rw-r--r--test/aouttest.c35
-rw-r--r--test/bintest.asm56
-rw-r--r--test/cofftest.asm82
-rw-r--r--test/cofftest.c34
-rw-r--r--test/elftest.asm83
-rw-r--r--test/elftest.c35
-rw-r--r--test/inc1.asm4
-rw-r--r--test/inc2.asm8
-rw-r--r--test/inctest.asm15
-rw-r--r--test/objlink.c30
-rw-r--r--test/objtest.asm82
13 files changed, 549 insertions, 0 deletions
diff --git a/test/Makefile b/test/Makefile
new file mode 100644
index 00000000..5f0e5c6f
--- /dev/null
+++ b/test/Makefile
@@ -0,0 +1,2 @@
+clean:
+ rm -f *.o *.obj *.com bintest inctest
diff --git a/test/aouttest.asm b/test/aouttest.asm
new file mode 100644
index 00000000..c52f1120
--- /dev/null
+++ b/test/aouttest.asm
@@ -0,0 +1,83 @@
+; test source file for assembling to a.out
+; build with:
+; nasm -f aout aouttest.asm
+; gcc -o aouttest aouttest.c aouttest.o
+; (assuming your gcc is a.out)
+
+; This file should test the following:
+; [1] Define and export a global text-section symbol
+; [2] Define and export a global data-section symbol
+; [3] Define and export a global BSS-section symbol
+; [4] Define a non-global text-section symbol
+; [5] Define a non-global data-section symbol
+; [6] Define a non-global BSS-section symbol
+; [7] Define a COMMON symbol
+; [8] Define a NASM local label
+; [9] Reference a NASM local label
+; [10] Import an external symbol
+; [11] Make a PC-relative call to an external symbol
+; [12] Reference a text-section symbol in the text section
+; [13] Reference a data-section symbol in the text section
+; [14] Reference a BSS-section symbol in the text section
+; [15] Reference a text-section symbol in the data section
+; [16] Reference a data-section symbol in the data section
+; [17] Reference a BSS-section symbol in the data section
+
+[BITS 32]
+[GLOBAL _lrotate] ; [1]
+[GLOBAL _greet] ; [1]
+[GLOBAL _asmstr] ; [2]
+[GLOBAL _textptr] ; [2]
+[GLOBAL _selfptr] ; [2]
+[GLOBAL _integer] ; [3]
+[EXTERN _printf] ; [10]
+[COMMON _commvar 4] ; [7]
+
+[SECTION .text]
+
+; prototype: long lrotate(long x, int num);
+_lrotate: ; [1]
+ push ebp
+ mov ebp,esp
+ mov eax,[ebp+8]
+ mov ecx,[ebp+12]
+.label rol eax,1 ; [4] [8]
+ loop .label ; [9] [12]
+ mov esp,ebp
+ pop ebp
+ ret
+
+; prototype: void greet(void);
+_greet mov eax,[_integer] ; [14]
+ inc eax
+ mov [localint],eax ; [14]
+ push dword [_commvar]
+ mov eax,[localptr] ; [13]
+ push dword [eax] ;
+ push dword [_integer] ; [1] [14]
+ push dword _printfstr ; [13]
+ call _printf ; [11]
+ add esp,16
+ ret
+
+[SECTION .data]
+
+; a string
+_asmstr db 'hello, world', 0 ; [2]
+
+; a string for Printf
+_printfstr db "integer==%d, localint==%d, commvar=%d"
+ db 10, 0
+
+; some pointers
+localptr dd localint ; [5] [17]
+_textptr dd _greet ; [15]
+_selfptr dd _selfptr ; [16]
+
+[SECTION .bss]
+
+; an integer
+_integer resd 1 ; [3]
+
+; a local integer
+localint resd 1 ; [6]
diff --git a/test/aouttest.c b/test/aouttest.c
new file mode 100644
index 00000000..9a8eba30
--- /dev/null
+++ b/test/aouttest.c
@@ -0,0 +1,35 @@
+/*
+ * test source file for assembling to a.out
+ * build with:
+ * nasm -f aout aouttest.asm
+ * gcc -o aouttest aouttest.c aouttest.o
+ * (assuming your gcc is a.out)
+ */
+
+#include <stdio.h>
+
+extern int lrotate(long, int);
+extern void greet(void);
+extern char asmstr[];
+extern void *selfptr;
+extern void *textptr;
+extern int integer, commvar;
+
+int main(void) {
+
+ printf("Testing lrotate: should get 0x00400000, 0x00000001\n");
+ printf("lrotate(0x00040000, 4) = 0x%08lx\n", lrotate(0x40000,4));
+ printf("lrotate(0x00040000, 14) = 0x%08lx\n", lrotate(0x40000,14));
+
+ printf("This string should read `hello, world': `%s'\n", asmstr);
+
+ printf("The integers here should be 1234, 1235 and 4321:\n");
+ integer = 1234;
+ commvar = 4321;
+ greet();
+
+ printf("These pointers should be equal: %p and %p\n",
+ &greet, textptr);
+
+ printf("So should these: %p and %p\n", selfptr, &selfptr);
+}
diff --git a/test/bintest.asm b/test/bintest.asm
new file mode 100644
index 00000000..0a3c4ae9
--- /dev/null
+++ b/test/bintest.asm
@@ -0,0 +1,56 @@
+; test source file for assembling to binary files
+; build with:
+; nasm -f bin -o bintest.com bintest.asm
+
+; When run (as a DOS .COM file), this program should print
+; hello, world
+; on two successive lines, then exit cleanly.
+
+; This file should test the following:
+; [1] Define a text-section symbol
+; [2] Define a data-section symbol
+; [3] Define a BSS-section symbol
+; [4] Define a NASM local label
+; [5] Reference a NASM local label
+; [6] Reference a text-section symbol in the text section
+; [7] Reference a data-section symbol in the text section
+; [8] Reference a BSS-section symbol in the text section
+; [9] Reference a text-section symbol in the data section
+; [10] Reference a data-section symbol in the data section
+; [11] Reference a BSS-section symbol in the data section
+
+[BITS 16]
+[ORG 0x100]
+
+[SECTION .text]
+
+ jmp start ; [6]
+
+end mov ax,0x4c00 ; [1]
+ int 0x21
+
+start mov byte [bss_sym],',' ; [1] [8]
+ mov bx,[bssptr] ; [7]
+ mov al,[bx]
+ mov bx,[dataptr] ; [7]
+ mov [bx],al
+ mov cx,2
+.loop mov dx,datasym ; [1] [4] [7]
+ mov ah,9
+ push cx
+ int 0x21
+ pop cx
+ loop .loop ; [5] [6]
+ mov bx,[textptr] ; [7]
+ jmp bx
+
+[SECTION .data]
+
+datasym db 'hello world', 13, 10, '$' ; [2]
+bssptr dw bss_sym ; [2] [11]
+dataptr dw datasym+5 ; [2] [10]
+textptr dw end ; [2] [9]
+
+[SECTION .bss]
+
+bss_sym resb 1 ; [3]
diff --git a/test/cofftest.asm b/test/cofftest.asm
new file mode 100644
index 00000000..bb843a15
--- /dev/null
+++ b/test/cofftest.asm
@@ -0,0 +1,82 @@
+; test source file for assembling to COFF
+; build with (under DJGPP, for example):
+; nasm -f coff cofftest.asm
+; gcc -o cofftest cofftest.c cofftest.o
+
+; This file should test the following:
+; [1] Define and export a global text-section symbol
+; [2] Define and export a global data-section symbol
+; [3] Define and export a global BSS-section symbol
+; [4] Define a non-global text-section symbol
+; [5] Define a non-global data-section symbol
+; [6] Define a non-global BSS-section symbol
+; [7] Define a COMMON symbol
+; [8] Define a NASM local label
+; [9] Reference a NASM local label
+; [10] Import an external symbol
+; [11] Make a PC-relative call to an external symbol
+; [12] Reference a text-section symbol in the text section
+; [13] Reference a data-section symbol in the text section
+; [14] Reference a BSS-section symbol in the text section
+; [15] Reference a text-section symbol in the data section
+; [16] Reference a data-section symbol in the data section
+; [17] Reference a BSS-section symbol in the data section
+
+[BITS 32]
+[GLOBAL _lrotate] ; [1]
+[GLOBAL _greet] ; [1]
+[GLOBAL _asmstr] ; [2]
+[GLOBAL _textptr] ; [2]
+[GLOBAL _selfptr] ; [2]
+[GLOBAL _integer] ; [3]
+[EXTERN _printf] ; [10]
+[COMMON _commvar 4] ; [7]
+
+[SECTION .text]
+
+; prototype: long lrotate(long x, int num);
+_lrotate: ; [1]
+ push ebp
+ mov ebp,esp
+ mov eax,[ebp+8]
+ mov ecx,[ebp+12]
+.label rol eax,1 ; [4] [8]
+ loop .label ; [9] [12]
+ mov esp,ebp
+ pop ebp
+ ret
+
+; prototype: void greet(void);
+_greet mov eax,[_integer] ; [14]
+ inc eax
+ mov [localint],eax ; [14]
+ push dword [_commvar]
+ mov eax,[localptr] ; [13]
+ push dword [eax]
+ push dword [_integer] ; [1] [14]
+ push dword _printfstr ; [13]
+ call _printf ; [11]
+ add esp,16
+ ret
+
+[SECTION .data]
+
+; a string
+_asmstr db 'hello, world', 0 ; [2]
+
+; a string for Printf
+_printfstr db "integer==%d, localint==%d, commvar=%d"
+ db 10, 0
+
+; some pointers
+localptr dd localint ; [5] [17]
+_textptr dd _greet ; [15]
+_selfptr dd _selfptr ; [16]
+
+[SECTION .bss]
+
+; an integer
+_integer resd 1 ; [3]
+
+; a local integer
+localint resd 1 ; [6]
diff --git a/test/cofftest.c b/test/cofftest.c
new file mode 100644
index 00000000..4dec0df9
--- /dev/null
+++ b/test/cofftest.c
@@ -0,0 +1,34 @@
+/*
+ * test source file for assembling to COFF
+ * build with (under DJGPP, for example):
+ * nasm -f coff cofftest.asm
+ * gcc -o cofftest cofftest.c cofftest.o
+ */
+
+#include <stdio.h>
+
+extern int lrotate(long, int);
+extern void greet(void);
+extern char asmstr[];
+extern void *selfptr;
+extern void *textptr;
+extern int integer, commvar;
+
+int main(void) {
+
+ printf("Testing lrotate: should get 0x00400000, 0x00000001\n");
+ printf("lrotate(0x00040000, 4) = 0x%08lx\n", lrotate(0x40000,4));
+ printf("lrotate(0x00040000, 14) = 0x%08lx\n", lrotate(0x40000,14));
+
+ printf("This string should read `hello, world': `%s'\n", asmstr);
+
+ printf("The integers here should be 1234, 1235 and 4321:\n");
+ integer = 1234;
+ commvar = 4321;
+ greet();
+
+ printf("These pointers should be equal: %p and %p\n",
+ &greet, textptr);
+
+ printf("So should these: %p and %p\n", selfptr, &selfptr);
+}
diff --git a/test/elftest.asm b/test/elftest.asm
new file mode 100644
index 00000000..a6034a6f
--- /dev/null
+++ b/test/elftest.asm
@@ -0,0 +1,83 @@
+; test source file for assembling to ELF
+; build with:
+; nasm -f elf elftest.asm
+; gcc -o elftest elftest.c elftest.o
+; (assuming your gcc is ELF)
+
+; This file should test the following:
+; [1] Define and export a global text-section symbol
+; [2] Define and export a global data-section symbol
+; [3] Define and export a global BSS-section symbol
+; [4] Define a non-global text-section symbol
+; [5] Define a non-global data-section symbol
+; [6] Define a non-global BSS-section symbol
+; [7] Define a COMMON symbol
+; [8] Define a NASM local label
+; [9] Reference a NASM local label
+; [10] Import an external symbol
+; [11] Make a PC-relative call to an external symbol
+; [12] Reference a text-section symbol in the text section
+; [13] Reference a data-section symbol in the text section
+; [14] Reference a BSS-section symbol in the text section
+; [15] Reference a text-section symbol in the data section
+; [16] Reference a data-section symbol in the data section
+; [17] Reference a BSS-section symbol in the data section
+
+[BITS 32]
+[GLOBAL lrotate] ; [1]
+[GLOBAL greet] ; [1]
+[GLOBAL asmstr] ; [2]
+[GLOBAL textptr] ; [2]
+[GLOBAL selfptr] ; [2]
+[GLOBAL integer] ; [3]
+[EXTERN printf] ; [10]
+[COMMON commvar 4] ; [7]
+
+[SECTION .text]
+
+; prototype: long lrotate(long x, int num);
+lrotate: ; [1]
+ push ebp
+ mov ebp,esp
+ mov eax,[ebp+8]
+ mov ecx,[ebp+12]
+.label rol eax,1 ; [4] [8]
+ loop .label ; [9] [12]
+ mov esp,ebp
+ pop ebp
+ ret
+
+; prototype: void greet(void);
+greet mov eax,[integer] ; [14]
+ inc eax
+ mov [localint],eax ; [14]
+ push dword [commvar]
+ mov eax,[localptr] ; [13]
+ push dword [eax]
+ push dword [integer] ; [1] [14]
+ push dword printfstr ; [13]
+ call printf ; [11]
+ add esp,16
+ ret
+
+[SECTION .data]
+
+; a string
+asmstr db 'hello, world', 0 ; [2]
+
+; a string for Printf
+printfstr db "integer==%d, localint==%d, commvar=%d"
+ db 10, 0
+
+; some pointers
+localptr dd localint ; [5] [17]
+textptr dd greet ; [15]
+selfptr dd selfptr ; [16]
+
+[SECTION .bss]
+
+; an integer
+integer resd 1 ; [3]
+
+; a local integer
+localint resd 1 ; [6]
diff --git a/test/elftest.c b/test/elftest.c
new file mode 100644
index 00000000..1965fcf8
--- /dev/null
+++ b/test/elftest.c
@@ -0,0 +1,35 @@
+/*
+ * test source file for assembling to ELF
+ * build with:
+ * nasm -f elf elftest.asm
+ * gcc -o elftest elftest.c elftest.o
+ * (assuming your gcc is ELF)
+ */
+
+#include <stdio.h>
+
+extern int lrotate(long, int);
+extern void greet(void);
+extern char asmstr[];
+extern void *selfptr;
+extern void *textptr;
+extern int integer, commvar;
+
+int main(void) {
+
+ printf("Testing lrotate: should get 0x00400000, 0x00000001\n");
+ printf("lrotate(0x00040000, 4) = 0x%08lx\n", lrotate(0x40000,4));
+ printf("lrotate(0x00040000, 14) = 0x%08lx\n", lrotate(0x40000,14));
+
+ printf("This string should read `hello, world': `%s'\n", asmstr);
+
+ printf("The integers here should be 1234, 1235 and 4321:\n");
+ integer = 1234;
+ commvar = 4321;
+ greet();
+
+ printf("These pointers should be equal: %p and %p\n",
+ &greet, textptr);
+
+ printf("So should these: %p and %p\n", selfptr, &selfptr);
+}
diff --git a/test/inc1.asm b/test/inc1.asm
new file mode 100644
index 00000000..e9e5819b
--- /dev/null
+++ b/test/inc1.asm
@@ -0,0 +1,4 @@
+; This file is part of the include test.
+; See inctest.asm for build instructions.
+
+message: db 'hello, world',13,10,'$'
diff --git a/test/inc2.asm b/test/inc2.asm
new file mode 100644
index 00000000..c3ba2f75
--- /dev/null
+++ b/test/inc2.asm
@@ -0,0 +1,8 @@
+; This file is part of the include test.
+; See inctest.asm for build instructions.
+
+_main: mov dx,message
+ mov ah,9
+ int 21h
+ mov ax,4c00h
+ int 21h
diff --git a/test/inctest.asm b/test/inctest.asm
new file mode 100644
index 00000000..95ab40ff
--- /dev/null
+++ b/test/inctest.asm
@@ -0,0 +1,15 @@
+; This file, plus inc1.asm and inc2.asm, test NASM's file inclusion
+; mechanism.
+;
+; This produces a DOS .COM file: to assemble, use
+; nasm -f bin inctest.asm -o inctest.com
+; and when run, it should print `hello, world'.
+
+[BITS 16]
+[ORG 0x100]
+
+ jmp _main
+
+[INC inc1.asm]
+
+[INCLUDE inc2.asm]
diff --git a/test/objlink.c b/test/objlink.c
new file mode 100644
index 00000000..2f92f05e
--- /dev/null
+++ b/test/objlink.c
@@ -0,0 +1,30 @@
+/*
+ * test source file for assembling to Microsoft 16-bit .OBJ
+ * build with (16-bit Microsoft C):
+ * nasm -f obj objtest.asm
+ * cl /AL objtest.obj objlink.c
+ * other compilers should work too, provided they handle large
+ * model in the same way as MS C
+ */
+
+#include <stdio.h>
+
+char text[] = "hello, world\n";
+
+extern void function(char *);
+extern int bsssym, commvar;
+extern void *selfptr;
+extern void *selfptr2;
+
+int main(void) {
+ printf("these should be identical: %p, %p\n",
+ (long) selfptr, (long) &selfptr);
+ printf("these should be equivalent but different: %p, %p\n",
+ (long) selfptr2, (long) &selfptr2);
+ printf("you should see \"hello, world\" twice:\n");
+ bsssym = 0xF00D;
+ commvar = 0xD00F;
+ function(text);
+ printf("this should be 0xF00E: 0x%X\n", bsssym);
+ printf("this should be 0xD00E: 0x%X\n", commvar);
+}
diff --git a/test/objtest.asm b/test/objtest.asm
new file mode 100644
index 00000000..8530baee
--- /dev/null
+++ b/test/objtest.asm
@@ -0,0 +1,82 @@
+; test source file for assembling to Microsoft 16-bit .OBJ
+; build with (16-bit Microsoft C):
+; nasm -f obj objtest.asm
+; cl /AL objtest.obj objlink.c
+; other compilers should work too, provided they handle large
+; model in the same way as MS C
+
+; This file should test the following:
+; [1] Define and export a global symbol
+; [2] Define a non-global symbol
+; [3] Define a common symbol
+; [4] Define a NASM local label
+; [5] Reference a NASM local label
+; [6] Import an external symbol
+; [7] Make a PC-relative relocated reference
+; [8] Reference a symbol in the same section as itself
+; [9] Reference a symbol in a different segment from itself
+; [10] Define a segment group
+; [11] Take the offset of a symbol in a grouped segment w.r.t. its segment
+; [12] Reserve uninitialised data space in a segment
+; [13] Directly take the segment address of a segment
+; [14] Directly take the segment address of a group
+; [15] Use SEG on a non-external
+; [16] Use SEG on an external
+
+[bits 16]
+
+[global _bsssym] ; [1]
+[global _function] ; [1]
+[global _selfptr] ; [1]
+[global _selfptr2] ; [1]
+[common _commvar 2] ; [3]
+[extern _printf] ; [6]
+
+[group mygroup mybss mydata] ; [10]
+[group mygroup2 mycode mycode2] ; [10]
+
+[segment mycode private]
+
+_function push bp
+ mov bp,sp
+ push ds
+ mov ax,mygroup ; [14]
+ mov ds,ax
+ inc word [_bsssym] ; [9]
+ mov ax,seg _commvar
+ mov ds,ax
+ dec word [_commvar]
+ pop ds
+ mov ax,[bp+6]
+ mov dx,[bp+8]
+ push dx
+ push ax
+ push dx
+ push ax
+ call far [cs:.printf] ; [5] [8]
+ pop ax
+ pop ax
+ call trampoline ; [7]
+ pop ax
+ pop ax
+ mov sp,bp
+ pop bp
+ retf
+
+.printf dw _printf, seg _printf ; [2] [4] [16]
+
+[segment mycode2 private]
+
+trampoline: pop ax
+ push cs
+ push ax
+ jmp far _printf
+
+[segment mybss private]
+
+_bsssym resw 64 ; [12]
+
+[segment mydata private]
+
+_selfptr dw _selfptr, seg _selfptr ; [8] [15]
+_selfptr2 dw _selfptr2 wrt mydata, mydata ; [11] [13]