Heater
Posts: 13366
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 12:21 pm

RichardRussell,
My understanding is that SWIs are kernel calls in Linux and therefore not usable from user-mode code; is that not right?
To make kernel calls on x86 one uses int 80h or sysenter instructions, on x86-64 use the syscall instruction. I have no idea what the equivalent ARM instructions are. No C library required.

As for if it is usable from user space that depends on permissions, as you would expect. Nothing stops a user from using kernel calls directly for doing things he has permission to do.

Here is a hello world program for Linux on x86-64 in assembler:

Code: Select all

$ cat hello.asm
; ----------------------------------------------------------------------------------------
; Writes "Hello, World" to the console using only system calls. Runs on 64-bit Linux only.
; To assemble and run:
;
;     nasm -felf64 hello.asm && ld hello.o && ./a.out
; ----------------------------------------------------------------------------------------

          global    _start

          section   .text
_start:   mov       rax, 1                  ; system call for write
          mov       rdi, 1                  ; file handle 1 is stdout
          mov       rsi, message            ; address of string to output
          mov       rdx, 13                 ; number of bytes
          syscall                           ; invoke operating system to do the write
          mov       rax, 60                 ; system call for exit
          xor       rdi, rdi                ; exit code 0
          syscall                           ; invoke operating system to exit

          section   .data
message:  db        "Hello, World", 10      ; note the newline at the end

$ nasm -felf64 hello.asm && ld hello.o && ./a.out
Hello, World
$
I'm sure an ARM guru can translate that to ARM for Raspbian easily enough.
Memory in C++ is a leaky abstraction .

User avatar
RichardRussell
Posts: 579
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 12:47 pm

Heater wrote:
Sat Jul 13, 2019 12:21 pm
Nothing stops a user from using kernel calls directly for doing things he has permission to do.
OK, but why would one choose to write out the text using a kernel call rather than, say, printf in the CRT? printf is much more portable: it works on every OS that BBC BASIC for SDL 2.0 will run on I think (including Windows) and as cross-platform compatibility is the keystone of BBCSDL I would always prefer the portable solution to something platform-specific.

Heater
Posts: 13366
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 1:05 pm

RichardRussell,
...but why would one choose to write out the text using a kernel call rather than, say, printf....
No idea. You will have to ask DavidS about that. I'm sure he will explain how it would be smaller, faster and more portable...

Personally I think you are doing the right thing, for all the obvious reasons.

I did once use Linux system calls but that was for my own humble compiler attempt that generated code for x86. But only for the silly reason that I did not want to have a dependency on another languages library for such basic things.
Memory in C++ is a leaky abstraction .

User avatar
RichardRussell
Posts: 579
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 1:17 pm

Heater wrote:
Sat Jul 13, 2019 1:05 pm
No idea. You will have to ask DavidS about that. I'm sure he will explain how it would be smaller, faster and more portable...
He'll probably say that printf is not available under RISC OS (and as a user of that OS, portability isn't a major concern)! I'll continue to discourage, and not provide specific support for, making kernel calls from BBC BASIC.

jahboater
Posts: 4690
Joined: Wed Feb 04, 2015 6:38 pm

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 3:53 pm

RichardRussell wrote:
Sat Jul 13, 2019 12:47 pm
Heater wrote:
Sat Jul 13, 2019 12:21 pm
Nothing stops a user from using kernel calls directly for doing things he has permission to do.
OK, but why would one choose to write out the text using a kernel call rather than, say, printf in the CRT? printf is much more portable: it works on every OS that BBC BASIC for SDL 2.0 will run on I think (including Windows) and as cross-platform compatibility is the keystone of BBCSDL I would always prefer the portable solution to something platform-specific.
On ARM it is the SVC instruction (SuperVisor Call) it used to be called SWI (SoftWare Interrupt) but ARM changed the name probably at the same time they introduced UAL a decade or so ago.

