6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Nov 23, 2024 3:19 pm

All times are UTC




Post new topic Reply to topic  [ 41 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
PostPosted: Mon Jun 29, 2020 12:33 am 
Offline
User avatar

Joined: Tue Mar 05, 2013 4:31 am
Posts: 1385
Just a simple reply.... I've taken the basic programs listed here, converted them to upper-case and have run them on Enhanced Basic (the CMOS version I worked on). The output is the same as what Bill is getting on other Basic interpreters.

_________________
Regards, KM
https://github.com/floobydust


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 29, 2020 12:56 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
I think we're working along similar wavelengths now. :)

In my experiences with BASIC interpreters, the variable name appended to NEXT is completely optional and only a sanity check; if it doesn't match the frame at the top of the FOR stack, then NEXT pops destructively through the stack until it finds a match or triggers a "NEXT WITHOUT FOR" abort.

Attachment:
loopijji.PNG
loopijji.PNG [ 9.43 KiB | Viewed 1633 times ]


Attachment:
loopijij.PNG
loopijij.PNG [ 10.55 KiB | Viewed 1633 times ]


In the second example the J frame is quietly discarded by each execution of NEXT I, so NEXT J couldn't eventually find it.

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 29, 2020 2:31 am 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 704
Location: North Tejas
That is not universal behavior.

Code:
10 for i = 1 to 3
20 for j = 1 to 3
30 print i,j
40 next i
50 next j
60 end


with these interpreters:

https://yohan.es/swbasic/

1 1
LINE 40 ERROR: Expected NEXT J got NEXT i

6800 FLEX BASIC

1 1
2 1
3 1

ERROR #62 AT LINE 50 (Error #62 is "FOR-NEXT" NEST ERROR)

GWBASIC

NEXT without FOR in 50

And a new one: basic at http://telehack.com/

1 1
2 1
3 1

50 next j
^
NEXT WITHOUT FOR 1 AT LINE 50


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 21, 2020 11:17 pm 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 704
Location: North Tejas
Thanks to feedback on several forums, I have come to an understanding of how the FOR and NEXT statements commonly behave in interpreters so that they can finally be implemented in my compiler.

viewtopic.php?f=2&t=6182
https://talk.dallasmakerspace.org/t/a-t ... asic/72013

The following code has been written to approximate what the compiler can be expected to generate for this program:

Code:
10 FOR I = 1 TO 3
20 PRINT I
30 NEXT I


Code:
                          00068 * 00010 FOR I = 1 TO 3
 010D                     00069 L00010
 010D BD 0248         [9] 00070          jsr    ForPush   ; Get new FOR context
                          00071
 0110 86 01           [2] 00072          ldaa   #T00001>>8 ; Store top of loop address
 0112 A7 04           [6] 00073          staa   4,X
 0114 86 26           [2] 00074          ldaa   #T00001&$FF
 0116 A7 05           [6] 00075          staa   5,X
                          00076
 0118 86 06           [2] 00077          ldaa   #I>>8     ; Store variable address
 011A A7 06           [6] 00078          staa   6,X
 011C 86 0F           [2] 00079          ldaa   #I&$FF
 011E A7 07           [6] 00080          staa   7,X
                          00081
 0120 86 00           [2] 00082          ldaa   #1>>8     ; Initialize variable
 0122 C6 01           [2] 00083          ldab   #1&$FF
                          00084
 0124 20 21 (0147)    [4] 00085          bra    T00003    ; Execute the body of the loop
                          00086
 0126                     00087 T00001
 0126 B6 060F         [4] 00088          ldaa   I         ; Load variable
 0129 F6 0610         [4] 00089          ldab   I+1
                          00090
 012C CB 01           [2] 00091          addb   #1&$FF    ; Add step
 012E 89 00           [2] 00092          adca   #1>>8
                          00093
 0130 81 00           [2] 00094          cmpa   #3>>8     ; Compare with limit
 0132 22 06 (013A)    [4] 00095          bhi    T00002    ; Higher, exit loop
 0134 25 11 (0147)    [4] 00096          blo    T00003    ; Lower, iterate loop
 0136 C1 03           [2] 00097          cmpb   #3&$FF
 0138 23 0D (0147)    [4] 00098          bls    T00003    ; Not higher, iterate loop
                          00099
 013A                     00100 T00002
 013A FE 0611         [5] 00101          ldx    ForTop    ; Address context
                          00102
 013D A6 09           [5] 00103          ldaa   9,X       ; Push address of bottom of loop
 013F 36              [4] 00104          psha
 0140 A6 08           [5] 00105          ldaa   8,X
 0142 36              [4] 00106          psha
                          00107
 0143 BD 027B         [9] 00108          jsr    ForPull   ; Remove FOR context
                          00109
 0146 39              [5] 00110          rts              ; "Jump" to bottom of loop
                          00111
 0147                     00112 T00003
 0147 B7 060F         [5] 00113          staa   I         ; Update variable
 014A F7 0610         [5] 00114          stab   I+1
                          00115
                          00116 * 00020 PRINT I
 014D                     00117 L00020
 014D FE 060F         [5] 00118          ldx    I
 0150 BD 0428         [9] 00119          jsr    PInt
                          00120
 0153 BD 04CB         [9] 00121          jsr    NewLine
                          00122
                          00123 * 00030 NEXT I
 0156                     00124 L00030
 0156 86 06           [2] 00125          ldaa   #I>>8     ; Address the variable
 0158 C6 0F           [2] 00126          ldab   #I&$FF
                          00127
 015A BD 0225         [9] 00128          jsr    Next
                          00129
 015D                     00130 End_


Three subroutines provide support for a FOR-NEXT loop:

ForPush - Create a new loop context
ForPull - Remove a loop context
Next - Process the bottom of a loop

Only Next is interesting; when the specified variable does not match the one at the top of the loop, contexts are discarded until a matching one is found or there are none left.

Code:
.                         00354 ******************************************************************************
.                         00355 *
.                         00356 * Next - Process NEXT
.                         00357 *
.                         00358 * Input:
.                         00359 *       A:B = variable address
.                         00360 *
.0225                     00361 Next
.0225 FE 0611         [5] 00362          ldx    ForTop    ; Point to current context
.0228 27 12 (023C)    [4] 00363          beq    Next1     ; Not within a FOR loop
.                         00364
.022A                     00365 Next0
.022A A1 06           [5] 00366          cmpa   6,X       ; Compare variable address
.022C 26 0E (023C)    [4] 00367          bne    Next1     ; Loop variable mismatch
.022E E1 07           [5] 00368          cmpb   7,X
.0230 26 0A (023C)    [4] 00369          bne    Next1
.                         00370
.0232 32              [4] 00371          pula             ; Get and save bottom of loop
.0233 A7 08           [6] 00372          staa   8,X
.0235 32              [4] 00373          pula
.0236 A7 09           [6] 00374          staa   9,X
.                         00375
.0238 EE 04           [6] 00376          ldx    4,X       ; Jump to top of loop
.023A 6E 00           [4] 00377          jmp    ,X
.                         00378
.023C                     00379 Next1
.023C EE 02           [6] 00380          ldx    2,X       ; Get previous context
.023E FF 0611         [6] 00381          stx    ForTop    ; Save it
.0241 26 E7 (022A)    [4] 00382          bne    Next0     ; Retry if valid
.                         00383
.0243 86 3E           [2] 00384          ldaa   #62       ; Report FOR-NEXT nesting error
.0245 7E 0284         [3] 00385          jmp    ErrH


Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 22, 2020 12:45 am 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 704
Location: North Tejas
Found a nasty bug in Next:

Code:
.                         00354 ******************************************************************************
.                         00355 *
.                         00356 * Next - Process NEXT
.                         00357 *
.                         00358 * Input:
.                         00359 *       A:B = variable address
.                         00360 *
.0225                     00361 Next
.0225 FE 0611         [5] 00362          ldx    ForTop    ; Point to current context
.0228 27 19 (0243)    [4] 00363          beq    Next2     ; Not within a FOR loop
.                         00364
.022A                     00365 Next0
.022A A1 06           [5] 00366          cmpa   6,X       ; Compare variable address
.022C 26 0E (023C)    [4] 00367          bne    Next1     ; Loop variable mismatch
.022E E1 07           [5] 00368          cmpb   7,X
.0230 26 0A (023C)    [4] 00369          bne    Next1
.                         00370
.0232 32              [4] 00371          pula             ; Get and save bottom of loop
.0233 A7 08           [6] 00372          staa   8,X
.0235 32              [4] 00373          pula
.0236 A7 09           [6] 00374          staa   9,X
.                         00375
.0238 EE 04           [6] 00376          ldx    4,X       ; Jump to top of loop
.023A 6E 00           [4] 00377          jmp    ,X
.                         00378
.023C                     00379 Next1
.023C EE 02           [6] 00380          ldx    2,X       ; Get previous context
.023E FF 0611         [6] 00381          stx    ForTop    ; Save it
.0241 26 E7 (022A)    [4] 00382          bne    Next0     ; Retry if valid
.                         00383 Next2
.0243 86 3E           [2] 00384          ldaa   #62       ; Report FOR-NEXT nesting error
.0245 7E 0284         [3] 00385          jmp    ErrH


Also, this

Code:
                          00107
 0143 BD 027B         [9] 00108          jsr    ForPull   ; Remove FOR context
                          00109
 0146 39              [5] 00110          rts              ; "Jump" to bottom of loop
                          00111


should be just a jump instead of a jsr followed by rts.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 22, 2020 3:12 pm 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 704
Location: North Tejas
From the school of Procrastination as a Valid Software Technique, the address of the bottom of the loop is kept on the stack until it is needed or discarded:

Code:
                          00068 * 00010 FOR I = 1 TO 3
 010D                     00069 L00010
 010D BD 023A         [9] 00070          jsr    ForPush   ; Get new FOR context
                          00071
 0110 86 01           [2] 00072          ldaa   #T00001>>8 ; Store top of loop address
 0112 A7 04           [6] 00073          staa   4,X
 0114 86 26           [2] 00074          ldaa   #T00001&$FF
 0116 A7 05           [6] 00075          staa   5,X
                          00076
 0118 86 06           [2] 00077          ldaa   #I>>8     ; Store variable address
 011A A7 06           [6] 00078          staa   6,X
 011C 86 01           [2] 00079          ldaa   #I&$FF
 011E A7 07           [6] 00080          staa   7,X
                          00081
 0120 86 00           [2] 00082          ldaa   #1>>8     ; Initialize variable
 0122 C6 01           [2] 00083          ldab   #1&$FF
                          00084
 0124 20 19 (013F)    [4] 00085          bra    T00004    ; Execute the body of the loop
                          00086
 0126                     00087 T00001
 0126 B6 0601         [4] 00088          ldaa   I         ; Load variable
 0129 F6 0602         [4] 00089          ldab   I+1
                          00090
 012C CB 01           [2] 00091          addb   #1&$FF    ; Add step
 012E 89 00           [2] 00092          adca   #1>>8
                          00093
 0130 81 00           [2] 00094          cmpa   #3>>8     ; Compare with limit
 0132 22 06 (013A)    [4] 00095          bhi    T00002    ; Higher, exit loop
 0134 25 07 (013D)    [4] 00096          blo    T00003    ; Lower, iterate loop
 0136 C1 03           [2] 00097            cmpb   #3&$FF
 0138 23 03 (013D)    [4] 00098          bls    T00003    ; Not higher, iterate loop
                          00099
 013A                     00100 T00002
 013A 7E 026D         [3] 00101          jmp    ForPull   ; Remove FOR context and exit loop
                          00102
 013D                     00103 T00003
 013D 31              [4] 00104          ins              ; Discard return address from Next
 013E 31              [4] 00105          ins
                          00106
 013F                     00107 T00004
 013F B7 0601         [5] 00108          staa   I         ; Update variable
 0142 F7 0602         [5] 00109          stab   I+1
                          00110
                          00111 * 00020 PRINT I
 0145                     00112 L00020
 0145 FE 0601         [5] 00113          ldx    I
 0148 BD 041A         [9] 00114          jsr    PInt
                          00115
 014B BD 04BD         [9] 00116          jsr    NewLine
                          00117
                          00118 * 00030 NEXT I
 014E                     00119 L00030
 014E 86 06           [2] 00120          ldaa   #I>>8     ; Address the variable
 0150 C6 01           [2] 00121          ldab   #I&$FF
                          00122
 0152 BD 021D         [9] 00123          jsr    Next
                          00124
 0155                     00125 End_


Code:
.                         00348 ******************************************************************************
.                         00349 *
.                         00350 * Next - Process NEXT
.                         00351 *
.                         00352 * Input:
.                         00353 *       A:B = variable address
.                         00354 *
.021D                     00355 Next
.021D FE 0603         [5] 00356          ldx    ForTop    ; Point to current context
.0220 27 13 (0235)    [4] 00357          beq    Next2     ; Not within a FOR loop
.                         00358
.0222                     00359 Next0
.0222 A1 06           [5] 00360          cmpa   6,X       ; Compare variable address
.0224 26 08 (022E)    [4] 00361          bne    Next1     ; Loop variable mismatch
.0226 E1 07           [5] 00362          cmpb   7,X
.0228 26 04 (022E)    [4] 00363          bne    Next1
.                         00364
.022A EE 04           [6] 00365          ldx    4,X       ; Jump to top of loop
.022C 6E 00           [4] 00366          jmp    ,X
.                         00367
.022E                     00368 Next1
.022E EE 02           [6] 00369          ldx    2,X       ; Get previous context
.0230 FF 0603         [6] 00370          stx    ForTop    ; Save it
.0233 26 ED (0222)    [4] 00371          bne    Next0     ; Retry if valid
.                         00372
.0235                     00373 Next2
.0235 86 3E           [2] 00374          ldaa   #62       ; Report FOR-NEXT nesting error
.0237 7E 0276         [3] 00375          jmp    ErrH


Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 22, 2020 8:49 pm 
Offline
User avatar

Joined: Thu May 14, 2015 9:20 pm
Posts: 155
Location: UK
To answer the original question, it very much depends on how you define the behaviour and logic of the system.

One of the failings of BASIC is that although a lot of the core functionality is standard, not everything is. Hence you will get different results on different systems / variants.

Code:
100 goto 1500
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 10
1510 print i
1520 goto 900
1530 next i
1540 print 1540
2000 rem

BBC BASIC on an 8 bit Acorn (note all lowercase keywords entered as upper case):
1
2
3
4
5
6
7
8
9
10
910

Sinclair ZX Spectrum - same as above.


100 goto 1500
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 0
1510 print i
1520 goto 900
1530 next i
1540 print 1540
2000 rem

BBC BASIC on an 8 bit Acorn (note all lowercase keywords entered as upper case):

1
910

Sinclair ZX Spectrum:

1540



100 goto 1500
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 2
1510 print i
1520 goto 900
1530 next i
1540 print 1540
2000 rem

BBC BASIC on an 8 bit Acorn (note all lowercase keywords entered as upper case):

1
2
910

Sinclair ZX Spectrum - same as above.


100 goto 1500
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 0
1510 print i
1520 goto 1700
1530 next i
1540 print 1540
1550 goto 2000
1700 next i
1710 print 1710
2000 rem

BBC BASIC on an 8 bit Acorn (note all lowercase keywords entered as upper case):

1
1710

Sinclair ZX Spectrum:

1540



100 goto 1500
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 2
1510 print i
1520 goto 1700
1530 next i
1540 print 1540
1550 goto 2000
1700 next i
1710 print 1710
2000 rem

BBC BASIC on an 8 bit Acorn (note all lowercase keywords entered as upper case):

1
2
1710

Sinclair ZX Spectrum - same as above.



100 a = 0
200 gosub 1500
300 a = 1
400 gosub 1500
500 end
900 next i
910 print 910
920 goto 2000
1500 for i = 1 to 2
1510 print i
1520 if a = 0 then goto 900
1530 goto 1700
1540 next i
1550 print 1550
1560 goto 2000
1700 next i
1710 print 1710
2000 return

BBC BASIC on an 8 bit Acorn (note all lowercase keywords entered as upper case):

1
2
910
1
2
1710

Sinclair ZX Spectrum - same as above.


100 gosub 1000
200 gosub 2000
300 end
1000 for i = 1 to 2
1100 goto 3000
2000 for i = 7 to 8
2100 goto 3000
3000 print i
3100 next i
3200 return

BBC BASIC on an 8 bit Acorn (note all lowercase keywords entered as upper case):

1
2
7
8

Sinclair ZX Spectrum - same as above.



100 gosub 1000
200 gosub 2000
300 end
1000 for i = 1 to 2
1100 goto 3000
2000 for i = 7 to 6 step -1
2100 goto 3000
3000 print i
3100 next i
3200 return

BBC BASIC on an 8 bit Acorn (note all lowercase keywords entered as upper case):

1
2
7
6

Sinclair ZX Spectrum - same as above.



10 FOR I=0 TO 10
20 IF I = 6 THEN NEXT I
30 PRINT I,
40 IF I < 5 THEN NEXT I
50 PRINT "OK"
60 IF I > 7 THEN NEXT I : GOTO 90
70 NEXT I
80 PRINT "LAST?"
90 PRINT "END"

BBC BASIC on an 8 bit Acorn:

         0
         1
         2
         3
         4
         5
OK
         7
OK
         8
OK
         9
OK
         10
OK
END

Sinclair ZX Spectrum:

0               1
2               3
4               5
OK
7               OK
8               OK
9               OK
10              OK
END



10 FOR I=1 TO 3
20 FOR J=1 TO 3
30 PRINT I,J
40 NEXT J
50 NEXT I

BBC BASIC on an 8 bit Acorn:

         1         1
         1         2
         1         3
         2         1
         2         2
         2         3
         3         1
         3         2
         3         3

Sinclair ZX Spectrum - same as above.



10 FOR I=1 TO 3
20 FOR J=1 TO 3
30 PRINT I,J
40 NEXT I
50 NEXT J

BBC BASIC on an 8 bit Acorn:

         1         1
         1         2
         1         3

No FOR at line 50

Sinclair ZX Spectrum:

1               1
2               1
3               1
4               2
5               3





Mark


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 23, 2020 12:45 am 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 704
Location: North Tejas
Thanks for running the tests and posting the results. That looks like a lot of work.

1024MAK wrote:
Code:
10 FOR I=1 TO 3
20 FOR J=1 TO 3
30 PRINT I,J
40 NEXT I
50 NEXT J


Sinclair ZX Spectrum:

1 1
2 1
3 1
4 2
5 3



That result is totally unexpected - overlapping loops.


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 23, 2020 5:58 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
BillG wrote:
That result is totally unexpected - overlapping loops.

Yeah, it seems like the activation record for I is surviving past its "intended" scope. It begs the question of how these activation records are allocated and maintained by the ZX interpreter ... is the internal mechanism even a stack? Somehow it doesn't seem likely ... maybe the allocation occurs in the same space with arrays? I would try to check, but Z-80 assembly language makes my eyes cross after a few minutes.

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 23, 2020 1:00 pm 
Offline
User avatar

Joined: Thu May 14, 2015 9:20 pm
Posts: 155
Location: UK
The strange operation of incorrectly nested FOR - NEXT loops is a known oddity of the ZX Spectrum BASIC.

If you look at how a FOR - NEXT loop operates, you will see that unlike most other BASIC versions, if the interpreter decides that the values on the FOR statement aren’t valid for a count cycle, execution will skip forward to after the corresponding NEXT command. That’s why I choose it as one of the two eight bit systems to use as an example.

The relevant section of the manual is here https://worldofspectrum.org/ZXBasicManual/zxmanchap4.html also the way that the variables are stored is detailed here https://worldofspectrum.org/ZXBasicManual/zxmanchap24.html

The relevant section of a disassembly is on PDF page 100 (book page 95) here http://www.primrosebank.net/computers/zxspectrum/docs/CompleteSpectrumROMDisassemblyThe.pdf

There is a more recent disassembly here https://www.tablix.org/~avian/spectrum/rom/zx82.htm

The FOR code is at address 1D03

Read on for the ‘NEXT’ statement code.

Some chat about the Sinclair implication https://worldofspectrum.org/forums/discussion/39970/for-1-to-0-execute-no-times

https://worldofspectrum.org/forums/discussion/57284/for-loop-without-next-instruction-how-come

Mark


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 23, 2020 5:23 pm 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 704
Location: North Tejas
I was wondering why my post disappeared...I put it in the wrong thread. So here it is again...

OK, the integration is done and the compiler can handle FOR loops now, even the monstrosity in the following program:

Code:
100 n = 1
110 m = 3
120 for i = n+1 to m+1 step m-2
130 print i
140 next i


The FOR statement compiles into:

Code:
                          00088 * 120 for i = n+1 to m+1 step m-2
 0125                     00089 L00120
                          00090          ifdef  __TRACE
                          00091          ldx    #120
                          00092          jsr    Trace
                          00093          endif
                          00094          ifdef  __ATLIN
 0125 CE 0125         [3] 00095          ldx    #L00120
 0128 FF 0663         [6] 00096          stx    ResLn_
                          00097          endif
                          00098
 012B BD 0296         [9] 00099          jsr    ForEnter
                          00100
 012E 86 01           [2] 00101          ldaa   #T00000>>8
 0130 A7 04           [6] 00102          staa   4,X
 0132 86 49           [2] 00103          ldaa   #T00000&$FF
 0134 A7 05           [6] 00104          staa   5,X
                          00105
 0136 86 06           [2] 00106          ldaa   #I_>>8
 0138 A7 06           [6] 00107          staa   6,X
 013A 86 75           [2] 00108          ldaa   #I_&$FF
 013C A7 07           [6] 00109          staa   7,X
                          00110
 013E C6 01           [2] 00111          ldab   #1
 0140 4F              [2] 00112          clra
 0141 FB 0672         [4] 00113          addb   N_+1
 0144 B9 0671         [4] 00114          adca   N_
                          00115
 0147 20 46 (018F)    [4] 00116          bra    T00003
                          00117
 0149                     00118 T00000
 0149 C6 01           [2] 00119          ldab   #1
 014B 4F              [2] 00120          clra
 014C FB 0674         [4] 00121          addb   M_+1
 014F B9 0673         [4] 00122          adca   M_
 0152 F7 066E         [5] 00123          stab   ITp00_+1
 0155 B7 066D         [5] 00124          staa   ITp00_
                          00125
 0158 F6 0674         [4] 00126          ldab   M_+1
 015B B6 0673         [4] 00127          ldaa   M_
 015E C0 02           [2] 00128          subb   #2
 0160 82 00           [2] 00129          sbca   #0
 0162 B7 066F         [5] 00130          staa   ITp01_
                          00131
 0165 FB 0676         [4] 00132          addb   I_+1
 0168 B9 0675         [4] 00133          adca   I_
                          00134
 016B 7D 066F         [6] 00135          tst    ITp01_
 016E 2B 0E (017E)    [4] 00136          bmi    T00004
                          00137
 0170 B1 066D         [4] 00138          cmpa   ITp00_
 0173 22 15 (018A)    [4] 00139          bhi    T00001
 0175 25 16 (018D)    [4] 00140          blo    T00002
 0177 F1 066E         [4] 00141          cmpb   ITp00_+1
 017A 23 11 (018D)    [4] 00142          bls    T00002
 017C 20 0C (018A)    [4] 00143          bra    T00001
                          00144
 017E                     00145 T00004
 017E B1 066D         [4] 00146          cmpa   ITp00_
 0181 25 07 (018A)    [4] 00147          blo    T00001
 0183 22 08 (018D)    [4] 00148          bhi    T00002
 0185 F1 066E         [4] 00149          cmpb   ITp00_+1
 0188 24 03 (018D)    [4] 00150          bhs    T00002
                          00151
 018A                     00152 T00001
 018A 7E 02D3         [3] 00153          jmp    ForExit
                          00154
 018D                     00155 T00002
 018D 31              [4] 00156          ins
 018E 31              [4] 00157          ins
                          00158
 018F                     00159 T00003
 018F B7 0675         [5] 00160          staa   I_
 0192 F7 0676         [5] 00161          stab   I_+1


Top
 Profile  
Reply with quote  
PostPosted: Sun Jul 26, 2020 1:43 pm 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 704
Location: North Tejas
Does anybody see anything noteworthy in the following code?

Code:
                          00075 ; 100 for i=1 to 3
 0B12                     00076 L00100:
                          00077          ifdef  __TRACE
                          00078          ldx    #<100
                          00079          lda    #>100
                          00080          jsr    Trace
                          00081          endif
                          00082          ifdef  __ATLIN
 0B12 A2 12           [2] 00083          ldx    #<L00100
 0B14 8E 11E4         [4] 00084          stx    ResLn_
 0B17 A2 0B           [2] 00085          ldx    #>L00100
 0B19 8E 11E5         [4] 00086          stx    ResLn_+1
                          00087          endif
                          00088
 0B1C 20 0CCC         [6] 00089          jsr    ForEnter
                          00090
 0B1F A0 04           [2] 00091          ldy    #4
 0B21 A9 3B           [2] 00092          lda    #<T00000
 0B23 91 1C           [6] 00093          sta    (Ptr0),Y
 0B25 C8              [2] 00094          iny
 0B26 A9 0B           [2] 00095          lda    #>T00000
 0B28 91 1C           [6] 00096          sta    (Ptr0),Y
                          00097
 0B2A C8              [2] 00098          iny
 0B2B A9 EE           [2] 00099          lda    #<I_
 0B2D 91 1C           [6] 00100          sta    (Ptr0),Y
 0B2F C8              [2] 00101          iny
 0B30 A9 11           [2] 00102          lda    #>I_
 0B32 91 1C           [6] 00103          sta    (Ptr0),Y
                          00104
 0B34 A2 01           [2] 00105          ldx    #<1
 0B36 A0 00           [2] 00106          ldy    #>1
                          00107
 0B38 4C 0B59         [3] 00108          jmp    T00003
                          00109
 0B3B                     00110 T00000:
 0B3B A9 01           [2] 00111          lda    #<1
 0B3D 18              [2] 00112          clc
 0B3E 6D 11EE         [4] 00113          adc    I_
 0B41 AA              [2] 00114          tax
 0B42 A9 00           [2] 00115          lda    #>1
 0B44 6D 11EF         [4] 00116          adc    I_+1
                          00117
 0B47 C9 00           [2] 00118          cmp    #>3
 0B49 90 0B (0B56)  [2/3] 00119          blo    T00002
 0B4B D0 06 (0B53)  [2/3] 00120          bne    T00001
                          00121
 0B4D E0 03           [2] 00122          cpx    #<3
 0B4F 90 05 (0B56)  [2/3] 00123          blo    T00002
 0B51 F0 03 (0B56)  [2/3] 00124          beq    T00002
                          00125
 0B53                     00126 T00001:
 0B53 4C 0D26         [3] 00127          jmp    ForExit
                          00128
 0B56                     00129 T00002:
 0B56 A8              [2] 00130          tay
 0B57 68              [4] 00131          pla
 0B58 68              [4] 00132          pla
                          00133
 0B59                     00134 T00003:
 0B59 8E 11EE         [4] 00135          stx    I_
 0B5C 8C 11EF         [4] 00136          sty    I_+1
                          00137
                          00138 ; 110 print i
 0B5F                     00139 L00110:
                          00140          ifdef  __TRACE
                          00141          ldx    #<110
                          00142          lda    #>110
                          00143          jsr    Trace
                          00144          endif
                          00145          ifdef  __ATLIN
 0B5F A2 5F           [2] 00146          ldx    #<L00110
 0B61 8E 11E4         [4] 00147          stx    ResLn_
 0B64 A2 0B           [2] 00148          ldx    #>L00110
 0B66 8E 11E5         [4] 00149          stx    ResLn_+1
                          00150          endif
                          00151
 0B69 AE 11EE         [4] 00152          ldx    I_
 0B6C AD 11EF         [4] 00153          lda    I_+1
 0B6F 20 0F5A         [6] 00154          jsr    PInt
                          00155
 0B72 20 1067         [6] 00156          jsr    NewLine
                          00157
                          00158 ; 120 next i
 0B75                     00159 L00120:
                          00160          ifdef  __TRACE
                          00161          ldx    #<120
                          00162          lda    #>120
                          00163          jsr    Trace
                          00164          endif
                          00165          ifdef  __ATLIN
 0B75 A2 75           [2] 00166          ldx    #<L00120
 0B77 8E 11E4         [4] 00167          stx    ResLn_
 0B7A A2 0B           [2] 00168          ldx    #>L00120
 0B7C 8E 11E5         [4] 00169          stx    ResLn_+1
                          00170          endif
                          00171
 0B7F A2 EE           [2] 00172          ldx    #<I_
 0B81 A9 11           [2] 00173          lda    #>I_
 0B83 20 0C80         [6] 00174          jsr    ForNext
                          00175
 0B86                     00176 End_:


Top
 Profile  
Reply with quote  
PostPosted: Sun Jul 26, 2020 8:28 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
BillG wrote:
Does anybody see anything noteworthy in the following code?

At first glance, the code between T00000 and T00001 seems unexpectedly much more efficient in the 6502 version compared to the 6800 version, but I might be missing some important detail that would help to explain why.

[Edit: ah, the FOR loop in the 6502 example is much simpler.]

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Top
 Profile  
Reply with quote  
PostPosted: Sun Jul 26, 2020 9:14 pm 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 704
Location: North Tejas
barrym95838 wrote:
BillG wrote:
Does anybody see anything noteworthy in the following code?

the 6502 version


Ding! Ding! Ding! We have a winner!

Bonus points if someone said the comparison code does not work for negative numbers. I am still working on that problem and it is not simple. I cannot compile the complicated loop used in the 6800 example yet and the 6800 version does not properly handle negative numbers either. The 6800 version should be much easier to fix due to the available signed conditional branch instructions.

I wanted to see how easy it was to retarget for the 6502. Not very easy but not very hard either. It is currently about half done.

Code for the 6809 should be much better not having to deal with everything in 8-bit quantities, but that is much later.


Top
 Profile  
Reply with quote  
PostPosted: Sun Jul 26, 2020 9:33 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
I thought about mentioning the unsigned thing, but missed the chance. You might want to give Bruce's article a chance to sink into your brain.

P.S. I think there might be a couple of typos in the test code at the bottom of Bruce's page ... those final two TSX instructions should be TXS, unless I'm horribly mistaken.

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Last edited by barrym95838 on Sun Jul 26, 2020 9:52 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 41 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 15 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: