PDA

View Full Version : Line numbers and GOTO



Charles Pegge
15-04-2009, 18:14
Eros may wish to avert his gaze. :roll:

http://community.thinbasic.com/index.php?topic=2517

LineNumbers1.tbasic



'LINE NUMBERS
'

uses "oxygen","file"
dim src as string

src = "
10 rem legacy
20 dim a,b,c,i
30 a=1 : b=1
40 c=a+b
60: i=i+1
70: if i<20 then a=b : b=c : goto 40 '
80:
80: print `FIBONACCI NUMBER: `+str$(c/b)
"




o2_basic src

'msgbox 0, o2_view "o2h "+src

if len(o2_error) then
msgbox 0, o2_error : stop
end if

o2_exec

'msgbox 0,o2_error

Charles Pegge
15-04-2009, 18:28
This is a more palatable use of line numbers:



'
'LINE NUMBERS: CONTEMPORARY STYLE
'

uses "oxygen","file"
dim src as string

src = "

10:

dim a=1, b=1, c=0, i=0

20:

c=a+b
inc i
if i<20 then a=b : b=c : goto 20

30:

print `FIBONACCI NUMBER: ` str c/b

40:

"

o2_basic src

ErosOlmi
15-04-2009, 18:40
Danger, danger ! :skull:




:diablo:
:D

kryton9
16-04-2009, 01:35
The first version looked fine to me and the code is tighter. Never saw a version like the second... it might grow on me so I won't be quick to judge.

Well the jmp command was just like a goto but with a label involved instead of line number in terms of a feature. I assume jmp is more efficient and is
not hated as much as goto?

I like line numbers as you know exactly where you are in the code. It is a feature I requested for thinAir and I know it will come in the future some time.
Without line numbers you have to always sort of analyze the code and it takes a few milliseconds to a couple of seconds depending on the code to know where you are when you scroll.

So thanks for adding them to the module!

Charles Pegge
16-04-2009, 11:30
GOTO translates to Assembly code jmp long in this instance. But Line numbers have to be turned into non-numbers internally. so all line numbers are prefixed with underscores _

To use JMP with line numbers it would be: jmp long _100 etc


When oxygegen reports an error it does not give the physical line number - instead it gives the last label encountered. Line numbers can be used as trace markers for this purpose - then removed in the final code.

Michael Clease
16-04-2009, 18:22
on the amiga I used to push my return address on the stack and do a JMP to a subroutine because it was quicker than a jsr would this still apply with 8086+ cpus?

Charles Pegge
16-04-2009, 20:19
Push return address and Jump is the operational equivalent of call - The slow parts are storing the return address to stack memory then refilling the instruction pipeline with code from a new memory location.

On today's 8086's :) Memory access time is the limiting factor.

What processor did your Amiga have? And was it doing any housekeeping prior to the call, which you might have been able to bypass?

Michael Clease
16-04-2009, 22:36
The Amiga used the 68000 range of CPUs and I cant remember what the code looked like its been about 10 years at least :unguee:



LEA returnaddress, (sp)+
JMP mysub

Timings

http://linas.org/mirrors/www.nvg.ntnu.no/2002.09.16/amiga/MC680x0_Sections/mc68000timing.HTML

Michael Hartlef
17-04-2009, 08:24
The Amiga used the 68000 range of CPUs and I cant remember what the code looked like its been about 10 years at least :unguee:



LEA returnaddress, (sp)+
JMP mysub

Timings

http://linas.org/mirrors/www.nvg.ntnu.no/2002.09.16/amiga/MC680x0_Sections/mc68000timing.HTML


Yes, that were the days. :P

John Spikowski
18-04-2009, 11:52
The first version looked fine to me and the code is tighter. Never saw a version like the second... it might grow on me so I won't be quick to judge.

In ScriptBasic, line numbers are labels and optional for compatibility. The numbering doesn't have to be in any order or used on every line.



100 FOR x = 1 TO 10
PRINT x
GOSUB 50
NEXT x

END

50 PRINTNL
RETURN

Charles Pegge
18-04-2009, 14:48
Yes the numbers have no numeric significance. They are simply labels so 100 would be different from 100.0. I see them as useful for rough drafts and debugging, because numbers are easier to think of then formal labels when you want to get something down quickly and test it.

Charles

John Spikowski
18-04-2009, 21:19
Charles,

I'm pretty sure that in ScriptBasic, line number labels must be positive integer.

GOTO 1000000

I have tested the above and it works fine. The line must start with a number and a space to be considered a line number label. Anything else must have a trailing : to define it as a label and must be standalone on the line with no statements following.

A100:


John

Petr Schreiber
18-04-2009, 21:26
John,

Charles is breaking limits for compilers, his Oxygen allows code to be compiled at runtime from code... so when he decides to make line number to look like PI number, I am sure he will make it work. When somebody borns as magician... :)


Petr

John Spikowski
18-04-2009, 23:45
When somebody borns as magician...

The BaCon (http://www.allbasic.info/wiki/index.php/BC:Main_Page) open source Basic to C project written in Kornshell is another example of magicians at work. :eusaclap:

Michael Clease
18-04-2009, 23:59
When somebody borns as magician...

The BaCon (http://www.allbasic.info/wiki/index.php/BC:Main_Page) open source Basic to C project written in Kornshell is another example of magicians at work. :eusaclap:


and heres a windows version http://www.bcxgurus.com/


BCX was programmed entirely using BCX BASIC, making it a self-translating translator. And translating is FAST! BCX translates its own source code, over 24,500 lines of code, in just 1.22 seconds on a P4 laptop running XP Pro.

ErosOlmi
19-04-2009, 00:34
Nothing magic.
In all those project what is called "magic" is just: passion, curiosity, time and knowledge put in practice

:diablo:

John Spikowski
19-04-2009, 08:07
http://files.allbasic.info/thinBASIC/wizard.jpg

I still believe in magic. :)

ErosOlmi
19-04-2009, 08:59
I've already seen that bad guy but I do not remember where and when ...

Michael Hartlef
19-04-2009, 09:59
and heres a windows version http://www.bcxgurus.com/


BCX was programmed entirely using BCX BASIC, making it a self-translating translator. And translating is FAST! BCX translates its own source code, over 24,500 lines of code, in just 1.22 seconds on a P4 laptop running XP Pro.


:offtopic: (btw. why is it such a mad looking fellow?)
Such a statement is only true for later versions as at the start you allways have to use a different tool that can produce executable machine code somehow.

Charles Pegge
19-04-2009, 10:44
I'm impressed by the length of the beard. I have only seen one gentleman in town with a beard anywhere near that size. He must have been a wizard too.

O2 is currently written in FreeBasic and I plan to self compile it as soon as all the necessary functions are in place. Currently it is about 15500 lines of code. But I should be able make it 15-20% shorter. (Had virtually no problems coding in FB - its very reliable)

And to link back to the topic. O2 does not use line numbers but has an abundance of GOTOs as befits a production line :D