printf is a bad example because it is very complicated and the library adds much. You (likely) could not, and would not, replace that with assembly code. On the other hand many of the library functions such as read(), write() etc etc are just thin wrappers around the system call. The ABI calling sequence and the syscall calling sequence are deliberately similar so that these wrapper function do little (the registers are the same).

Here is a simple exit function in C that guarantees to exit the program immediately regardless.

Code: Select all

/*
 *  Fast exit.
 */
static inline noreturn
leave( const int rc )
{
#if x64 && LINUX
  asm( "syscall" :: "a" (SYS_exit), "D" (rc) );
#elif A64 && LINUX
  register int w0 reg("w0") = rc,
               w8 reg("w8") = SYS_exit;
  asm( "svc 0" :: "r" (w0), "r" (w8) );
#else
  _exit(rc);
#endif
  notreached;
}

jahboater
Posts: 4690
Joined: Wed Feb 04, 2015 6:38 pm

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 4:05 pm

Heater wrote:
Sat Jul 13, 2019 12:21 pm
I'm sure an ARM guru can translate that to ARM for Raspbian easily enough.
:) :) You could change it into x86_64 easily too by replacing all the instructions with 32-bit ones. :) :)
Makes it rather smaller.
:)

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 4:07 pm

@RichardRussell:
You need to be able to use the libraries before you can call a function in them, so you need to link them in at run time, with a call to the Linux Kernel.

Compilers usually take care of that kind of thing for you.
Also jahboater just mentioned another place where calling SWI is needed.

The only question is does the assembler in your BBC BASIC recognize the SWI instruction?
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

User avatar
RichardRussell
Posts: 579
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 4:27 pm

DavidS wrote:
Sat Jul 13, 2019 4:07 pm
You need to be able to use the libraries before you can call a function in them, so you need to link them in at run time
In the case of the C Run Time it's not an issue, because the ARM edition of BBC BASIC for SDL 2.0 is written in C! Indeed it's precisely for this reason that calling CRT functions from BASIC (or inline assembler) code is the best way, because it guarantees portability: if the CRT wasn't available BBC BASIC could not run! Calling Linux kernel functions won't work at all in Windows, and I'm not sure it will work in MacOS, iOS or Android either.
Also jahboater just mentioned another place where calling SWI is needed.
Needed? There are no circumstances when calling the kernel from BBC BASIC is 'needed' (you certainly should never do a forced exit without closing down SDL2 cleanly).
The only question is does the assembler in your BBC BASIC recognize the SWI instruction?
I don't know. If you're trying to write 'generic' BBC BASIC code that will run in BBC BASIC for SDL 2.0 you mustn't use it, for the aforementioned reasons.

User avatar
RichardRussell
Posts: 579
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 5:38 pm

DavidS wrote:
Sat Jul 13, 2019 4:07 pm
The only question is does the assembler in your BBC BASIC recognize the SWI instruction?
I've checked, it does (but only with a numeric parameter of course):

Code: Select all

0043184C EF000003             swi 3

User avatar
John_Spikowski
Posts: 1396
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 7:17 pm

In an early version of the ScriptBasic SDL_gfx extension module development I added a library to emulate console support.

You would probably hear less complaining about being stuck in your IDE if you gave your users a console like environment to work in.

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 7:56 pm

RichardRussell wrote:
Sat Jul 13, 2019 5:38 pm
DavidS wrote:
Sat Jul 13, 2019 4:07 pm
The only question is does the assembler in your BBC BASIC recognize the SWI instruction?
I've checked, it does (but only with a numeric parameter of course):

Code: Select all

0043184C EF000003             swi 3
Thank you for that. I am writing code that is specific to the target Operating Systems, specifically one version for RISC OS, and another for ARMHF Linux.

Android runs ontop of a Linux system.

Every Linux program is making some SWI calls, even if it does not know it, it is how the system works.

I noticed in the hello world code earlier that it looks like x86 Linux changed its software inturrupt, it used to be on INT 80h.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

User avatar
RichardRussell
Posts: 579
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 8:19 pm

