lang-bootstrap/04b/in04b

185 lines
2.6 KiB
Text
Raw Normal View History

2021-11-19 09:52:27 -05:00
; declaration:
2022-01-06 11:57:55 -05:00
; global <name>
; local <name>
; argument <name>
2021-11-19 09:52:27 -05:00
; :<label>
; statement:
; <declaration>
2022-01-06 17:37:02 -05:00
; if <term> <==/</>/>=/<=/!=/[/]/[=/]=> <term> goto <label> NOTE: this uses signed comparisons
2021-11-19 09:52:27 -05:00
; goto <label>
; <lvalue> = <rvalue>
; <lvalue> += <rvalue>
; <lvalue> -= <rvalue>
; <function>(<term>, <term>, ...)
; return <rvalue>
2022-01-05 23:44:04 -05:00
; string <str>
2021-11-19 09:52:27 -05:00
; byte <number>
; term:
; <var>
2022-01-06 11:57:55 -05:00
; .<label>
2021-11-19 09:52:27 -05:00
; <number>
; number:
; 'c
; 12345
; 0xabc
; lvalue:
; <var>
2022-01-06 11:57:55 -05:00
; *1<var> / *2<var> / *4<var> / *8<var>
2021-11-19 09:52:27 -05:00
; rvalue:
2022-01-06 11:57:55 -05:00
; <term>
2021-11-19 09:52:27 -05:00
; &<var>
2022-01-06 11:57:55 -05:00
; *1<var> / *2<var> / *4<var> / *8<var>
; ~<term>
2021-11-19 09:52:27 -05:00
; <function>(<term>, <term>, ...)
; <term> + <term>
; <term> - <term>
; NOTE: *, /, % are signed (imul and idiv)
; <term> * <term>
; <term> / <term>
; <term> % <term>
; <term> & <term>
; <term> | <term>
; <term> ^ <term>
; <term> < <term> (left shift)
; <term> > <term> (unsigned right shift)
2022-01-06 17:54:03 -05:00
main()
2022-01-06 17:37:02 -05:00
2022-01-06 17:54:03 -05:00
:main
function
puts(.str_hello_world)
putc(10) ; newline
syscall(0x3c, 0)
:str_hello_world
string Hello, world!
byte 0
2022-01-06 17:37:02 -05:00
2022-01-06 17:54:03 -05:00
:strlen
function
argument s
local len
local c
local p
len = 0
:strlen_loop
p = s + len
c = *1p
if c == 0 goto strlen_loop_end
len += 1
goto strlen_loop
:strlen_loop_end
return len
2022-01-06 17:37:02 -05:00
2022-01-06 17:54:03 -05:00
:putc
2022-01-06 17:37:02 -05:00
function
2022-01-06 17:54:03 -05:00
argument c
local p
p = &c
syscall(1, 1, p, 1)
return
2022-01-06 13:53:52 -05:00
2022-01-06 17:54:03 -05:00
:puts
function
argument s
local len
len = strlen(s)
syscall(1, 1, s, len)
return
2022-01-06 14:02:30 -05:00
2022-01-06 13:53:52 -05:00
:syscall
function
; I've done some testing, and this should be okay even if
; rbp-56 goes beyond the end of the stack.
; mov rax, [rbp-16]
byte 0x48
byte 0x8b
byte 0x85
byte 0xf0
byte 0xff
byte 0xff
byte 0xff
; mov rdi, rax
byte 0x48
byte 0x89
byte 0xc7
; mov rax, [rbp-24]
byte 0x48
byte 0x8b
byte 0x85
byte 0xe8
byte 0xff
byte 0xff
byte 0xff
; mov rsi, rax
byte 0x48
byte 0x89
byte 0xc6
; mov rax, [rbp-32]
byte 0x48
byte 0x8b
byte 0x85
byte 0xe0
byte 0xff
byte 0xff
byte 0xff
; mov rdx, rax
byte 0x48
byte 0x89
byte 0xc2
; mov rax, [rbp-40]
byte 0x48
byte 0x8b
byte 0x85
byte 0xd8
byte 0xff
byte 0xff
byte 0xff
; mov r10, rax
byte 0x49
byte 0x89
byte 0xc2
; mov rax, [rbp-48]
byte 0x48
byte 0x8b
byte 0x85
byte 0xd0
byte 0xff
byte 0xff
byte 0xff
; mov r8, rax
byte 0x49
byte 0x89
byte 0xc0
; mov rax, [rbp-56]
byte 0x48
byte 0x8b
byte 0x85
byte 0xc8
byte 0xff
byte 0xff
byte 0xff
; mov r9, rax
byte 0x49
byte 0x89
byte 0xc1
; mov rax, [rbp-8]
byte 0x48
byte 0x8b
byte 0x85
byte 0xf8
byte 0xff
byte 0xff
byte 0xff
; syscall
byte 0x0f
byte 0x05
return