Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 05:12 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Most elegant MMBasic way to ......

     Page 2 of 2    
Author Message
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 04:17pm 18 Aug 2020
Copy link to clipboard 
Print this post

  CaptainBoing said  interesting. So in the "translated" code, are GOTO lines actual memory addresses?



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: Australia
Posts: 341
Posted: 02:11am 19 Aug 2020
Copy link to clipboard 
Print this post

  CaptainBoing said  a little code. Times in mS (natch)

I tried to avoid timing anything but the switch and made the workload as identical as I could. Select is around twice as fast as a On x GoSub but it's all relative and tweakable



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: Australia
Posts: 341
Posted: 02:50am 19 Aug 2020
Copy link to clipboard 
Print this post

  Tinine said  Yes! The moral of the story is don't write spaghetti code!

Don't blame GOTO.


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 States
Posts: 719
Posted: 04:14pm 19 Aug 2020
Copy link to clipboard 
Print this post

> 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 Kingdom
Posts: 2170
Posted: 04:03pm 20 Aug 2020
Copy link to clipboard 
Print this post

  Tinine said  
Makes no real difference, actually but it just makes me feel better to know that I have greater efficiency.  


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: Germany
Posts: 486
Posted: 07:40pm 20 Aug 2020
Copy link to clipboard 
Print this post

  CaptainBoing said  Ooh, yeah! feels good!


Isnīt this actually all about BASIC?

Andre ... such a GURU?
 
     Page 2 of 2    
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025