ScriptBasic wrote:
Sat Jul 13, 2019 7:17 pm
You would probably hear less complaining about being stuck in your IDE if you gave your users a console like environment to work in.
I'm not sure to whom that comment was directed, but none of my users has ever complained about being 'stuck' in the IDE (why "stuck"?). Most of them are past (or current) BBC BASIC for Windows users anyway so they are very familiar with the IDE, and many have probably never typed at a command prompt. Even I most commonly start Raspbian BBC BASIC using the desktop icon, and don't open a command window.

Heater
Posts: 13366
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 8:29 pm

RichardRussell,
I'm not sure to whom that comment was directed...
Why that that would be me. When I discovered that one cannot run the thing and get a result solely from the command line.

You need not worry about it of course. I am only one.
Memory in C++ is a leaky abstraction .

User avatar
John_Spikowski
Posts: 1396
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 8:51 pm

For me that would be like being stuck in UltraEdit and the output window and its JavaScript embedded scripting engine is my BASIC environment.

Heater
Posts: 13366
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 9:30 pm

ScriptBasic,
For me that would be like being stuck in UltraEdit and the output window and its JavaScript embedded scripting engine is my BASIC environment.
You should upgrade Microsoft's Visual Studio Code. All that JS scripting loveliness, cross-platform, runs on the PI and save 50 bucks per year (or whatever)
Memory in C++ is a leaky abstraction .

User avatar
RichardRussell
Posts: 579
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Sat Jul 13, 2019 10:07 pm

ScriptBasic wrote:
Sat Jul 13, 2019 8:51 pm
For me that would be like being stuck in UltraEdit and the output window and its JavaScript embedded scripting engine is my BASIC environment.
SDL was conceived as a cross-platform games engine and is still graphics-dominated, so it's hardly surprising that BBC BASIC for SDL 2.0 is first and foremost a GUI language and that its principal user interface is expected to be one or other of the two supplied IDEs. There are plenty of console BASICs (including versions of BBC BASIC) so it's not a market I feel a need to compete in.

User avatar
John_Spikowski
Posts: 1396
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Introduction to BBC BASIC

Sun Jul 14, 2019 12:44 am

BBC BASIC seems more like Paul Dunning's SpecBasic. He seems more hung up on BASIC needs line numbers.

SDL_terminal

User avatar
RichardRussell
Posts: 579
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Sun Jul 14, 2019 9:21 am

ScriptBasic wrote:
Sun Jul 14, 2019 12:44 am
BBC BASIC seems more like Paul Dunning's SpecBasic.
There are similarities, in that both are closely based on the BASIC that was supplied with home computers of the early 1980s (the BBC Micro in the case of BBC BASIC and the ZX Spectrum in the case of SpecBAS). Both have also been greatly extended in their capabilities since those early 8-bit versions,

The main difference probably lies in the fact that, from very early on, BBC BASIC was ported to other platforms (e.g. the Cambridge Z88, Amstrad NC100, CP/M, MS-DOS, etc.) most of which could not closely emulate the BBC Micro in respect of graphics etc. Hence the 'language' acquired a life of its own, separate from being an emulation of the BBC Micro, whereas (I think) SpecBAS has retained a high degree of 'Spectrum emulation' throughout.

BBC BASIC has always been more of a 'general purpose' language, and when it came to porting it to Windows in 2001 and latterly to MacOS, Linux and the rest, the ability to access the native capabilities of the host machine (native graphics including 3D, calling routines in shared libraries, interfacing with the network and other forms of I/O etc.) was a key requirement, whilst I think SpecBAS provides more of a 'sandboxed' environment.

