Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 39 additions & 37 deletions sectorc.s
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,34 @@ execute:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; compile statements (optionally advancing tokens beforehand)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
_is_call:
mov al,0xe8 ; emit "call" instruction
stosb

mov ax,[bx] ; load function offset from symbol-table
sub ax,di ; compute relative to this location: "dest - cur - 2"
dec ax
dec ax
stosw ; emit target
;; [fall-through] ; loop to compile next statement

compile_stmts_tok_next2:
call tok_next
compile_stmts_tok_next:
call tok_next
db 0x81 ; mask following call

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this 0x81 byte do?

;; [fall-through]
_not_while:
call compile_assign ; handle an assignment statement
;; [fall-through] ; loop to compile next statement

compile_stmts:
mov ax,bx
xchg bx,ax
cmp ax,TOK_BLK_END ; if we reach '}' then return
je return

test dh,dh ; if dh is 0, it's not a call
je _not_call
mov al,0xe8 ; emit "call" instruction
stosb

mov ax,[bx] ; load function offset from symbol-table
sub ax,di ; compute relative to this location: "dest - cur - 2"
sub ax,2
stosw ; emit target

jmp compile_stmts_tok_next2 ; loop to compile next statement
jne _is_call

_not_call:
cmp ax,TOK_ASM ; check for "asm"
Expand All @@ -137,18 +145,15 @@ _not_if:
jne _not_while
push di ; save loop start location
call _control_flow_block ; compile control-flow block
jmp _patch_back ; patch up backward and forward jumps of while-stmt

_not_while:
call compile_assign ; handle an assignment statement
jmp compile_stmts ; loop to compile next statement
;; [fall-through] ; patch up backward and forward jumps of while-stmt

_patch_back:
mov al,0xe9 ; emit "jmp" instruction (backwards)
stosb
pop ax ; restore loop start location
sub ax,di ; compute relative to this location: "dest - cur - 2"
sub ax,2
dec ax
dec ax
stosw ; emit target
;; [fall-through]
_patch_fwd:
Expand Down Expand Up @@ -180,7 +185,7 @@ return: ; this label gives us a way to do conditional re
compile_assign:
cmp ax,TOK_DEREF ; check for "*(int*)"
jne _not_deref_store
call tok_next ; consome "*(int*)"
call tok_next ; consume "*(int*)"
call save_var_and_compile_expr ; compile rhs first
;; [fall-through]

Expand All @@ -207,7 +212,7 @@ compile_store:
jmp emit_var ; [tail-call]

save_var_and_compile_expr:
mov bp,bx ; save dest to bp
xchg bp,ax ; save dest to bp
call tok_next ; consume dest
;; [fall-through] ; fall-through will consume "=" before compiling expr

Expand All @@ -219,22 +224,18 @@ compile_expr_tok_next:
compile_expr:
call compile_unary ; compile left-hand side

push ds ; need to swap out 'ds' to scan the table with lodsw
push cs
pop ds

push ds
push cs ; cannot use cs override!
pop ds ; because ';' here must be retained separately
mov si,binary_oper_tbl - 2 ; load ptr to operator table (biased backwards)
_check_next:
lodsw ; discard 16-bit of machine-code
lodsw ; load 16-bit token value
cmp ax,bx ; matches token?
je _found
test ax,ax ; end of table?
je _not_found
cmp ax,bx ; matches token?
jne _check_next

pop ds
ret ; all-done, not found

_found:
lodsw ; load 16-bit of machine-code
push ax ; save it to the stack
Expand All @@ -258,8 +259,10 @@ emit_cmp_op:
;; [fall-through]

emit_op:
mov ax,bx
xchg bx,ax
stosw ; emit machine code for op

_not_found:
pop ds
ret

Expand Down Expand Up @@ -305,10 +308,10 @@ emit_var:
;; [fall-through]

emit_tok:
mov ax,bx
xchg bx,ax
stosw ; emit token value
jmp tok_next ; [tail-call]

db 0x81 ; mask following call [tail-call]
;; [fall-through]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; get next token, setting the following:
;;; ax: token
Expand All @@ -334,7 +337,7 @@ _nextch:
cmp al,32
jle _done ; if char is space then break

shl cx,8
mov ch,cl
mov cl,al ; shift this char into cx

imul bx,10
Expand All @@ -345,7 +348,7 @@ _nextch:
jmp _nextch ; [loop]

_done:
mov ax,cx
xchg cx,ax
cmp ax,0x2f2f ; check for single-line comment "//"
je _comment_double_slash
cmp ax,0x2f2a ; check for multi-line comment "/*"
Expand Down Expand Up @@ -381,16 +384,15 @@ getch:

getch_tryagain:
mov ax,0x0200
xor dx,dx
cwd
int 0x14 ; get a char from serial (bios function)

and ah,0x80 ; check for failure and clear ah as a side-effect
jne getch_tryagain ; failed, try again later

cmp al,59 ; check for ';'
jne getch_done ; if not ';' return it
mov [si],ax ; save the ';'
xor ax,ax ; return 0 instead, treated as whitespcae
xchg [si],ax ; save the ';', return 0 instead, treated as whitespcae

getch_done:
pop dx
Expand Down