![]() |
Forum Index : Microcontroller and PC projects : Most elegant MMBasic way to ......
![]() ![]() |
|||||
Author | Message | ||||
Tinine Guru ![]() Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
Yes, here is an example of my quadrature decoder and counter. Unfortunately, I don't have the ASM output but it reads pretty much the same, except for IFs and GOTOs being replaced with some kind of cmp/jmp. The label and variable names are preserved. The only oddity in PropBasic is that when testing more than one condition, they need to be on separate lines. DEVICE P8X32A, XTAL1, PLL16X FREQ 80_000_000 Chan_A PIN 0 INPUT Chan_B PIN 1 INPUT Chan_Z PIN 2 INPUT Counter VAR LONG = 0 Idx_Monitor VAR LONG = 0 Idx_Count VAR LONG = 0 Count_Error VAR LONG = 0 PROGRAM Start Start: 'Decide where to start If Chan_A = 1 And Chan_B = 1 Then Goto A1B1 Endif If Chan_A = 0 And Chan_B = 0 Then Goto A0B0 Endif If Chan_A = 1 And Chan_B = 0 Then Goto A1B0 Endif If Chan_A = 0 And Chan_B = 1 Then Goto A0B1 Endif 'Should keep hopping between these labels unless an illegal 'condition crops up. A1B1: If Chan_A = 0 Then Dec Counter Goto A0B1 Elseif Chan_B = 0 Then Inc Counter Goto A1B0 Endif Goto A1B1 A0B0: If Chan_A = 1 Then Dec Counter Goto A1B0 Elseif Chan_B = 1 Then Inc Counter Goto A0B1 Endif Goto A0B0 A1B0: If Chan_A = 0 Then Inc Counter Goto A0B0 Elseif Chan_B = 1 Then Dec Counter Goto A1B1 Endif Goto A1B0 A0B1: If Chan_A = 1 Then Inc Counter Goto A1B1 Elseif Chan_B = 0 Then Dec Counter Goto A0B0 Endif Goto A0B1 End Edited 2020-08-19 02:30 by Tinine |
||||
capsikin Guru ![]() Joined: 30/06/2020 Location: AustraliaPosts: 341 |
I modified the program for a CMM2 to try and do some comparisons, but it seems like the speed depends on how many lines come before the code (at least for goto). So I modified it more to show that. I thought I saw someone talking about some basics searching through the program lines to find the jump target, maybe it's doing that. It took about 1000 ms with the GOTO and a bunch of statement lines before the code, faster without the goto or without the extra lines. (maybe reduce the number of loops on a slower processor) Dim Integer n,t,tt print:print tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 'tt=0 'tt=0 'tt=0 'tt=0 'tt=0 'some redundant lines to test if it slows things down loater. timer=0 for n=1 to 100000 'comment out goto to just test the for loop goto 10 10 Next t=timer ?t, ?"with just for loop or with numbers GoTo":? print "if goto command is used, speed depends on how many statement lines I add before it." print "if goto command is not used, speed stays the same (for loop not affected by earlier lines)" print "goto not affected by any lines after, or comment lines before." end tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 tt=0 |
||||
capsikin Guru ![]() Joined: 30/06/2020 Location: AustraliaPosts: 341 |
I kind of agree, and for a two line loop like 10 PRINT "Hello World!" 20 GOTO 10 I don't think it's unclear at all, but for larger structures, even just 4 or 5 lines, the indentation conventions make standard structures easier to read. (in general - not saying they work well for everything). That said the indentation conventions don't highlight an EXIT DO or EXIT SUB like they do with the start and end of the DO or SUB structure - I wonder if there's a good way to do this. And you can indent any GOTO code if you can work out a clear way to do it (loops might not be too hard, but you don't need GOTO for making loops). Also if you use labels rather than line numbers, GOTO doesn't show you if you're jumping forward or backwards. Edited 2020-08-19 12:51 by capsikin |
||||
hitsware2![]() Guru ![]() Joined: 03/08/2019 Location: United StatesPosts: 719 |
> Also if you use labels rather than line numbers, > GOTO doesn't show you if you're jumping forward > or backwards. FWIW : Having line numbers doesn't mean they are sequential . my site |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
Here's another one to scratch the efficiency pedant itch for you then: > Clear Dim Integer a,t > timer=0:for a=1 to 1000000:next a:t=timer:print t 30168 > timer=0:for a=1 to 1000000:next:t=timer:print t 23828 > Drop the variable off the Next and shave some mS. The last loop executes in only 79% of the time (aatch, that is quite a bit!) Like you said, Makes no real difference but, Ooh, yeah! feels good! ![]() Edited 2020-08-21 03:03 by CaptainBoing |
||||
Poppy![]() Guru ![]() Joined: 25/07/2019 Location: GermanyPosts: 486 |
Isnīt this actually all about BASIC? ![]() ![]() ![]() | ||||
![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |