What is CALL Statement in BBCSDL ?

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
User avatar
zachnoland
Posts: 22
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

What is CALL Statement in BBCSDL ?

Post by zachnoland »

I initially thought the CALL statement was for calling a function or procedure in another procedure/function parameters. I know I was wrong due to the limited learning resources available on BBC BASIC, and some misunderstandings understanding something. CALL It seems interesting, cuz when i try it causes force close and seems like memory access error.
Richard Russell
Posts: 457
Joined: Tue 18 Jun 2024, 09:32

Re: What is CALL Statement in BBCSDL ?

Post by Richard Russell »

zachnoland wrote: Tue 22 Jul 2025, 16:41 I initially thought the CALL statement was for calling a function or procedure...
In BBC BASIC, right back to the BBC Micro in 1981, CALL is used to execute a machine code routine in memory (typically, but not necessarily, created by the inline assembler). That has been the case in virtually every version of BBC BASIC since (the only exception I know is Brandy and its derivatives).

In that respect CALL is similar in operation to USR, the main difference being that CALL has a much more sophisticated mechanism for passing parameters to the machine code (it creates a parameter block in memory which the machine code can access).

The most common application of CALL that you will encounter in 'modern' BBC BASICs is in conjunction with the sortlib library, which makes good use of the parameter-passing capability. It's typically used in this context:

Code: Select all

      C% = num : CALL Sort%%, array1(0), array2(0), array3(0) ....
Here all the arrays are sorted together, with array1() being the primary sort key, array(2) being the secondary sort key (used when two or more items in array1 have the same value) and array(3) being the tertiary sort key, etc.

For another example of the use of CALL, look at hello.bbc in the examples/tools/ directory of BBCSDL. There, near the end, you will see that the assembled code is run using the statement:

Code: Select all

      CALL hello
when i try it causes force close and seems like memory access error.
Inevitably, just as with USR, CALLing a memory address which does not contain a suitable machine code routine will result in a catastrophic crash!

The online documentation for CALL is here, you could have found the answer there. It also describes an alternative use of CALL when it is given a string parameter rather than a numeric parameter, but I don't think that's what you are talking about.

Another place you could have looked is the BeebWiki.
User avatar
zachnoland
Posts: 22
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

Re: What is CALL Statement in BBCSDL ?

Post by zachnoland »

Richard Russell wrote: Tue 22 Jul 2025, 17:34 CALL is used to execute a machine code routine in memory (typically, but not necessarily, created by the inline assembler).
I came to this conclusion to run the output generated in Assembly inline, running low-level programming code.
Richard Russell wrote: Tue 22 Jul 2025, 17:34 The most common application of CALL that you will encounter in 'modern' BBC BASICs is in conjunction with the sortlib library, which makes good use of the parameter-passing capability. It's typically used in this context:

Code: Select all

      C% = num : CALL Sort%%, array1(0), array2(0), array3(0) ....
Previously I tried sortlib and was confused about why it used CALL. Likewise, why are there variables with double percentages such as double integers A%% ? (I think that variables allow to store more data than normal variable.)
Richard Russell wrote: Tue 22 Jul 2025, 17:34 For another example of the use of CALL, look at hello.bbc in the examples/tools/ directory of BBCSDL. There, near the end, you will see that the assembled code is run using the statement:

Code: Select all

      CALL hello
I don't really know what assembly id uses in hello.bbc. I don't really understand how to use inline assembly in bbcsdl. Cuz so far I only understand 2 Assembly such as aarch64/aarch32 and x86_64.
Richard Russell
Posts: 457
Joined: Tue 18 Jun 2024, 09:32

Re: What is CALL Statement in BBCSDL ?

Post by Richard Russell »

zachnoland wrote: Wed 23 Jul 2025, 10:10 Previously I tried sortlib and was confused about why it used CALL.
Firstly, sortlib was originally written in assembly language, to get the best performance. For example the BBC BASIC for Windows version is written in x86-32 assembly language, and the earlier versions for MS-DOS were written in 8086 assembly language.

Secondly, CALL accepts a variable number of parameters of different types; this is extremely convenient for sortlib, specifically because you can sort any number of arrays of any types (for example the sort key might be a numeric array and the data to be sorted a string array).

You can't do anything similar with a regular BASIC function or procedure, because the number and type(s) of the parameters are fixed. So from all points of view CALL is perfect for sorting.
Likewise, why are there variables with double percentages such as double integers A%% ?
You can find a description of all the different variable types here.
I don't really understand how to use inline assembly in bbcsdl. Cuz so far I only understand 2 Assembly such as aarch64/aarch32 and x86_64.
All three can be found in hello.bbc. If you renumber the program the x86-64 code starts at line 140:

Code: Select all

  140         .hello
  150         sub rsp,24
  160         push rbx : push rbp
  170         mov rbp,"oswrch"
  180         lea rbx,[rel asciiz]
  190         .loop
  200         mov al,[rbx]  ; get next char
  210         inc rbx       ; bump pointer
  220         cmp al,0      ; is it a NUL?
  230         jz done       ; if so, exit
  240         movzx ecx,al  ; for Windows &
  250         mov edi,ecx   ; System V ABIs
  260         call rbp      ; output char
  270         jmp short loop; continue
The aarch64 source code starts at line 410:

Code: Select all

  410         .hello
  420         stp x29,x30,[sp,#-16]!
  430         stp x19,x20,[sp,#-16]!
  440         ldr x19,oswrch
  450         adr x20,asciiz    ; pointer to text
  460         .loop
  470         ldrb w0,[x20],#1  ; get next character
  480         cbz w0,exit       ; if it's a NUL, exit
  490         blr x19           ; output character
  500         b loop            ; continue
And the arm32 source code starts at line 920:

Code: Select all

  920         .hello
  930         push {r8-r9,lr}
  940         ldr r8,oswrch
  950         adr r9,asciiz     ; pointer to text
  960         .loop
  970         ldrb r0,[r9],#1   ; get next character
  980         cmp r0,#0         ; is it a NUL ?
  990         popeq {r8-r9,pc}  ; if so, exit
 1000         blx r8            ; output character
 1010         b loop            ; continue