From b6b9e29c1484c9c8b8af2b382d3a9d796c310217 Mon Sep 17 00:00:00 2001 From: exegete Date: Sat, 17 Oct 2020 03:33:08 +0300 Subject: Common dir added, stderr output, minor changes --- common/common.sts | 2 ++ common/const.sts | 10 ++++++ common/macro.sts | 9 +++++ ia32/bitwise.sts | 8 ++--- ia32/branch.sts | 12 +++---- ia32/const.sts | 3 ++ ia32/core.sts | 20 +++++------ ia32/elf/begin.sts | 1 + ia32/ia32.sts | 1 + ia32/macro.sts | 20 +++-------- ia32/math.sts | 97 +++++++++++++++++++++++++----------------------------- ia32/memory.sts | 10 +++--- ia32/syscall.sts | 48 ++++++++++++++++----------- test.sts | 6 ++-- 14 files changed, 131 insertions(+), 116 deletions(-) create mode 100644 common/common.sts create mode 100644 common/const.sts create mode 100644 common/macro.sts create mode 100644 ia32/const.sts diff --git a/common/common.sts b/common/common.sts new file mode 100644 index 0000000..cc5f5ee --- /dev/null +++ b/common/common.sts @@ -0,0 +1,2 @@ +module common/macro.sts +module common/const.sts diff --git a/common/const.sts b/common/const.sts new file mode 100644 index 0000000..b2e9f31 --- /dev/null +++ b/common/const.sts @@ -0,0 +1,10 @@ +defconst '0' 30 +defconst '1' 31 +defconst '2' 32 +defconst '3' 33 +defconst '4' 34 +defconst '5' 35 +defconst '6' 36 +defconst '7' 37 +defconst '8' 38 +defconst '9' 39 diff --git a/common/macro.sts b/common/macro.sts new file mode 100644 index 0000000..f8620d5 --- /dev/null +++ b/common/macro.sts @@ -0,0 +1,9 @@ +[ local 1 + top as %1% +] + +[ change 0 + dup get + swap 2 get swap 2 + set + swap drop +] diff --git a/ia32/bitwise.sts b/ia32/bitwise.sts index 6d376e4..b86e6c6 100644 --- a/ia32/bitwise.sts +++ b/ia32/bitwise.sts @@ -1,6 +1,6 @@ defasm xor - # 5b ?' ; pop ebx - # 58 ?' ; pop eax - # 31 ?' # d8 ?' ; xor eax, ebx - # 50 ?' ; push eax + # 5b ?' ; pop ebx + # 58 ?' ; pop eax + # 31 ?' # d8 ?' ; xor eax, ebx + # 50 ?' ; push eax next diff --git a/ia32/branch.sts b/ia32/branch.sts index 9c71217..35177f6 100644 --- a/ia32/branch.sts +++ b/ia32/branch.sts @@ -1,11 +1,11 @@ defasm branch - # 8b ?' # 36 ?' ; mov esi, [esi] + # 8b ?' # 36 ?' ; mov esi, [esi] next defasm 0branch - # 58 ?' ; pop eax - # 83 ?' # c6 ?' # 4 ?' ; add esi, 4 - # 83 ?' # f8 ?' # 00 ?' ; cmp eax, 0 - # 75 ?' # 3 ?' ; jne +3 - # 8b ?' # 76 ?' # fc ?' ; mov esi, [esi - 4] + # 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/ia32/const.sts b/ia32/const.sts new file mode 100644 index 0000000..31f8041 --- /dev/null +++ b/ia32/const.sts @@ -0,0 +1,3 @@ +defconst word_size 4 +defconst word_max ffffffff +defconst word_endianness 0 diff --git a/ia32/core.sts b/ia32/core.sts index 17b7d89..4a5e692 100644 --- a/ia32/core.sts +++ b/ia32/core.sts @@ -1,19 +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 + # 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 + # 8b ?' # 75 ?' # 0 ?' ; mov esi, [ebp] + # 83 ?' # c5 ?' # 4 ?' ; add ebp, 4 next defasm lit - # ad ?' ; lodsd - # 50 ?' ; push eax + # ad ?' ; lodsd + # 50 ?' ; push eax next - -defconst word_size 4 diff --git a/ia32/elf/begin.sts b/ia32/elf/begin.sts index 41c1efc..27b4714 100644 --- a/ia32/elf/begin.sts +++ b/ia32/elf/begin.sts @@ -125,3 +125,4 @@ _return_stack ?. # ff ?' # 20 ?' module ia32/ia32.sts +module common/common.sts diff --git a/ia32/ia32.sts b/ia32/ia32.sts index a667ebf..0d8a9f5 100644 --- a/ia32/ia32.sts +++ b/ia32/ia32.sts @@ -1,4 +1,5 @@ module ia32/macro.sts +module ia32/const.sts module ia32/core.sts module ia32/stack.sts module ia32/branch.sts diff --git a/ia32/macro.sts b/ia32/macro.sts index 9335176..fa9510b 100644 --- a/ia32/macro.sts +++ b/ia32/macro.sts @@ -20,7 +20,7 @@ [ defconst 2 defasm %1% - # 68 ?' # %2% ?. ; push %2% + # 68 ?' # %2% ?. ; push %2% next ] @@ -30,7 +30,7 @@ # %2% # 4 ?* ?res _code_segment ?choose defasm %1% - # 68 ?' _%1%_arr ?. ; push _%1%_arr + # 68 ?' _%1%_arr ?. ; push _%1%_arr next ] @@ -40,17 +40,13 @@ [ as 1 _data_segment ?choose - defword %1% - lit ?$ # 0 ?. - exit + defword %1% + lit ?$ # 0 ?. + exit _code_segment ?choose lit ?. ! ] -[ local 1 - top as %1% -] - [ literal 0 lit # %1% ?. ] @@ -88,9 +84,3 @@ branch ?swap ?. ?$ ?swap ?! ] - -[ change 0 - dup get - swap 2 get swap 2 + set - swap drop -] diff --git a/ia32/math.sts b/ia32/math.sts index 95ca5f7..fefe1d4 100644 --- a/ia32/math.sts +++ b/ia32/math.sts @@ -1,79 +1,70 @@ defasm + - # 58 ?' ; pop eax - # 01 ?' # 04 ?' # 24 ?' ; add [esp], eax + # 58 ?' ; pop eax + # 01 ?' # 04 ?' # 24 ?' ; add [esp], eax next defasm - - # 58 ?' ; pop eax - # 29 ?' # 04 ?' # 24 ?' ; sub [esp], eax + # 58 ?' ; pop eax + # 29 ?' # 04 ?' # 24 ?' ; sub [esp], eax next defasm / - # 5b ?' ; pop ebx - # 58 ?' ; pop eax - # 31 ?' # d2 ?' ; xor edx, edx - # f7 ?' # f3 ?' ; div ebx - # 52 ?' ; push edx - # 50 ?' ; push eax + # 5b ?' ; pop ebx + # 58 ?' ; pop eax + # 31 ?' # d2 ?' ; xor edx, edx + # f7 ?' # f3 ?' ; div ebx + # 52 ?' ; push edx + # 50 ?' ; push eax next defasm mod - # 5b ?' ; pop ebx - # 58 ?' ; pop eax - # 31 ?' # d2 ?' ; xor edx, edx - # f7 ?' # f3 ?' ; div ebx - # 52 ?' ; push edx + # 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 + # 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 + # 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 ?' ; 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 ?' ; sete al - # 0f ?' # b6 ?' # c0 ?' ; movzx eax, al - # 50 ?' ; push eax + # 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 + # 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 + # 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/ia32/memory.sts b/ia32/memory.sts index 891fab0..240c598 100644 --- a/ia32/memory.sts +++ b/ia32/memory.sts @@ -1,10 +1,10 @@ defasm @ - # 58 ?' ; pop eax - # ff ?' # 30 ?' ; push dword [eax] + # 58 ?' ; pop eax + # ff ?' # 30 ?' ; push dword [eax] next defasm ! - # 5b ?' ; pop ebx - # 58 ?' ; pop eax - # 89 ?' # 03 ?' ; mov [ebx], eax + # 5b ?' ; pop ebx + # 58 ?' ; pop eax + # 89 ?' # 03 ?' ; mov [ebx], eax next diff --git a/ia32/syscall.sts b/ia32/syscall.sts index 92d4d3a..3acfef2 100644 --- a/ia32/syscall.sts +++ b/ia32/syscall.sts @@ -1,7 +1,7 @@ defasm sys_exit - # b8 ?' # 1 ?. ; mov eax, 1 - # 31 ?' # db ?' ; xor ebx, ebx - # cd ?' # 80 ?' ; int 0x80 + # 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 @@ -9,23 +9,33 @@ _data_segment ?choose _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 + # 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 + # 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/test.sts b/test.sts index a1216ac..950a517 100644 --- a/test.sts +++ b/test.sts @@ -1,11 +1,11 @@ module ia32/elf/begin.sts set_entry -32 -31 +'2' +'1' top as what what @ sys_write -what word_size + @ sys_write +what word_size + @ sys_write_err sys_exit module ia32/elf/end.sts -- cgit v1.2.3