DEV Community

Charles Anthony
Charles Anthony

Posted on

2024-01-21 Debugging ZIP

Updated zipd.txt; it now feeds "foo" to ZIP; going to debug the "not found" path.

ZIP
foo 
:��>2�1 ?
Enter fullscreen mode Exit fullscreen mode
inner  0057     outer
inner  0BEA       type
inner  0075       inline
inner  08EA       aspace
inner  0BB3       token
inner  088F       qsearch
inner  0976         context
inner  08C5         at
inner  08C5         at
inner  0B2E         search
inner  0A31         dup
inner  0758         p_if
inner  0A71         mode
inner  0927         cat
inner  0758         p_if
inner  0031         semi
inner  0758       p_if
inner  085A       qnumber
inner  0A7A         number
inner  0758         p_if
inner  080A         one
inner  0031         semi
inner  0743       p_end
inner  0AEA       question
inner  0057     outer
inner  0BEA       type
inner  0075       inline

Enter fullscreen mode Exit fullscreen mode

It gets to QUESTION ok, so the problem is probably in there.

In QUESTION, the address of the unrecognized token is passed to TYPE, but TYPE expects the address on the stack: (It may be that the the _type label should be one instruction farther down (past the POP HL), but I decided that a consistent API was preferable.

Changed:

        dec     hl      ; back-up to token length
        jp      _type   ; go echo unknown token
Enter fullscreen mode Exit fullscreen mode

to:

        dec     hl      ; back-up to token length
        push    hl      ; XXX Fix added by CAC; type expects the address on the stack.
        jp      _type   ; go echo unknown token

Enter fullscreen mode Exit fullscreen mode

Now:

ZIP
FOO 
FOO ?
Enter fullscreen mode Exit fullscreen mode

Next up: get “.” implemented.

ZIP
DP . ��

inner  07E8         dot
inner  083A           sharpbegin
inner  08FC            abs
inner  06B8            sharps
inner  0695              sharp
inner  07FC                zero
inner  0932                base
inner  0966                cat
inner  09EC                ddmod
inner  0913                ascii
inner  0BDC                swap
inner  0031                semi
inner  0A70              dup
inner  080A              zeq
inner  0743              p_end
inner  0695              sharp
inner  07FC                zero
inner  0932                base
inner  0966                cat
inner  09EC                ddmod
inner  0913                ascii
inner  0BDC                swap
inner  0031                semi
inner  0A70              dup
inner  080A              zeq
inner  0743              p_end
inner  0695              sharp
. . .
Enter fullscreen mode Exit fullscreen mode

It keeps looping in #S  (sharps)

: #S BEGIN # DUP 0 = END DROP ;

Enter fullscreen mode Exit fullscreen mode

Looking at the ABS

08fe d1                         pop     de      ; get number
CPU: C0Z0S0V0H0N0 A =08 BC =07EE DE =00FA HL =08FE S =0331 P =08FE POP DE

Enter fullscreen mode Exit fullscreen mode

00FA is correct (the address of DP).

‘zero’ pushes a zero on the stack.

‘base cat’ pushes 16 on the stack.

ddmod:

09ef e1                         pop     hl      ; get 8 bit divisor
CPU: C0Z1S0V0H0N1 A =09 BC =0000 DE =0006 HL =0010 S =032D P =09EF POP HL

Enter fullscreen mode Exit fullscreen mode

Divisor is 16; correct

09f0 d1                         pop     de      ; 8 most significant
CPU: C0Z1S0V0H0N1 A =09 BC =0000 DE =0000 HL =0010 S =032F P =09F0 POP DE

09f1 c1                         pop     bc      ; get 16 least
CPU: C0Z1S0V0H0N1 A =09 BC =00FA DE =0000 HL =0010 S =0331 P =09F1 POP BC

Enter fullscreen mode Exit fullscreen mode

dividend is 0000FA; correct

09f2 7c                         ld      a,h     ; divisor sign
CPU: C0Z1S0V0H0N1 A =00 BC =00FA DE =0000 HL =0010 S =0331 P =09F2 LD A,H

09f3 aa                         xor     d       ; result sign
CPU: C0Z1S0V1H0N0 A =00 BC =00FA DE =0000 HL =0010 S =0331 P =09F3 XOR D

09f4 08                         ex      af,af'  ; save sign flag
CPU: C0Z0S0V0H1N0 A =02 BC =00FA DE =0000 HL =0010 S =0331 P =09F4 EX AF,AF'
                  A'=00 BC'=069F DE'=09EE HL'=09EE IX=0528 IY=003D
Enter fullscreen mode Exit fullscreen mode

Sign flag is 0; correct

09f5 7d                         ld      a,l     ; get dividend sign
CPU: C0Z0S0V0H1N0 A =10 BC =00FA DE =0000 HL =0010 S =0331 P =09F5 LD A,L

09f6 a7                         and     a       ; test sign
CPU: C0Z0S0V0H1N0 A =10 BC =00FA DE =0000 HL =0010 S =0331 P =09F6 AND A

09f7 f2 fc 09                   jp      p,mov1  ; if +, its ok
CPU: C0Z0S0V0H1N0 A =10 BC =00FA DE =0000 HL =0010 S =0331 P =09F7 JP P,09FCh
Enter fullscreen mode Exit fullscreen mode

Dividend is positive; correct.

09fc 57                 mov1:   ld      d,a     ; store divisor
CPU: C0Z0S0V0H1N0 A =10 BC =00FA DE =1000 HL =0010 S =0331 P =09FC LD D,A

09fd 60                         ld      h,b     ; get 16 least
CPU: C0Z0S0V0H1N0 A =10 BC =00FA DE =1000 HL =0010 S =0331 P =09FD LD H,B

09fe 69                         ld      l,c     ; to hl
CPU: C0Z0S0V0H1N0 A =10 BC =00FA DE =1000 HL =00FA S =0331 P =09FE LD L,C
HL is 00FA; correct.

09ff 7b                         ld      a,e     ; get 8 most
CPU: C0Z0S0V0H1N0 A =00 BC =00FA DE =1000 HL =00FA S =0331 P =09FF LD A,E

0a00 a7                         and     a       ; test sign
CPU: C0Z1S0V1H1N0 A =00 BC =00FA DE =1000 HL =00FA S =0331 P =0A00 AND A

0a01 f2 0e 0a                   jp      p,mov2  ; if +. its ok
CPU: C0Z1S0V1H1N0 A =00 BC =00FA DE =1000 HL =00FA S =0331 P =0A01 JP P,0A0Eh
Enter fullscreen mode Exit fullscreen mode

Dividend is positive; correct.

0a0e 57                 mov2:   ld      d,a     ; move high 8
CPU: C0Z1S0V1H1N0 A =00 BC =00FA DE =0000 HL =00FA S =0331 P =0A0E LD D,A

0a0f cd 0e 07                   call    pudd    ; divide 24X8
CPU: C0Z1S0V1H1N0 A =00 BC =00FA DE =0000 HL =00FA S =032F P =0A0F CALL 070Eh

070e 06 10              pudd:   ld      b, 16   ; divide count 16
CPU: C0Z1S0V1H1N0 A =00 BC =10FA DE =0000 HL =00FA S =032F P =070E LD B,10h

0710 29                 dloop1: add     hl,hl   ; shift low 16
CPU: C0Z1S0V1H0N0 A =00 BC =10FA DE =0000 HL =01F4 S =032F P =0710 ADD HL,HL
Enter fullscreen mode Exit fullscreen mode

HL is 01F4  (00FA + 00FA); correct.

0711 7a                         ld      a,d     ; get high 8
CPU: C0Z1S0V1H0N0 A =00 BC =10FA DE =0000 HL =01F4 S =032F P =0711 LD A,D

0712 8a                         adc     a,d     ; shift high 8
CPU: C0Z1S0V0H0N0 A =00 BC =10FA DE =0000 HL =01F4 S =032F P =0712 ADC A,D
Enter fullscreen mode Exit fullscreen mode

A is 0 (0 + 0); correct.

0713 57                         ld      d,a     ; restore high
CPU: C0Z1S0V0H0N0 A =00 BC =10FA DE =0000 HL =01F4 S =032F P =0713 LD D,A

0714 93                         sub     e       ; subtract divisor
CPU: C0Z1S0V0H0N1 A =00 BC =10FA DE =0000 HL =01F4 S =032F P =0714 SUB E

0715 fa 1a 07                   jp      m,skip1 ; too much, it;s ok
CPU: C0Z1S0V0H0N1 A =00 BC =10FA DE =0000 HL =01F4 S =032F P =0715 JP M,071Ah

0718 2c                         inc     l       ; set result low bit = 1
CPU: C0Z0S1V0H0N0 A =00 BC =10FA DE =0000 HL =01F5 S =032F P =0718 INC L

0719 57                         ld      d,a     ; decrease dividend
CPU: C0Z0S1V0H0N0 A =00 BC =10FA DE =0000 HL =01F5 S =032F P =0719 LD D,A

071a 10 f4              skip1:  djnz    dloop1  ; loop 16 times
CPU: C0Z0S1V0H0N0 A =00 BC =0FFA DE =0000 HL =01F5 S =032F P =071A DJNZ 0710h

. . .

071a 10 f4              skip1:  djnz    dloop1  ; loop 16 times 
CPU: C0Z0S1V0H0N1 A =FA BC =00FA DE =FA00 HL =FFFE S =032F P =071A DJNZ 0710h

071c 4a                         ld      c,d     ; remainder to C
CPU: C0Z0S1V0H0N1 A =FA BC =00FA DE =FA00 HL =FFFE S =032F P =071C LD C,D

071d c9                         ret             ; quotient in HL 
CPU: C0Z0S1V0H0N1 A =FA BC =00FA DE =FA00 HL =FFFE S =0331 P =071D RET
                  A'=00 BC'=069F DE'=09EE HL'=09EE IX=0528 IY=003D
Enter fullscreen mode Exit fullscreen mode

0000FA / 10 => quotient F, remainder A

Wrong; code returned quotient FFFE, remainder FA.

Top comments (0)