Skip to content

Commit

Permalink
Optimize variable/label search, a few bytes less.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsc committed Feb 26, 2018
1 parent a41c4e2 commit 944b4c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/actions.asm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
; From runtime.asm
.import read_word
; From vars.asm
.import var_search, name_new, var_getlen
.import var_search, name_new
.import label_search
.importzp var_namelen, label_count, var_count
; From alloc.asm
Expand Down Expand Up @@ -342,7 +342,6 @@ xrts: rts
lda #VT_WORD
sta tmp3 ; Store variable type
; Check if we have a valid name - this exits on error!
jsr var_getlen
; Search existing var
jsr var_search
bcs exit
Expand All @@ -357,7 +356,6 @@ exit:
; Creates a new variable, with no type (the type will be set by parser next)
.proc E_VAR_CREATE
; Check if we have a valid name - this exits on error!
jsr var_getlen
; Search existing var
jsr var_search
bcc E_VAR_WORD::not_found ; Exit with error if already exists
Expand Down Expand Up @@ -529,7 +527,6 @@ xit: rts

.proc label_create
; Check if we have a valid name - this exits on error!
jsr var_getlen
jsr label_search
bcc xit
; Create a new label
Expand Down
21 changes: 11 additions & 10 deletions src/vars.asm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
; Handles a list of names (variables or labels)
; --------------------------------------------

.export var_getlen, var_search, label_search, name_new
.export var_search, label_search, name_new
.exportzp var_namelen, label_count

; From interpreter.asm
Expand Down Expand Up @@ -82,18 +82,20 @@ char_ok:
; Inputs:
; (INBUFF + CIX) : Variable name, from parsing code, terminated in any invalid char
.proc label_search
jsr var_getlen
ldx #label_buf - prog_ptr
ldy #label_count
ldy label_count
bne list_search
.endproc

; Search the list of variables by name,
; Inputs:
; (INBUFF + CIX) : Variable name, from parsing code, terminated in any invalid char
.proc var_search
jsr var_getlen
; Pointer to var list to "var"
ldx #var_buf - prog_ptr
ldy #var_count
ldy var_count
.endproc ; Fall through

; Search a list of names - used for variables or labels
Expand All @@ -115,7 +117,7 @@ search_loop:
ldy #0
lda (var),y
cmp len
bne next_var
bne next_var_len

; Compare data
tay
Expand All @@ -129,6 +131,7 @@ next_var:
; Advance pointer to next var
ldy #0
lda (var),y
next_var_len:
clc
adc #2 ; No carry, as len is max 128
adc var
Expand All @@ -139,7 +142,7 @@ next_var:
search_start:
inx
::search_count= * + 1
cpx $12
cpx #0
bne search_loop

not_found:
Expand Down Expand Up @@ -207,17 +210,15 @@ exit_2:
; Copy length and name of var/label
ldy #0
lda len
sta (var),y
loop:
lda (name),y
iny
sta (var),y
lda (name),y
cpy len
bne loop
iny
bcc loop

; Set type to 0 initially
lda #0
iny
sta (var), y
rts
.endproc
Expand Down

0 comments on commit 944b4c8

Please sign in to comment.