summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2019-07-28 22:21:28 -0700
committerH. Peter Anvin <hpa@zytor.com>2019-07-28 22:21:28 -0700
commit1e821d2e0468ee100e4e3fb65b6464bd876e679b (patch)
tree9f4bc5deb0fc79ff1abe9e6b731f84ce43bc070b
parent42d675278612cd573565015c41914bd6b3362678 (diff)
downloadsamples-1e821d2e0468ee100e4e3fb65b6464bd876e679b.tar.gz
samples-1e821d2e0468ee100e4e3fb65b6464bd876e679b.tar.xz
samples-1e821d2e0468ee100e4e3fb65b6464bd876e679b.zip
macros.s: show how macros can be used to implement most things
Simple macros which can take care of most of the weirdness.
-rw-r--r--Makefile4
-rw-r--r--macros.s122
2 files changed, 124 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index d9188bc..05b03a2 100644
--- a/Makefile
+++ b/Makefile
@@ -12,8 +12,8 @@ LDFLAGS = -m elf_i386 -q -z max-page-size=16 --hash-style=gnu
OBJCOPY = $(CROSS)objcopy
-OBJS = testme.o psp.o
-ALL = testme.exe.elf testme.com.elf testme.com # testme.exe
+OBJS = macros.o psp.o
+ALL = macros.exe.elf macros.com.elf macros.com # testme.exe
# Don't erase intermediate files
.SECONDARY:
diff --git a/macros.s b/macros.s
new file mode 100644
index 0000000..869701d
--- /dev/null
+++ b/macros.s
@@ -0,0 +1,122 @@
+ .code16
+
+.macro s.group group, sections:vararg
+ .pushsection "!\group", "a"
+ .balign 16
+ .irp sec,\sections
+ ".L.SEG.\sec" :
+ .endr
+ .popsection
+.endm
+
+.macro s.section name:req, subname, opts:vararg
+ .ifndef ".L.SEG.\name"
+ .section "\name\()!", "a"
+ .balign 16
+ ".L.SEG.\name" :
+ .endif
+ .L.SEG = ".L.SEG.\name"
+ .section "\name\()$\subname", \opts
+.endm
+
+.macro s.def syms:vararg
+ .irp sym,\syms
+ \sym :
+ .set "\sym\()!", .L.SEG
+ .endr
+.endm
+
+.macro s.globl syms:vararg
+ .irp sym,\syms
+ .globl \sym, "\sym\()!"
+ .endr
+.endm
+
+ s.group DGROUP, _RODATA, _DATA, _BSS
+
+ s.section testme_TEXT,,"ax"
+
+ s.globl _farfunc
+s.def _farfunc /* Far function */
+ mov %cs,%ax
+ lret
+
+ s.section _TEXT,,"ax"
+
+ s.globl ___start
+s.def ___start
+ mov $0,%ax
+ .reloc .-2, R_386_SEG16, .L.SEG
+ mov %ax,%ds
+
+ /* gas doesn't handle lcall with symbols */
+ .byte 0x9a
+ .word _farfunc
+ .reloc .-2, R_386_SUB16, "_farfunc!"
+ .word 0
+ .reloc .-2, R_386_SEG16, "_farfunc!"
+
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+
+ /*
+ * Return the value of _foo in ax, and its linear address in edx
+ */
+ s.globl _get_foo
+s.def _get_foo
+ mov _foo,%ax /* foo is in our own data segment */
+ .reloc .-2, R_386_SUB16, .L.SEG
+ mov $_foo,%edx
+ ret
+
+ /*
+ * Return a pointer to the command line (from the PSP) in
+ * es:bx
+ */
+ s.globl _get_cmdline
+s.def _get_cmdline
+ mov $0,%ax
+ .reloc .-2, R_386_SEG16, "__psp!"
+ mov %ax,%es
+ mov $__psp + 127,%dx
+ .reloc .-2, R_386_SUB16, "__psp!"
+ ret
+
+ s.section _DATA,,"aw"
+ .balign 2
+
+s.def _other
+ .word 0x5678
+
+ /* Subsections */
+ s.section _DATA,.misc1,"aw"
+s.def _foo
+ .word 0x1234
+
+ s.section _DATA,.misc2,"aw"
+s.def _foo2
+ .word 0x4321
+
+ s.section _BSS,,"aw",@nobits
+ s.globl _nasa
+s.def _nasa
+ .space 512