Skip to content

Commit

Permalink
Kernel (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbirddog authored Jun 25, 2024
1 parent 413ae81 commit f2b5372
Show file tree
Hide file tree
Showing 10 changed files with 595 additions and 159 deletions.
63 changes: 29 additions & 34 deletions blue.asm
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,24 @@ segment readable executable
include "elf.inc"
include "linux.inc"
include "code_buffer.inc"

output_file:
db "a.out"
db 0x00

_simulate_compilation:
; xor edi, edi
mov al, 0x31
call _core_code.b_comma
mov al, 0xff
call _core_code.b_comma

; mov eax, 60
mov al, 0xb8
call _core_code.b_comma
mov eax, 0x3c
call _core_code.d_comma

; syscall
mov al, 0x0f
call _core_code.b_comma
mov al, 0x05
call _core_code.b_comma
ret
include "data_stack.inc"
include "dictionary.inc"
include "parser.inc"
include "to_number.inc"
include "kernel.inc"

entry $
mov [_blue.base], 10

call code_buffer_init
call kernel_init
mov rsi, blue_bye
mov ecx, blue_bye.length
xor eax, eax

call _simulate_compilation
call kernel_loop

cmp eax, SUCCESS
jne .exit
;
; write the output to ./a.out
;
Expand All @@ -62,11 +47,21 @@ entry $
mov eax, SYS_CLOSE
syscall

call code_buffer_deinit

;
; exit cleanly
;
call kernel_deinit
xor edi, edi

.exit:
mov eax, SYS_EXIT
syscall

segment readable

blue_bye:
db '49 b, 255 b, ' ; xor edi, edi
db '184 b, 60 d, ' ; mov eax, 60
db '15 b, 5 b,' ; syscall
.length = $ - blue_bye

output_file:
db "a.out"
db 0x00
7 changes: 6 additions & 1 deletion defs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
FAILURE = 0
SUCCESS = 1

INTERPRET = 0
COMPILE = 1

ERR_NOT_A_WORD = 165

_blue:
.tib dq 1
.tib_len dd 1
Expand All @@ -24,7 +29,7 @@ _data_stack:

_dictionary:
.prev_word_offset = 8
.entry_code_offset = 16
.code_offset = 16
.word_offset = 24
.length = 4096
.base dq 1
Expand Down
80 changes: 80 additions & 0 deletions kernel.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

kernel_init:
call code_buffer_init
call data_stack_init
call dictionary_init

mov [_blue.base], 10
mov [_blue.mode], INTERPRET
ret

;
; expects
; - tib value in rsi
; - tib length in ecx
; - tib in in eax
;
kernel_loop:
mov [_blue.tib], rsi
mov [_blue.tib_len], ecx
mov [_blue.tib_in], eax

.loop:
call parser_next_word
cmp [_blue.word_len], 0
je .done

call dictionary_find
test eax, eax
jz .number_or_bail

call _handle_word
jmp .loop
.number_or_bail:
call to_number
cmp eax, SUCCESS
jne .not_a_word

mov eax, edi
call data_stack_push
jmp .loop

.done:
mov eax, SUCCESS
ret

.not_a_word:
mov edi, ERR_NOT_A_WORD
ret

kernel_deinit:
call dictionary_deinit
call data_stack_deinit
call code_buffer_deinit
ret

;
; expects
; - word in rax
;
_handle_word:
test al, DE_IMMEDIATE
jnz .interpret

test [_blue.mode], INTERPRET
jnz .interpret

.interpret:
push rax
call data_stack_pop
pop rsi

add rsi, _dictionary.code_offset
mov rsi, [rsi]
call rsi
ret

.compile:
ret
124 changes: 124 additions & 0 deletions kernel_test.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
format elf64 executable 3

macro tc1 tib, tib_len, expected, expected_len {
call kernel_init

mov rsi, tib
mov ecx, tib_len
xor eax, eax

call kernel_loop

inc [test_num]
cmp eax, SUCCESS
jne failure
mov rsi, expected
mov ecx, expected_len
call check_code_buffer
call kernel_deinit
}

macro tc2 tib, tib_len {
call kernel_init

mov rsi, tib
mov ecx, tib_len
xor eax, eax

call kernel_loop

inc [test_num]
cmp eax, FAILURE
jne failure

cmp edi, ERR_NOT_A_WORD
jne failure
call kernel_deinit
}

segment readable writeable

include "defs.inc"

test_num db 1

segment readable executable

include "linux.inc"
include "code_buffer.inc"
include "data_stack.inc"
include "dictionary.inc"
include "parser.inc"
include "to_number.inc"
include "kernel.inc"

entry $
mov [test_num], 0

call kernel_init
inc [test_num]
cmp [_blue.base], 10
jne failure
inc [test_num]
cmp [_blue.mode], INTERPRET
jne failure
call kernel_deinit

tc1 clean_exit.blue, clean_exit.blue_length, \
clean_exit.expected, clean_exit.expected_length

tc2 bogus.blue, bogus.blue_length
xor edi, edi
exit:
mov eax, 60
syscall

failure:
mov dil, [test_num]
jmp exit

check_code_buffer:
mov rdi, [_code_buffer.here]
sub rdi, [_code_buffer.base]
cmp edi, ecx
jne failure

mov rdi, [_code_buffer.base]

.loop:
cmpsb
jne failure

dec ecx
jnz .loop
ret

segment readable

bogus:
.blue:
db '^%^*^%'
.blue_length = $ - .blue

clean_exit:
.blue:
db '49 b, 255 b, ' ; xor edi, edi
db '184 b, 60 d, ' ; mov eax, 60
db '15 b, 5 b,' ; syscall
.blue_length = $ - .blue

.expected:
db 0x31, 0xff
db 0xb8
dd 0x3c
db 0x0f, 0x05
.expected_length = $ - .expected
57 changes: 0 additions & 57 deletions scratch/blue.asm

This file was deleted.

3 changes: 0 additions & 3 deletions scratch/blue_test.inc

This file was deleted.

22 changes: 0 additions & 22 deletions scratch/defs.inc

This file was deleted.

Loading

0 comments on commit f2b5372

Please sign in to comment.