collapseos/apps/zasm/main.asm

119 lines
2.1 KiB
NASM
Raw Normal View History

2019-05-09 14:09:40 -04:00
; *** Requirements ***
; blockdev
2019-05-09 14:09:40 -04:00
; JUMP_STRNCMP
; JUMP_ADDDE
2019-05-09 21:21:08 -04:00
; JUMP_ADDHL
2019-05-09 14:09:40 -04:00
; JUMP_UPCASE
; JUMP_UNSETZ
; JUMP_INTODE
2019-05-09 21:21:08 -04:00
; JUMP_FINDCHAR
; JUMP_BLKSEL
2019-05-09 21:21:08 -04:00
; RAMSTART (where we put our variables in RAM)
2019-04-30 15:51:39 -04:00
jp main
#include "util.asm"
.equ IO_RAMSTART RAMSTART
#include "io.asm"
#include "parse.asm"
#include "literal.asm"
#include "instr.asm"
#include "directive.asm"
.equ SYM_RAMSTART IO_RAMEND
#include "symbol.asm"
2019-04-30 15:51:39 -04:00
; *** Code ***
; Read file through blockdev ID in H and outputs its upcodes through blockdev
; ID in L.
2019-04-30 15:51:39 -04:00
main:
ld a, h
ld de, IO_IN_GETC
call JUMP_BLKSEL
ld a, l
ld de, IO_OUT_GETC
call JUMP_BLKSEL
2019-05-09 21:21:08 -04:00
ld hl, 0
ld (curOutputOffset), hl
2019-04-30 15:51:39 -04:00
.loop:
call ioReadLine
or a ; is A 0?
jr z, .stop ; We have EOF
2019-04-30 15:51:39 -04:00
call parseLine
jr nz, .stop
2019-04-30 15:51:39 -04:00
jr .loop
.stop:
ret
2019-05-09 21:21:08 -04:00
; Increase (curOutputOffset) by A
incOutputOffset:
push de
ld de, (curOutputOffset)
call JUMP_ADDDE
ld (curOutputOffset), de
pop de
ret
; Parse line in (HL), write the resulting opcode(s) in (DE) and increases
; (curOutputOffset) by the number of bytes written. Advances HL where
; tokenization stopped and DE to where we should write the next upcode.
; Sets Z if parse was successful, unset if there was an error or EOF.
2019-04-30 15:51:39 -04:00
parseLine:
push bc
2019-04-30 15:51:39 -04:00
call tokenize
2019-04-30 22:27:11 -04:00
ld a, b ; TOK_*
2019-04-30 21:40:22 -04:00
cp TOK_INSTR
jr z, .instr
2019-05-01 11:26:41 -04:00
cp TOK_DIRECTIVE
jr z, .direc
2019-05-09 21:21:08 -04:00
cp TOK_LABEL
jr z, .label
cp TOK_EMPTY
jr z, .success ; empty line? do nothing but don't error out.
2019-05-01 11:26:41 -04:00
jr .error ; token not supported
2019-04-30 21:40:22 -04:00
.instr:
2019-04-30 22:27:11 -04:00
ld a, c ; I_*
2019-04-30 21:40:22 -04:00
call parseInstruction
2019-04-30 15:51:39 -04:00
or a ; is zero?
jr z, .error
2019-05-09 21:21:08 -04:00
call incOutputOffset
ld b, a
2019-05-01 11:26:41 -04:00
ld hl, instrUpcode
.loopInstr:
ld a, (hl)
call ioPutC
inc hl
djnz .loopInstr
2019-05-01 11:26:41 -04:00
jr .success
.direc:
ld a, c ; D_*
call parseDirective
2019-05-09 21:21:08 -04:00
call incOutputOffset
ld b, a
2019-05-01 11:26:41 -04:00
ld hl, direcData
.loopDirec:
ld a, (hl)
call ioPutC
inc hl
djnz .loopDirec
jr .success
2019-05-09 21:21:08 -04:00
.label:
; The string in (scratchpad) is a label with its trailing ':' removed.
ld hl, scratchpad
ld de, (curOutputOffset)
call symRegister
jr .success
.success:
xor a ; ensure Z
2019-04-30 15:51:39 -04:00
jr .end
.error:
call JUMP_UNSETZ
2019-04-30 15:51:39 -04:00
.end:
pop bc
ret
2019-05-09 21:21:08 -04:00
; *** Variables ***
; The offset where we currently are with regards to outputting opcodes
curOutputOffset:
.fill 2