aboutsummaryrefslogtreecommitdiff
path: root/sts/ia32
diff options
context:
space:
mode:
Diffstat (limited to 'sts/ia32')
-rw-r--r--sts/ia32/bitwise.sts27
-rw-r--r--sts/ia32/branch.sts11
-rw-r--r--sts/ia32/const.sts3
-rw-r--r--sts/ia32/core.sts17
-rw-r--r--sts/ia32/elf/begin.sts129
-rw-r--r--sts/ia32/elf/end.sts21
-rw-r--r--sts/ia32/elf/syscall.sts41
-rw-r--r--sts/ia32/float/control.sts19
-rw-r--r--sts/ia32/float/float.sts3
-rw-r--r--sts/ia32/float/math.sts32
-rw-r--r--sts/ia32/float/stack.sts25
-rw-r--r--sts/ia32/ia32.sts8
-rw-r--r--sts/ia32/macro.sts86
-rw-r--r--sts/ia32/math.sts68
-rw-r--r--sts/ia32/memory.sts10
-rw-r--r--sts/ia32/stack.sts41
16 files changed, 541 insertions, 0 deletions
diff --git a/sts/ia32/bitwise.sts b/sts/ia32/bitwise.sts
new file mode 100644
index 0000000..205e618
--- /dev/null
+++ b/sts/ia32/bitwise.sts
@@ -0,0 +1,27 @@
+defasm xor
+ # 5b ?' ; pop ebx
+ # 58 ?' ; pop eax
+ # 31 ?' # d8 ?' ; xor eax, ebx
+ # 50 ?' ; push eax
+next
+
+defasm and
+ # 5b ?' ; pop ebx
+ # 58 ?' ; pop eax
+ # 21 ?' # d8 ?' ; and eax, ebx
+ # 50 ?' ; push eax
+next
+
+defasm shr
+ # 59 ?' ; pop ecx
+ # 58 ?' ; pop eax
+ # d3 ?' # e8 ?' ; shr eax, cl
+ # 50 ?' ; push eax
+next
+
+defasm shl
+ # 59 ?' ; pop ecx
+ # 58 ?' ; pop eax
+ # d3 ?' # e0 ?' ; shl eax, cl
+ # 50 ?' ; push eax
+next
diff --git a/sts/ia32/branch.sts b/sts/ia32/branch.sts
new file mode 100644
index 0000000..35177f6
--- /dev/null
+++ b/sts/ia32/branch.sts
@@ -0,0 +1,11 @@
+defasm branch
+ # 8b ?' # 36 ?' ; mov esi, [esi]
+next
+
+defasm 0branch
+ # 58 ?' ; pop eax
+ # 83 ?' # c6 ?' # 4 ?' ; add esi, 4
+ # 83 ?' # f8 ?' # 0 ?' ; cmp eax, 0
+ # 75 ?' # 3 ?' ; jne +3
+ # 8b ?' # 76 ?' # fc ?' ; mov esi, [esi - 4]
+next
diff --git a/sts/ia32/const.sts b/sts/ia32/const.sts
new file mode 100644
index 0000000..31f8041
--- /dev/null
+++ b/sts/ia32/const.sts
@@ -0,0 +1,3 @@
+defconst word_size 4
+defconst word_max ffffffff
+defconst word_endianness 0
diff --git a/sts/ia32/core.sts b/sts/ia32/core.sts
new file mode 100644
index 0000000..4a5e692
--- /dev/null
+++ b/sts/ia32/core.sts
@@ -0,0 +1,17 @@
+defasm docol
+ # 83 ?' # ed ?' # 4 ?' ; sub ebp, 4
+ # 89 ?' # 75 ?' # 0 ?' ; mov [ebp], esi
+ # 83 ?' # c0 ?' # 4 ?' ; add eax, 4
+ # 89 ?' # c6 ?' ; mov esi, eax
+ # ad ?' # ff ?' # 20 ?' ; go next
+next
+
+defasm exit
+ # 8b ?' # 75 ?' # 0 ?' ; mov esi, [ebp]
+ # 83 ?' # c5 ?' # 4 ?' ; add ebp, 4
+next
+
+defasm lit
+ # ad ?' ; lodsd
+ # 50 ?' ; push eax
+next
diff --git a/sts/ia32/elf/begin.sts b/sts/ia32/elf/begin.sts
new file mode 100644
index 0000000..16d3355
--- /dev/null
+++ b/sts/ia32/elf/begin.sts
@@ -0,0 +1,129 @@
+?create ?? _code_segment
+_code_segment ?choose
+
+# 08048000 ?org
+# 4 ?size
+# 0 ?endianness
+
+?create ?? _data_segment
+_data_segment ?choose
+
+# 09048000 ?org
+# 4 ?size
+# 0 ?endianness
+
+_code_segment ?choose
+
+; ELF header
+; EI_MAG - ELF magic number
+# 7f ?' # 45 ?' # 4c ?' # 46 ?'
+; EI_CLASS - 32-bit format
+# 1 ?'
+; EI_DATA - little-endiann
+# 1 ?'
+; EI_VERSION - current ELF header version
+# 1 ?'
+; EI_OSABI - Linux ABI
+# 3 ?'
+; EI_ABIVERSION - none
+# 0 ?'
+; EI_PAD - padding
+# 7 ?res
+; Executable file type
+# 2 ?2.
+; IA-32 architecture
+# 3 ?2.
+; ELF version
+# 1 ?4.
+; Entry point
+?$ ?? _elf_entry
+# 0 ?4.
+; Program headers offset
+# 34 ?4.
+; Section headers offset
+# 0 ?4.
+; Extra flags
+# 0 ?4.
+; ELF header size
+# 34 ?2.
+; Program header size
+# 20 ?2.
+; Number of program headers
+# 2 ?2.
+; Section header size
+# 28 ?2.
+; Number of section headers
+# 0 ?2.
+; Section name section index
+# 0 ?2.
+
+; ELF CODE PROGRAM HEADER
+; Segment type
+# 1 ?4.
+; Segment offset
+# 0 ?4.
+; Segment virtual address
+?$$ ?4.
+; Segment physical address
+?$$ ?4.
+; Segment file size
+?$ ?? _code_segment_file_size
+# 0 ?4.
+; Segment memory size
+?$ ?? _code_segment_memory_size
+# 0 ?4.
+; Segment flags
+# 5 ?4.
+; Segment alignment
+# 1000 ?4.
+
+; ELF DATA PROGRAM HEADER
+; Segment type
+# 1 ?4.
+; Segment offset
+?$ ?? _data_segment_offset
+# 0 ?4.
+; Segment virtual address
+_data_segment ?choose
+?$$
+_code_segment ?choose
+?4.
+; Segment physical address
+_data_segment ?choose
+?$$
+_code_segment ?choose
+?4.
+; Segment file size
+?$ ?? _data_segment_file_size
+# 0 ?4.
+; Segment memory size
+?$ ?? _data_segment_memory_size
+# 0 ?4.
+; Segment flags
+# 6 ?4.
+; Segment alignment
+# 1000 ?4.
+
+; Return stack
+_data_segment ?choose
+1000 ?res
+?$ ?dup ?? _return_stack
+_code_segment ?choose
+
+; PROGRAM ENTRY CODE
+?$ _elf_entry ?!
+; mov ebp, _return_stack
+# bd ?'
+_return_stack ?.
+; mov esi, _threaded_code_entry
+# be ?'
+?$ ?? _threaded_code_entry
+# 0 ?.
+; lodsd
+# ad ?'
+; jmp dword [eax]
+# ff ?' # 20 ?'
+
+module sts/ia32/ia32.sts
+module sts/ia32/elf/syscall.sts
+module sts/common/common.sts
diff --git a/sts/ia32/elf/end.sts b/sts/ia32/elf/end.sts
new file mode 100644
index 0000000..7c80eec
--- /dev/null
+++ b/sts/ia32/elf/end.sts
@@ -0,0 +1,21 @@
+; Code segment alignment
+# 1000 ?$ # 1000 ?mod ?- ?dup ?res
+
+; Code segment ELF sizes
+?$ ?$$ ?- _code_segment_file_size ?!
+?$ ?$$ ?- _code_segment_memory_size ?!
+
+; Data segment ELF offset
+?$ ?$$ ?-
+_code_segment ?choose
+_data_segment_offset ?!
+
+; Data segment ELF sizes
+_data_segment ?choose
+?$ ?$$ ?-
+_code_segment ?choose
+_data_segment_file_size ?!
+_data_segment ?choose
+?$ ?$$ ?-
+_code_segment ?choose
+_data_segment_memory_size ?!
diff --git a/sts/ia32/elf/syscall.sts b/sts/ia32/elf/syscall.sts
new file mode 100644
index 0000000..3acfef2
--- /dev/null
+++ b/sts/ia32/elf/syscall.sts
@@ -0,0 +1,41 @@
+defasm sys_exit
+ # b8 ?' # 1 ?. ; mov eax, 1
+ # 31 ?' # db ?' ; xor ebx, ebx
+ # cd ?' # 80 ?' ; int 0x80
+; no need in next since this is the last instruction
+
+_data_segment ?choose
+ ?$ ?? _iobuf # 0 ?.
+_code_segment ?choose
+
+defasm sys_read
+ # b8 ?' # 3 ?. ; mov eax, 3
+ # 31 ?' # db ?' ; xor ebx, ebx
+ # b9 ?' _iobuf ?. ; mov ecx, _iobuf
+ # ba ?' # 1 ?. ; mov edx, 1
+ # cd ?' # 80 ?' ; int 0x80
+ # 31 ?' # db ?' ; xor ebx, ebx
+ # 8a ?' # 1d ?' _iobuf ?. ; mov bl, [_iobuf]
+ # 53 ?' ; push ebx
+ # 50 ?' ; push eax
+next
+
+defasm sys_write
+ # 58 ?' ; pop eax
+ # a2 ?' _iobuf ?. ; mov [_iobuf], al
+ # b8 ?' # 4 ?. ; mov eax, 4
+ # bb ?' # 1 ?. ; mov ebx, 1
+ # b9 ?' _iobuf ?. ; mov ecx, _iobuf
+ # ba ?' # 1 ?. ; mov edx, 1
+ # cd ?' # 80 ?' ; int 0x80
+next
+
+defasm sys_write_err
+ # 58 ?' ; pop eax
+ # a2 ?' _iobuf ?. ; mov [_iobuf], al
+ # b8 ?' # 4 ?. ; mov eax, 4
+ # bb ?' # 2 ?. ; mov ebx, 2
+ # b9 ?' _iobuf ?. ; mov ecx, _iobuf
+ # ba ?' # 1 ?. ; mov edx, 1
+ # cd ?' # 80 ?' ; int 0x80
+next
diff --git a/sts/ia32/float/control.sts b/sts/ia32/float/control.sts
new file mode 100644
index 0000000..3863f5f
--- /dev/null
+++ b/sts/ia32/float/control.sts
@@ -0,0 +1,19 @@
+defasm float_init
+ # 9b ?' ; wait
+ # db ?' # e3 ?' ; fninit
+next
+
+defasm float_cload
+ # d9 ?' # 2c ?' # 24 ?' ; fldcw [esp]
+ # 58 ?' ; pop eax
+next
+
+defasm float_sstore
+ # 9b ?' # df ?' # e0 ?' ; fstsw ax
+ # 50 ?' ; push eax
+next
+
+defasm float_cstore
+ # 50 ?' ; push eax
+ # d9 ?' # 3c ?' # 24 ?' ; fstcw [esp]
+next
diff --git a/sts/ia32/float/float.sts b/sts/ia32/float/float.sts
new file mode 100644
index 0000000..acaf3a0
--- /dev/null
+++ b/sts/ia32/float/float.sts
@@ -0,0 +1,3 @@
+module sts/ia32/float/control.sts
+module sts/ia32/float/stack.sts
+module sts/ia32/float/math.sts
diff --git a/sts/ia32/float/math.sts b/sts/ia32/float/math.sts
new file mode 100644
index 0000000..d02b491
--- /dev/null
+++ b/sts/ia32/float/math.sts
@@ -0,0 +1,32 @@
+defasm float_add
+ # de ?' # c1 ?' ; faddp st1, st0
+next
+
+defasm float_sub
+ # de ?' # e9 ?' ; fsubp st1, st0
+next
+
+defasm float_mul
+ # de ?' # c9 ?' ; fmullp st1, st0
+next
+
+defasm float_div
+ # de ?' # f9 ?' ; fdivp st1, st0
+next
+
+defasm float_sqrt
+ # d9 ?' # fa ?' ; fsqrt
+next
+
+defasm float_more
+ # de ?' # d9 ?' ; fcompp
+ # 9b ?' # df ?' # e0 ?' ; fstsw ax
+ # 25 ?' # 100 ?. ; and eax, 0x100
+ # c1 ?' # e8 ?' # 8 ?' ; shr eax, 8
+ # 50 ?' ; push eax
+next
+
+defword float_less
+ float_more
+ 0 =
+exit
diff --git a/sts/ia32/float/stack.sts b/sts/ia32/float/stack.sts
new file mode 100644
index 0000000..8c16fef
--- /dev/null
+++ b/sts/ia32/float/stack.sts
@@ -0,0 +1,25 @@
+defasm float_fload
+ # d9 ?' # 04 ?' # 24 ?' ; fld dword [esp]
+ # 58 ?' ; pop eax
+next
+
+defasm float_iload
+ # db ?' # 04 ?' # 24 ?' ; fild dword [esp]
+ # 58 ?' ; pop eax
+next
+
+defasm float_dload
+ # df ?' # 2c ?' # 24 ?' ; fild qword [esp]
+ # 58 ?' ; pop eax
+ # 58 ?' ; pop eax
+next
+
+defasm float_fstore
+ # 50 ?' ; push eax
+ # d9 ?' # 1c ?' # 24 ?' ; fstp dword [esp]
+next
+
+defasm float_istore
+ # 50 ?' ; push eax
+ # db ?' # 1c ?' # 24 ?' ; fistp dword [esp]
+next
diff --git a/sts/ia32/ia32.sts b/sts/ia32/ia32.sts
new file mode 100644
index 0000000..dc3d06f
--- /dev/null
+++ b/sts/ia32/ia32.sts
@@ -0,0 +1,8 @@
+module sts/ia32/macro.sts
+module sts/ia32/const.sts
+module sts/ia32/core.sts
+module sts/ia32/stack.sts
+module sts/ia32/branch.sts
+module sts/ia32/memory.sts
+module sts/ia32/math.sts
+module sts/ia32/bitwise.sts
diff --git a/sts/ia32/macro.sts b/sts/ia32/macro.sts
new file mode 100644
index 0000000..fa9510b
--- /dev/null
+++ b/sts/ia32/macro.sts
@@ -0,0 +1,86 @@
+[ next 0
+ # ad ?' # ff ?' # 20 ?'
+]
+
+[ defasm 1
+ [ %1% 0
+ _%0% ?.
+ \]
+ ?$ ?? _%1%
+ ?$ # 4 ?+ ?.
+]
+
+[ defword 1
+ [ %1% 0
+ _%0% ?.
+ \]
+ ?$ ?? _%1%
+ _docol # 4 ?+ ?.
+]
+
+[ defconst 2
+ defasm %1%
+ # 68 ?' # %2% ?. ; push %2%
+ next
+]
+
+[ defarr 2
+ _data_segment ?choose
+ ?$ ?? _%1%_arr
+ # %2% # 4 ?* ?res
+ _code_segment ?choose
+ defasm %1%
+ # 68 ?' _%1%_arr ?. ; push _%1%_arr
+ next
+]
+
+[ defvar 1
+ defarr %1% 1
+]
+
+[ as 1
+ _data_segment ?choose
+ defword %1%
+ lit ?$ # 0 ?.
+ exit
+ _code_segment ?choose
+ lit ?. !
+]
+
+[ literal 0
+ lit # %1% ?.
+]
+
+[ set_entry 0
+ ?$ _threaded_code_entry ?!
+]
+
+[ if 0
+ 0branch ?$ # 0 ?.
+]
+
+[ fi 0
+ ?$ ?swap ?!
+]
+
+[ else 0
+ branch ?$ # 0 ?.
+ ?swap ?$ ?swap ?!
+]
+
+[ do 0
+ ?$
+]
+
+[ untilod 0
+ 0branch ?.
+]
+
+[ until 0
+ 0 = 0branch ?$ # 0 ?.
+]
+
+[ od 0
+ branch ?swap ?.
+ ?$ ?swap ?!
+]
diff --git a/sts/ia32/math.sts b/sts/ia32/math.sts
new file mode 100644
index 0000000..609a21a
--- /dev/null
+++ b/sts/ia32/math.sts
@@ -0,0 +1,68 @@
+defasm +
+ # 58 ?' ; pop eax
+ # 01 ?' # 04 ?' # 24 ?' ; add [esp], eax
+next
+
+defasm -
+ # 58 ?' ; pop eax
+ # 29 ?' # 04 ?' # 24 ?' ; sub [esp], eax
+next
+
+defasm mul
+ # 5b ?' ; pop ebx
+ # 58 ?' ; pop eax
+ # f7 ?' # e3 ?' ; mul ebx
+ # 50 ?' ; push eax
+next
+
+defasm mod
+ # 5b ?' ; pop ebx
+ # 58 ?' ; pop eax
+ # 31 ?' # d2 ?' ; xor edx, edx
+ # f7 ?' # f3 ?' ; div ebx
+ # 52 ?' ; push edx
+next
+
+defasm div
+ # 5b ?' ; pop ebx
+ # 58 ?' ; pop eax
+ # 31 ?' # d2 ?' ; xor edx, edx
+ # f7 ?' # f3 ?' ; div ebx
+ # 50 ?' ; push eax
+next
+
+defasm =
+ # 5b ?' ; pop ebx
+ # 58 ?' ; pop eax
+ # 39 ?' # d8 ?' ; cmp eax, ebx
+ # 0f ?' # 94 ?' # c0 ?' ; sete al
+ # 0f ?' # b6 ?' # c0 ?' ; movzx eax, al
+ # 50 ?' ; push eax
+next
+
+defasm !=
+ # 5b ?' ; pop ebx
+ # 58 ?' ; pop eax
+ # 39 ?' # d8 ?' ; cmp eax, ebx
+ # 0f ?' # 95 ?' # c0 ?' ; setne al
+ # 0f ?' # b6 ?' # c0 ?' ; movzx eax, al
+ # 50 ?' ; push eax
+next
+
+defasm <
+ # 5b ?' ; pop ebx
+ # 58 ?' ; pop eax
+ # 39 ?' # d8 ?' ; cmp eax, ebx
+ # 0f ?' # 92 ?' # c0 ?' ; setb al
+ # 0f ?' # b6 ?' # c0 ?' ; movzx eax, al
+ # 50 ?' ; push eax
+next
+
+defasm >
+ # 5b ?' ; pop ebx
+ # 58 ?' ; pop eax
+ # 39 ?' # d8 ?' ; cmp eax, ebx
+ # 0f ?' # 97 ?' # c0 ?' ; seta al
+ # 0f ?' # b6 ?' # c0 ?' ; movzx eax, al
+ # 50 ?' ; push eax
+next
diff --git a/sts/ia32/memory.sts b/sts/ia32/memory.sts
new file mode 100644
index 0000000..240c598
--- /dev/null
+++ b/sts/ia32/memory.sts
@@ -0,0 +1,10 @@
+defasm @
+ # 58 ?' ; pop eax
+ # ff ?' # 30 ?' ; push dword [eax]
+next
+
+defasm !
+ # 5b ?' ; pop ebx
+ # 58 ?' ; pop eax
+ # 89 ?' # 03 ?' ; mov [ebx], eax
+next
diff --git a/sts/ia32/stack.sts b/sts/ia32/stack.sts
new file mode 100644
index 0000000..e884098
--- /dev/null
+++ b/sts/ia32/stack.sts
@@ -0,0 +1,41 @@
+defasm top
+ # 54 ?' ; push esp
+next
+
+defasm ref
+ # 58 ?' ; pop eax
+ # c1 ?' # e0 ?' # 2 ?' ; shl eax, 2
+ # 01 ?' # e0 ?' ; add eax, esp
+ # 50 ?' ; push eax
+next
+
+defasm get
+ # 58 ?' ; pop eax
+ # 8b ?' # 4 ?' # 84 ?' ; mov eax, [esp + 4 * eax]
+ # 50 ?' ; push eax
+next
+
+defasm set
+ # 5b ?' ; pop ebx
+ # 58 ?' ; pop eax
+ # 89 ?' # 04 ?' # 9c ?' ; mov [esp + 4 * ebx], eax
+next
+
+defasm drop
+ # 58 ?' ; pop eax
+next
+
+defasm dup
+ # ff ?' # 34 ?' # 24 ?' ; push dword [esp]
+next
+
+defasm over
+ # ff ?' # 74 ?' # 24 ?' # 4 ?' ; push dword [esp + 4]
+next
+
+defasm swap
+ # 58 ?' ; pop eax
+ # 5b ?' ; pop ebx
+ # 50 ?' ; push eax
+ # 53 ?' ; push ebx
+next