I have no fundamental objection to supporting console applications: BBC BASIC for Windows does (there's a checkbox for 'Console mode' in the Compile dialog) but that has not been a high priority for BBC BASIC for SDL 2.0 especially given the availability of other versions of BBC BASIC such as Brandy.

User avatar
John_Spikowski
Posts: 1396
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Introduction to BBC BASIC

Sun Jul 14, 2019 6:38 pm

Too bad the SciptBaisc Brandy extension module got a bad rap. It could have provided the console environment BBC BASIC lacks.

ZXDunny
Posts: 117
Joined: Sun Jul 08, 2012 7:57 pm

Re: Introduction to BBC BASIC

Sun Jul 14, 2019 6:49 pm

Richard said it best - SpecBAS (by me, Paul Dunn - no idea how this Dunning chap is) is as tightly tied to Sinclair BASIC as I can make it.

That means line numbers, string slicing Sinclair-style, UDG character sets and even using the same algorithms to generate graphics primitives as the original z80 version. The goal was to fulfil a childhood dream of mine - back the day I loved BASIC but found machine code (what you guys call assembly language these days) impenetrable. BASIC was too slow to make fast arcade games and I often wished for a vastly improved CPU which would enable machine code speed from BASIC.

Now I have that - a GHz class processor running Sinclair BASIC. Job done, goal achieved. Now I spend my time adding stuff to it that I find useful, or that users request. A better editor. 256 colours, a BEEP that can do more than just toggling a port bit on and off. Sprites, more graphics primitives, textured filling, rotation, scaling and all that stuff that the old Sega Arcades used to do. Better flow control with DO/WHILE/REPEAT, LOOP UNTIL etc.

And because it was the only thing available to users on the original hardware, it was their console too. So SpecBAS has full console abilities too (but it's not strictly acting as an OS of course).

So no, it's not in the same class as BBC BASIC - SpecBAS aims to be Sinclair BASIC on steroids, whereas BBC BASIC seems to be aiming itself at the general programming language class.

And that's just fine, this isn't a competition.

Heater
Posts: 13366
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sun Jul 14, 2019 6:56 pm

What?

We called it "Assembly Language" back in 1974. Assembler had symbolic names for memory addresses and mnemonics for machine instructions and registers etc.

"machine code" is that binary stuff you put into memory. Or more likely, later, enter as hexadecimal.
Memory in C++ is a leaky abstraction .

ZXDunny
Posts: 117
Joined: Sun Jul 08, 2012 7:57 pm

Re: Introduction to BBC BASIC

Sun Jul 14, 2019 7:05 pm

Heater wrote:
Sun Jul 14, 2019 6:56 pm
What?

We called it "Assembly Language" back in 1974. Assembler had symbolic names for memory addresses and mnemonics for machine instructions and registers etc.

"machine code" is that binary stuff you put into memory. Or more likely, later, enter as hexadecimal.
Yes, that was what we did. Assemblers were expensive. We used machine code, with POKE, worked out on paper. Hex did indeed come later.

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Introduction to BBC BASIC

Sun Jul 14, 2019 7:49 pm

@RichardRussell:
I am looking at a bit of BBC BASIC code by you, and it uses GOSUB a lot, as well as GOTO.

Kind of curious of why? In a language that has structured parameterised procedures and functions why are you using so much GOSUB and GOTO? I had not seen anyone use those two kewords in BBC BASIC in a very long time (actually I do not think I have ever seen GOSUB used at all).
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

ZXDunny
Posts: 117
Joined: Sun Jul 08, 2012 7:57 pm

Re: Introduction to BBC BASIC

Sun Jul 14, 2019 8:04 pm

DavidS wrote:
Sun Jul 14, 2019 7:49 pm
@RichardRussell:
I am looking at a bit of BBC BASIC code by you, and it uses GOSUB a lot, as well as GOTO.

Kind of curious of why? In a language that has structured parameterised procedures and functions why are you using so much GOSUB and GOTO? I had not seen anyone use those two kewords in BBC BASIC in a very long time (actually I do not think I have ever seen GOSUB used at all).
Well, back in the day they were a lot faster than procedures, so maybe for speed? Or just convenience?

Heater
Posts: 13366
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sun Jul 14, 2019 8:35 pm

DavidS,

I believe that code you are looking at is ejolson's "classic basic" entry for the Fibonaccu Challenge, very slightly modified to run under BBC BASIC by TimRussell.

If you have never used GOSUB you have never used BASIC.
Memory in C++ is a leaky abstraction .

Return to “Other programming languages”