agssim: implemented syscalls
commit0a3ac25813cee26013dac03a1eebe1e218ac0e26
authorrofl0r <rofl0r@users.noreply.github.com>
Sat, 7 Dec 2019 02:21:36 +0000 (7 02:21 +0000)
committerrofl0r <rofl0r@users.noreply.github.com>
Sat, 7 Dec 2019 02:24:12 +0000 (7 02:24 +0000)
tree58c1d3bdfa1cf60128fba0245e90d93d91653298
parent9bceaf11a17165a101c73130b65235544517fc86
agssim: implemented syscalls

in order to have a program do something useful, we need syscalls.
since ags doesn't have a syscall instruction (it was designed
by a windows guy...) we repurpose SCMD_CALLAS (`callscr`)
instruction, as that one is never emitted in ags bytecode
(it's only used for some live-patching of stuff while the
engine runs).

currently SYS_read, SYS_write, and SYS_exit are available, modeled
after linux x86_64 ABI.

PoC "hello world" program:

```asm
jmpi main
hello:
; set mar to sp
ptrstack 0
; write 'H' to stack
li ax, 72
memwrite1 ax
addi mar, 1
; write 'e' to stack
li ax, 101
memwrite1 ax
addi mar, 1
; write 'l' to stack
li ax, 108
memwrite1 ax
addi mar, 1
; write 'l' to stack
li ax, 108
memwrite1 ax
addi mar, 1
; write 'o' to stack
li ax, 111
memwrite1 ax
addi mar, 1
; write '\n' to stack
li ax, 10
memwrite1 ax
addi mar, 1
;reset mar to point to the beginning of stack, i.e. our string
ptrstack 0
;move stack pointer past our buffer
addi sp, 8
; push number of bytes to write onto stack
li ax, 6
push ax
; push address of buffer
push mar
; push 1 (fd number of stdout)
li ax, 1
push ax
; put syscall number 1 (write) into ax
li ax, 1
; do the syscall
callscr ax
; restore stackptr to prev value so return addr is correct
mr sp, mar
; return
ret
main:
li ax, hello
call ax
; push 0 onto stack
li ax, 0
push ax
; SYS_exit
li ax, 60
callscr ax
```
agssim.c