Skip to content

Commit e9979c5

Browse files
committed
Updates bound.asm
This new routine: 1) Returns num of dims for both LBound(var), UBound(var) 2) Returns 0 for both LBound(var, n), Ubound(var, n) if n > num dims 3) Returns the right LBound, UBound value otherwise Also fixes comments
1 parent 2f44c43 commit e9979c5

1 file changed

Lines changed: 62 additions & 8 deletions

File tree

library-asm/bound.asm

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,67 @@
66
; This code is released under the BSD License
77
; ---------------------------------------------------------
88

9-
; Implements bothe the LBOUND(array, N) and RBOUND(array, N) function
10-
9+
; Implements both LBOUND(array, N) and UBOUND(array, N) function
1110
; Parameters:
12-
; HL = N (dimension)
13-
; [stack - 2] -> LBound table for the var
14-
; Returns entry [N] in HL
11+
; HL = PTR to array
12+
; [stack - 2] -> N (dimension)
13+
14+
PROC
15+
LOCAL __BOUND
16+
LOCAL __DIM_NOT_EXIST
17+
LOCAL __CONT
18+
19+
__LBOUND:
20+
ld a, 4
21+
jr __BOUND
22+
23+
__UBOUND:
24+
ld a, 6
1525

1626
__BOUND:
27+
ex de, hl ; DE <-- Array ptr
28+
pop hl ; HL <-- Ret address
29+
ex (sp), hl ; CALLEE: HL <-- N, (SP) <-- Ret address
30+
ex de, hl ; DE <-- N, HL <-- ARRAY_PTR
31+
32+
push hl
33+
ld c, (hl)
34+
inc hl
35+
ld h, (hl)
36+
ld l, c ; HL = start of dimension table (first position contains number of dimensions - 1)
37+
ld c, (hl)
38+
inc hl
39+
ld b, (hl)
40+
inc bc ; Number of total dimensions of the array
41+
pop hl ; Recovers ARRAY PTR
42+
ex af, af' ; Saves A for later
43+
ld a, d
44+
or e
45+
jr nz, __CONT ; N = 0 => Return number of dimensions
46+
47+
;; Return the number of dimensions of the array
48+
ld h, b
49+
ld l, c
50+
ret
51+
52+
__CONT:
53+
dec de
54+
ex af, af' ; Recovers A (contains PTR offset)
55+
ex de, hl ; HL = N (dimension asked) - 1, DE = Array PTR
56+
or a
57+
sbc hl, bc ; if no Carry => the user asked for a dimension that does not exist. Return 0
58+
jr nc, __DIM_NOT_EXIST
59+
60+
add hl, bc ; restores HL = (N - 1)
1761
add hl, hl ; hl *= 2
18-
ex de, hl
19-
pop hl
20-
ex (sp), hl ; __CALLEE
62+
ex de, hl ; hl = ARRAY_PTR + 3, DE jsz = (N - 1) * 2
63+
ld b, 0
64+
ld c, a
65+
add hl, bc ; HL = &BOUND_PTR
66+
ld a, (hl)
67+
inc hl
68+
ld h, (hl)
69+
ld l, a ; LD HL, (HL) => Origin of L/U Bound table
2170

2271
add hl, de ; hl += OFFSET __LBOUND._xxxx
2372
ld e, (hl) ; de = (hl)
@@ -27,3 +76,8 @@ __BOUND:
2776
ex de, hl ; hl = de => returns result in HL
2877
ret
2978

79+
__DIM_NOT_EXIST:
80+
; The dimension requested by the user does not exists. Return 0
81+
ld hl, 0
82+
ret
83+
ENDP

0 commit comments

Comments
 (0)