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

RISC OS Assembly Tutorials.

Mon Oct 28, 2013 11:23 pm

UPDATE CREATING NEW THREAD FOR CONTINUED EXAMPLES

New thread posted for further tutorials, in the form of the game ShaterBrick:
http://www.raspberrypi.org/phpBB3/viewt ... 55&t=59895
ShaterBrick0.gif
ShaterBrick0.gif (4.73 KiB) Viewed 12138 times
END UPDATE

In this thread I will introduce ARM Assembly in RISC OS.

I will provide examples of increasing difficulty beginning with a simple hello world type example and progressing through the basic concepts all of the way to a complete multi source WIMP program that uses a custom module for some things. The idea being to teach by example, I will also provide explanations of exactly how each example works, and what is going on.

I will attempt to proceed at a slow pace with frequent new examples.

There have been some changes to the method of what I present since this thread was begun. The changes are:
  • The choice of assembler has changed to extASM.
  • All examples will now include a version for the BBC BASIC V assembler.
  • The lessens will be a little better prepared in the future (first three will get updated), including spell checking.
WHAT IS REQUIRED FOR THESE EXAMPLES: The original text of this post was:
This morning I took a couple of minutes to whip up a couple of Assembly sample programs for RISC OS. These are only simple Hello World, and input examples. This resulted from some Linux based examples from DexOS. This got me think about evolving this into a complete tutorial series. As such I am going to start by posting these on this thread, and let it evolve from there.

I ask as well what would be required for a good series of tutorials in this area? I Have had some false starts on this since before the release of the RPi, and now it is time to start creating the tutorial in view of all, thus helping as many as possible.

My first two examples are done using ASM by Nick Roberts, and Drlink. I will consider suggestions as to the best assembler, for new RISC OS assembly language programmers, as well.


I am going to provide each example in its own post as I will also attempt to give explanation of what is going on. The idea is to teach by example.
Last edited by DavidS on Sun Nov 03, 2013 8:34 am, edited 4 times in total.
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
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: RISC OS Assembly Tuts.

Tue Oct 29, 2013 12:01 am

We will start with a simple Hello World program.

First here is the source code:

Code: Select all

;A simple Hello World styled application For RISC OS.  Displays some text and exits.
;This code is based on the Linux Hello world from DexOS.
     AREA main, CODE
     ENTRY start

start:
     ADR R0,MyHello       ;Set the address of the text to display.
     SWI "OS_PrettyPrint" ;Call OS_PrettyPrint, to display our text.

     MOV R0,#0            ;No error block.
     SWI "OS_Exit"        ;Call OS_Exit to Exit to the system.

MyHello:
     DCS "Hello from ASM, on the Raspberry Pi, with RISC OS." ;Our text.
     DCB &0D              ;Include a Caradge Return charactor.
     DCB 0                ;Indicates the end of the text.
     ALIGN                ;We have to keep things alligned.

     END
Anything starting at a ; is a comment until the next line, even if there is something else before it. Comments alow us to make notes in our sorce to ourself and others that may see the source. So the first two lines do not do anything.

Then we have some information that tells the Assembler how to do its thing:

Code: Select all

     AREA main, CODE
     ENTRY start
We are not going to worrie about exactly what this does at this time, as it is not ARM code.

Next the program is ran in order starting at the top, and continuing down. First thing we have is a label. A label starts at the begining of the line and is used to identify a location in memory. In the form used by ASM (the assembler we are using), they end with a : character. They are a convient way to refer to a location. The label start: in our listing is only needed so that the assembler and linker know where the first instruction to be ran is in our code.

After that we have the two lines:

Code: Select all

     ADR R0,MyHello       ;Set the address of the text to display.
     SWI "OS_PrettyPrint" ;Call OS_PrettyPrint, to display our text.

These together are used to tell RISC OS to display the data at the address represented by the label MyHello: on the screen, interpreting the data to be text.

The ADR mnemonic (Human readable representation of an instruction), is a psudo instruction, how it assembles veries. Though for now you just have to know that it loads the address of the a label into the specified Register. The R0 is the register, this regester has to contain the address for the os to know what to print, the camma is a seperater, and MyHello is the name of the label whos address we want.

On the next line we have the instruction SWI. SWI causes the CPU to transfer control away from our program and run a part of the OS that interprets what SWI is being called. It then passes control to the code that handles the SWI, before control is returned to our program.

The SWI "OS_PrettyPrint" will display text, and if a Caradge return character (&0D in Hex) is fornd it will continue on a new line. It knows to stop if it finds a NULL character. A null character has a value of 0.


And once we are done we have to leave our program. So the next code is:

Code: Select all

     MOV R0,#0            ;No error block.
     SWI "OS_Exit"        ;Call OS_Exit to Exit to the system.
The MOV instruction will copy a value into a register. In this case we want the value 0 in R0. The # character is used to tell the assembler that this is an immediate value, not a register number, more on this in a later instalment. We are using zero as we are not going to be telling the system about an error in this example.

The SWI "OS_Exit" tells the OS that we are done. The os will then free our programs memory, and forget about us, alowing the system to continue with other things.


I am including a zip file with this source code and the runnable program. Everything is in the application directory. Make sure to copy this out of the Zip file before you make any changes.

In order to compile the source from the download take a look at the !Help file:

Code: Select all

This is a simple Hello World application.  It does nothing other than to display some text.

You must have !ASM running, and drlink in a directory that is in your path.

To assemble open the application directory, open the A directory, and drag the File 'Hello' onto the ASM IconBar Icon, select ARMv5E and choose RUN.

Then you must link, to do so:
At the * prompt change to the application directory and enter:
*drlink -o !Hello o.Hello

Then you can run the application by double clicking on the application directory.

EDIT: I updated the download to include Build and Clean scripts.
Attachments
HelloUp.zip
The source for this example updated.
(3.05 KiB) Downloaded 374 times
Last edited by DavidS on Tue Oct 29, 2013 9:10 pm, edited 1 time in total.
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
GavinW
Posts: 90
Joined: Tue Nov 01, 2011 8:11 pm
Location: UK
Contact: Website

Re: RISC OS Assembly Tuts.

Tue Oct 29, 2013 7:42 pm

Aha! Tut as in tutorial, not as in tut-tut. Do you know this site:
http://www.heyrick.co.uk/assembler/
? Yes, ARM assembly language is seductive. Tie yourself to the mast and stuff your ears with wax. I have seen so many perish.
otium negare negotium vanum

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

Re: RISC OS Assembly Tuts.

Tue Oct 29, 2013 8:55 pm

GavinW wrote:Aha! Tut as in tutorial, not as in tut-tut. Do you know this site:
http://www.heyrick.co.uk/assembler/
? Yes, ARM assembly language is seductive. Tie yourself to the mast and stuff your ears with wax. I have seen so many perish.
LOL :) Well a good thing that ARM assembly has been my language of choice for over 20 years then.

Yes I know about heyrick.co.uk/assembler.
Last edited by DavidS on Tue Oct 29, 2013 9:00 pm, edited 1 time in total.
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
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: RISC OS Assembly Tuts.

Tue Oct 29, 2013 8:59 pm

Ok now we will look at a simple application that requests uwer input and then displays what the user typed.

So first here is the source code:

Code: Select all

;A simple Example of getting user input in text mode.  Thsi is not very effecient.
     AREA main, CODE
     ENTRY start

start:     
     SWI "OS_WriteS"      ;Display the inline Text imediately after this.
     DCS "Enter some text followed by enter:\x00"
     ALIGN
     
     ADR R0,ABuffer       ;Point to our buffer.
     MOV R1,#50           ;Give a buffer size of 50 bytes.
     MOV R2,#32           ;Allow characters in the range of 32 [space] through
     MOV R3,#126          ; the top of standard ASCII characters.
     SWI "OS_ReadLine32"  ;Get the input.
     
     SWI "OS_NewLine"     ;Skip a line.
     
     ADR R0,ABuffer       ;Point at our new string.
     SWI "OS_Write0"      ;Display our new string.
     SWI "OS_NewLine"     ;Go down one line.

     MOV R0,#0            ;No error block.
     SWI "OS_Exit"        ;Call OS_Exit to Exit to the system.

ABuffer:
     RESB 51   ;This will reserve 50 Byte buffer filled with zeros.
     ALIGN
Still not woried about the directives at the start of the file, so we will begin at the start: label:

Code: Select all

start:     
     SWI "OS_WriteS"      ;Display the inline Text imediately after this.
     DCS "Enter some text followed by enter:"
     ALIGN
Now this is an interesting one :) . The SWI "OS_WriteS" will display the text immediatly following the SWI instruction, and return to our code at the instruction after the text.

The text must end with a NULL character. And this introduces yet another feature of ASM: it alows for some C style character definitions using the backslash as a deliminator, most of these are the same as in C.

The ALIGN is a directive that makes sure that the next instruction is aligned to a multiple of 4bytes (32bits) this is required by the ARM CPU.

Now we move on to getting the input:

Code: Select all

     
     ADR R0,ABuffer       ;Point to our buffer.
     MOV R1,#50           ;Give a buffer size of 50 bytes.
     MOV R2,#32           ;Allow characters in the range of 32 [space] through
     MOV R3,#126          ; the top of standard ASCII characters.
     SWI "OS_ReadLine32"  ;Get the input.
We already know about the ADR instruction and this form of the mov instruction.

SWI "OS_ReadLine32" gets input from the default input (usualy the KB) and displays on the screen (echos it), while copying the input into ASCII form and putting it in a buffer (reserved area of memory). OS_ReadLine32 knows to stop accepting input when the user presses enter.

"OS_ReadLine32" Requires 4 parameters to tell it what to do. These are provided in the registers R0,R1,R2,and R3:
  • R0 contains the address of the buffer to put the text into.
  • R1 contains the maximum number of characters to put in the buffer.
  • R2 contains the lowest ASCI value to accept from the input.
  • R3 contains the highest ASCII value to accept from the input.
Thus we say that we want to put no more than 50 characters in the buffer, that are greater than or equal to space, and less than or equal to the highest standard printable ASCII character.

Now to printing a copy of the input surounded by blank lines:

Code: Select all

     SWI "OS_NewLine"     ;Skip a line.
     
     ADR R0,ABuffer       ;Point at our new string.
     SWI "OS_Write0"      ;Display our new string.
     SWI "OS_NewLine"     ;Go down one line.

SWI "OS_NewLine" produces a linefeed (goes to the begining of the next line for output).

SWI "OS_Write0" displays text in the buffer at the address in R0. This is different from OS_PrettyPrint in that it does not interpret control related characters in the string, just sends it directly to the VDU (through OS_WriteC, that is for another lesson).

OS_Exit is the same as in the previous lesson.

And of cource we need to reserve a buffer, we will make it 51 bytes long and filled with zerros, so no mater what the user types in it will always end with a null character (as we limit input to 50 characters). So:

Code: Select all

ABuffer:
     RESB 51   ;This will reserve 50 Byte buffer filled with zeros.
     ALIGN
RESB just reserves a number of bytes all filled with zeros. not much more to say on that one :) .

=========================================================================
In this one I have include a build script, and a clean script.

The build script will build the source:

Code: Select all

ASM -processor ARMv5E <Obey$Dir>.A.InputTest
drlink -o <Obey$Dir>.!RunImage <Obey$Dir>.o.InputTest
And the clean script will delete the assembled RunImage and lonkable object:

Code: Select all

Remove <Obey$Dir>.!RunImage
Remove <Obey$Dir>.o.InputTest
I also include a copy of this post

As usual remember only extract in RISC OS, and copy out of the zip before making changes.
Attachments
InputTest.zip
consol Input example for RISC OS.
(5.24 KiB) Downloaded 308 times
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

AMcS
Posts: 184
Joined: Sun Jan 06, 2013 11:23 am
Location: Dublin, Ireland

Re: RISC OS Assembly Tuts.

Wed Oct 30, 2013 1:13 am

GavinW wrote:Aha! Tut as in tutorial, not as in tut-tut. Do you know this site:
http://www.heyrick.co.uk/assembler/
? Yes, ARM assembly language is seductive. Tie yourself to the mast and stuff your ears with wax. I have seen so many perish.
Rick Murray's "Heyrick" site is a very good way of getting into ARM assembler (have been known to peruse it myself from time to time... and it's a real treasure), and it's also done with a sense of style and with Rick's unique sense of humour - perhaps that's his equivalent of stuffing his ears with wax - who knows :)

AMcS
Posts: 184
Joined: Sun Jan 06, 2013 11:23 am
Location: Dublin, Ireland

Re: RISC OS Assembly Tuts.

Wed Oct 30, 2013 1:22 am

Thanks for the tutorial's David, the general principles come through well (although the Assembler is not my preferred one BBC BASIC V inline - but hey each to their own :D )
DavidS wrote:

Code: Select all

ABuffer:
     RESB 51   ;This will reserve 50 Byte buffer filled with zeros.
     ALIGN
RESB just reserves a number of bytes all filled with zeros. not much more to say on that one :) .
In BBC BASIC V inline ARM assembler I'd use EQUS and a STRING$(50,CHR$(0)) for the same effect - the above becoming:

Code: Select all

.ABuffer
     EQUS STRING$(50,CHR$(0))   
     ALIGN
And yes it is longer winded - but a nice trick nonetheless :D

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

Re: RISC OS Assembly Tuts.

Wed Oct 30, 2013 4:15 am

AMcS wrote:Thanks for the tutorial's David, the general principles come through well (although the Assembler is not my preferred one BBC BASIC V inline - but hey each to their own :D )
Interesting. I actually find myself using the BBC BASIC inline assembler more and more lately. Especialy as the !ASM does not suport any of the newer instructions. I often use the BBC BASIC V assembler to assemble routines that use the newer instructions, and include the output in my assembly listing for ASM.

Maybe some one should write a stand alone assembler that uses the syntax of the BBC BASIC V inline assembler, though has proper support for macros and suport for outputting AOF files, that way we could do multiple file assembly with out resorting to tricks. Of cource it would have to include assemble time suport for some BBC BASIC V statements, like the STRING$() function and CHR$() sudo function that you mention below.

If there were support for assembling to AOF format I would probably almost exclusively use the BBC BASIC V assembler.
DavidS wrote:

Code: Select all

ABuffer:
     RESB 51   ;This will reserve 50 Byte buffer filled with zeros.
     ALIGN
RESB just reserves a number of bytes all filled with zeros. not much more to say on that one :) .
In BBC BASIC V inline ARM assembler I'd use EQUS and a STRING$(50,CHR$(0)) for the same effect - the above becoming:

Code: Select all

.ABuffer
     EQUS STRING$(50,CHR$(0))   
     ALIGN
And yes it is longer winded - but a nice trick nonetheless :D
Yes it is a nice trick. Should I provide a BBC BASIC V version of each of my examples?

Also is there a simple way to produce AOFs from BBC BASIC V?

I know that there used to be an extension for BBC BASIC to do this though I think it is a 26bit only module.
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

MarkTP
Posts: 7
Joined: Mon Mar 11, 2013 5:26 am

Re: RISC OS Assembly Tutorials.

Wed Oct 30, 2013 7:41 am

Hi David
What a great idea to post some simple(?) tutorials as an intro to Assembly. I have recently embarked on my second childhood by starting to learn BBC BASIC (well no-one uses fortran any more do they?). So in response to you offer to use the BASIC inline assembler, YES PLEASE. I would dearly love to get my head around this, it seems a great way to learn how a computer really works. Keep up the great work!

User avatar
GavinW
Posts: 90
Joined: Tue Nov 01, 2011 8:11 pm
Location: UK
Contact: Website

Re: RISC OS Assembly Tutorials.

Wed Oct 30, 2013 9:45 am

There is an important point about the BBC Basic assembler that I think is sometimes missed, or is not stressed enough; namely that the assembled code, if it is not just for a transient utility, is designed to use BBC Basic's runtime system. By contrast, standalone assemblers usually produce AOF files so that they can be linked and use the C runtime system (or Fortran or whatever). I believe that Terje Sletteboe produced !extAOF so that you could use the BBC Basic assembler to produce AOF files. There have been a lot of standalone assemblers produced over the years: Charm's assembler (Pete Nowosad), AS (Niklas Roejemo), ASM (Nick Roberts), ExtBasAsm (Darren Salt), extASM (Eivind Hagen and Terje Sletteboe), TLA (the last assembler :?) and certainly others whose names I have forgotten. They are a testament to the strong aesthetic pull of the ARM's original instruction set.
otium negare negotium vanum

Steve Drain
Posts: 105
Joined: Tue Oct 30, 2012 2:08 pm
Location: Exeter UK

Re: RISC OS Assembly Tutorials.

Wed Oct 30, 2013 11:18 am

GavinW wrote:There is an important point about the BBC Basic assembler that I think is sometimes missed, or is not stressed enough; namely that the assembled code, if it is not just for a transient utility, is designed to use BBC Basic's runtime system.
I am not quite sure this is clear to me. You can use offset assembly to write modules, for instance.
There have been a lot of standalone assemblers ... ExtBasAsm (Darren Salt)
I can make not comment about the others, but this is an extension to the BASIC assembler. It is fairly comprehensive and can produce AOF files. However, as has been mentioned elsewhere, it is hard-coded to old versions of the BASIC module and is 26-bit only. The source is available, and I would be delighted if some clever person could update it. :)
They are a testament to the strong aesthetic pull of the ARM's original instruction set.
Indeed they are. :)

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

Re: RISC OS Assembly Tutorials.

Wed Oct 30, 2013 3:13 pm

MarkTP wrote: Hi David
What a great idea to post some simple(?) tutorials as an intro to Assembly. I have recently embarked on my second childhood by starting to learn BBC BASIC (well no-one uses fortran any more do they?). So in response to you offer to use the BASIC inline assembler, YES PLEASE. I would dearly love to get my head around this, it seems a great way to learn how a computer really works. Keep up the great work!
Ok I will include a BBC BASIC V version of all of every future instalment, including todays. In which I will be introducing branching (a for loop example).

As a bit of a sylabus, tomorow I will give a HelloWimp example that just creates a window with a text icon saying Hello, I will also be including a discription of all ARMv6 instructions either today or tomorow. The folowing day will be WimpTut2 adding menus and an Iconbar icon, then WimpTut3 more gadgets, the day after that WimpTut4 Drag and Drop.

I have already written the code for the first 15 tutorials, the descriptions slow me down a bit. I feel that one per day should be good though. And of course now I havve to do a BBC BASIC V assembler version of each.
GrainW wrote: There is an important point about the BBC Basic assembler that I think is sometimes missed, or is not stressed enough; namely that the assembled code, if it is not just for a transient utility, is designed to use BBC Basic's runtime system.
Realy? I have never had any trouble writing any thing in BBC BASIC V assembly. Though I do admit that it takes a bit of work to deal with multi module source as there is no longer a good way to produce an AOF from BBC BASIC.
By contrast, standalone assemblers usually produce AOF files so that they can be linked and use the C runtime system (or Fortran or whatever). I believe that Terje Sletteboe produced !extAOF so that you could use the BBC Basic assembler to produce AOF files.
Oh really??? I did not know that ExtAOF coud be used with BBC BASIC V files. I will have to give that a try.
...
They are a testament to the strong aesthetic pull of the ARM's original instruction set.
Yes they are :) .

=============================================================================================
Steve Drain wrote: I can make not comment about the others, but this is an extension to the BASIC assembler. It is fairly comprehensive and can produce AOF files. However, as has been mentioned elsewhere, it is hard-coded to old versions of the BASIC module and is 26-bit only. The source is available, and I would be delighted if some clever person could update it. :)
I did not know that the source is available. I can not even find ExtBasAsm anywhere anymore.

Thank you.
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

AMcS
Posts: 184
Joined: Sun Jan 06, 2013 11:23 am
Location: Dublin, Ireland

Re: RISC OS Assembly Tuts.

Wed Oct 30, 2013 8:02 pm

DavidS wrote:
AMcS wrote:Thanks for the tutorial's David, the general principles come through well (although the Assembler is not my preferred one BBC BASIC V inline - but hey each to their own :D )
Should I provide a BBC BASIC V version of each of my examples?
Yes please, but only if it is not too much trouble.
DavidS wrote:Also is there a simple way to produce AOFs from BBC BASIC V?

I know that there used to be an extension for BBC BASIC to do this though I think it is a 26bit only module.
Unfortunately I don't know.

AMcS
Posts: 184
Joined: Sun Jan 06, 2013 11:23 am
Location: Dublin, Ireland

Re: RISC OS Assembly Tutorials.

Wed Oct 30, 2013 8:17 pm

GavinW wrote:They are a testament to the strong aesthetic pull of the ARM's original instruction set.
Indeed, ARM Assembly started as a really neat, well structured comprehensible language. It (sadly) IMHO is being progressively butchered - but at least the core (as shown in BBC BASIC ARM assembler and the others you mentioned) still shows the glory of what was.

Must admit I can't imagine what ARM v8 will look like (probably too ugly for words I suspect... but would be happy to be proven wrong !).

uart77
Posts: 21
Joined: Thu Jun 20, 2013 8:27 pm

Re: RISC OS Assembly Tutorials.

Wed Oct 30, 2013 8:49 pm

Good work, David. What I said about RiscOS was wrong. To be honest, I haven't used it much and don't know much about it. I'll give it another try.
Most RISC OS assemblers are written in C (much as Fasm and Nasm are)
No, FASM is written in 100% ASSEMBLER and it's extremely efficient. Source included. Re-distributable. Sorry we don't have a native ARM version yet.

DexOS: Thanks for the RiscOS examples! :) All I needed to know was the executable format/header. Man, I could easily create an assembler for RPI bare-metal. Since I don't have file I/O for RPI, though, it would assemble source code in memory.

My D-ARM7 Disassembler
My Magic-ARM Assembler+Compiler

Steve Drain
Posts: 105
Joined: Tue Oct 30, 2012 2:08 pm
Location: Exeter UK

Re: RISC OS Assembly Tutorials.

Wed Oct 30, 2013 9:01 pm

DavidS wrote:I did not know that the source is available. I can not even find ExtBasAsm anywhere anymore.
You can find both at: http://old-www.moreofthesa.me.uk/progs.source.html.

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

Re: RISC OS Assembly Tutorials.

Thu Oct 31, 2013 12:56 am

This time we will show how simple branching is done by implementing a FOR loop in assembly.

In BBC BASIC V the for loop would look like:

Code: Select all

FOR x% = 1 TO &1000 STEP 1
  PRINT "The current count in HEX is: ";~x%
NEXT
So let us again begin with wome code though twice this time.
First in formated for the ASM assembler:

Code: Select all

;A simple Example of getting user input in text mode.  Thsi is not very effecient.
     AREA main, CODE
     ENTRY start

start:
     SWI "OS_WriteS"          ; Display the inline Text imediatly after this.
     DCS "We will itterate through our loop displaying each number as we go."
     EQUB 0
     ALIGN

     MOV  R0,R13                  ;We need to save the old stack.
     ADRl R13,StckEnd             ;Set up a local stack (way bigger than we need for now).
     
     STMFD R13!,{R0-R4,R14}   ; Save our registers, and the Link register.


     MOV R3,#0               ; FOR X=1
     MOV R4,#&1000           ; TO &1000
loop00:
     ADD R3,R3,#1            ; STEP 1

     SWI "OS_WriteS"         ; Display a prefix for our number.
     EQUS "The current count in HEX is: &\x00"
     EQUB 0
     ALIGN

     MOV R0,R3                ; Pass the number to convert and,
     ADR R1,HexStr
     MOV R2,#16
     SWI "OS_ConvertHex4"
     SWI "OS_Write0"          ; Display the string.
     SWI  "OS_NewLine"

                              ;NEXT (the next instructions.
     CMP R3,R4                ; Are we done looping yet?
     BNE loop00              ; If not go back to the loop00.

     LDMFD R13!,{R0-R4,R14}
     MOV   R13,R0

     MOV  R0,#0
     SWI "OS_Exit"


HexStr:
    RESB 6
    ALIGN DBLE

;Reserve 1024 bytes (256 Words) for our local stack
    RESB 1024
    ALIGN DBLE
StckEnd:
    ALIGN
And the same for the BBC BASIC assembler:

Code: Select all

ON ERROR PRINT "LINE : " + STR$(ERL) + " ERROR : " + REPORT$ : E

DIM Code% 4096

FOR pass% = 4 TO 7 STEP 3
P% = &8000
O% = Code%
[OPT pass%
.start
  SWI "OS_WriteS"          ;
  EQUS "We will itterate through our loop displaying each number
s we go."
  ALIGN

  MOV R0,R13
  ADR R13,StckEnd
  STMFD R13!,{R0-R4,R14}

  MOV R3,#0
  MOV R4,#&1000
.loop
  ADD R3,R3,#1

  SWI "OS_WriteS"
  EQUS "The current count in hex is: &"+CHR$(0)
  ALIGN

  MOV R0,R3
  ADR R1,HexStr
  MOV R2,#5
  SWI "OS_ConvertHex4"
  SWI "OS_Write0"
  SWI "OS_NewLine"

  CMP R3,R4
  BNE loop

  LDMFD R13!,{R0-R4,R14}
  MOV R13,R0

  MOV R0,#0
  SWI "OS_Exit"
  

.HexStr
  EQUS STRING$(8,CHR$(0))
  ALIGN

  EQUS STRING$(248,CHR$(0))
.StckEnd
]
NEXT

SYS "OS_File", 10, "!RunImage", &FF8, , Code%, O%
You may notice that I did not include an AIF header on the BBC BASIC V code, it will still work quite well. Also I must apologice about the lack of comments in the BBC BASIC copy.

As the two are almost identicle, I will use the BBC BASIC V copy to break down.

Code: Select all

.start
  SWI "OS_WriteS"          ;
  EQUS "We will itterate through our loop displaying each number
s we go."
  ALIGN
Nothing new here. Just displaying a string with OS_WriteS.

Code: Select all

  MOV R0,R13
  ADR R13,StckEnd
  STMFD R13!,{R0-R4,R14}
OK this is a bit different from what I have shown you.

We copy R13 into R0 so as to preserve it, as we must set R13 before we can use it as a stack pointer. That is done by the MOV R0,R13 instruction.

Then to set the value of the stack to the space we reserve for it at the end of the file, we use the ADR psudo op again. Though this time we point at the last word (this is because the stack starts at a high address and grows toward lower addresses).

Then we immediately use the stack to store the registers we are going to use, and the old value of R13 (now in R0). We are also saving the Link Register, we do not need to for this example though it is a good idea to get in the habit as the link register (R14) will be used alot once we start doing subroutines.

Code: Select all

  MOV R3,#0
  MOV R4,#&1000
.loop
  ADD R3,R3,#1
This portion is the same as the FOR x% = 1 TO &1000 STEP 1 in our for loop.

R3 is used as our veriable, and R4 is the limit. I chose these as R1, R2, and R3 are all used by SWIs that we call.

If you gessed that ADD R3,R3,#1 is equilivent to R3 = R3 + 1, you are correct.

Code: Select all

  MOV R0,R3
  ADR R1,HexStr
  MOV R2,#5
  SWI "OS_ConvertHex4"
  SWI "OS_Write0"
  SWI "OS_NewLine"
The only new thing here is the OS_ConvertHex4 SWI call. OS_ConvertHex4 takes the value in R0 and converts it to an ASCII string of Hexadecimal digits, placing the result in the buffer whose address is in R1, the buffer size being passed in R2, the 4 means that it produces a 4 digit string. After calling "OS_ConvertHex4" R0 will hold the adress of the buffer. So we can call OS_Write0 with out any extra setup.

Code: Select all

  CMP R3,R4
  BNE loop
NEXT X. The compare instrucion compares R3 with R4. The B instruction will branch to the label loop (at the start of our loop). This means instead of continuing to the next instruction the next instruction ran will be the one at loop. Though we put the NE postfix on our B instruntion, so it will only work so long as R3 and R4 our not equal (thank to the CMP instruction) (NE means not equal), and they will only be equal if the loop is done.

Code: Select all

  LDMFD R13!,{R0-R4,R14}
  MOV R13,R0
This restores the registers that we saved on our stack when we got started.

Code: Select all

  MOV R0,#0
  SWI "OS_Exit"
Same as always. Time for our program to quit.

Then we reserve some buffer space for OS_ConvertHex4 and for our stack.



As always the zip file contains todays example code. And you must unzip in RISC OS, and copy the files out of the ZIP before runing. This time I did not include any binarys, so you have to build it.
Attachments
TutForLoop.zip
(7.17 KiB) Downloaded 295 times
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
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: RISC OS Assembly Tutorials.

Thu Oct 31, 2013 5:46 am

AMcS wrote:
DavidS wrote:
AMcS wrote:Thanks for the tutorial's David, the general principles come through well (although the Assembler is not my preferred one BBC BASIC V inline - but hey each to their own :D )
Should I provide a BBC BASIC V version of each of my examples?
Yes please, but only if it is not too much trouble.
As you can see, it is not. I am including the BBC BASIC inline assembler version from now on. I plan to take a couple of minutes to convert the first two examples to BBC BASIC and expand those postings to reflect the BBC BASIC Implementations.
DavidS wrote:Also is there a simple way to produce AOFs from BBC BASIC V?

I know that there used to be an extension for BBC BASIC to do this though I think it is a 26bit only module.
Unfortunately I don't know.
That Is OK it is pointed to below by Steve Drain. Thank you.


AMcS wrote: Indeed, ARM Assembly started as a really neat, well structured comprehensible language. It (sadly) IMHO is being progressively butchered - but at least the core (as shown in BBC BASIC ARM assembler and the others you mentioned) still shows the glory of what was.
Yes and I am happy that there are still people interested in learning ARM assembly. The original instructions have not changed, and other than the status bits being moved out of R15 it is basicly the same language. I do note though that most of the newer ops are not part of the ARM; VFP, Jazel, Thumb are all not ARM, even though the THUMB extensions use the same core. I write in ARM assembly not the others, well ok I use some VFP.
Must admit I can't imagine what ARM v8 will look like (probably too ugly for words I suspect... but would be happy to be proven wrong !).
I hope that you are wrong on that also. I have not looked at the ARMv8 docs yet.


=============================================================================================

uart77 wrote: Good work, David. What I said about RiscOS was wrong. To be honest, I haven't used it much and don't know much about it. I'll give it another try.
Well it is a lot different from most other OSes so this puts some people off. Then there is the issue of the version of RISC OS included in the current NOOBs image is extremely buggy and does not reflect the current RISC OS on the RPi. I find that I am often telling people to download the RC11 image and use that.
No, FASM is written in 100% ASSEMBLER and it's extremely efficient. Source included. Re-distributable. Sorry we don't have a native ARM version yet.
I am glad that I was wrong on that.
... All I needed to know was the executable format/header. ...
Well you already got it from DexOS, though you do not need it. Even though you are not supposed to you can use flat binaries on RISC OS so long as they are assembled with an origin of &8000 (0x8000) and begin at there origin. This is the old way in RISC OS and it still works (so far).

If you look at my example from today for the BBC BASIC V assembler you will note that I did not use a header on that one, thus giving an example of this.

=============================================================================================
Steve Drain wrote:
DavidS wrote: I did not know that the source is available. I can not even find ExtBasAsm anywhere anymore.
You can find both at: http://old-www.moreofthesa.me.uk/progs.source.html
Thank you for that. I have began to look at the source, I probably will not do much with it though it lookes as if getting it 32bit should be no trouble. The trouble will be getting it to work with BBC BASIC V version 1.54, and all future versions. Who knows maybe I will play with the source and see if anything comes of it.
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
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: RISC OS Assembly Tutorials.

Thu Oct 31, 2013 8:14 pm

uart77 wrote: DexOS: Thanks for the RiscOS examples! :) All I needed to know was the executable format/header. Man, I could easily create an assembler for RPI bare-metal. Since I don't have file I/O for RPI, though, it would assemble source code in memory.

My D-ARM7 Disassembler
My Magic-ARM Assembler+Compiler
The way i use fasmArm with riscos is to assembly the files on a x86 linux pc and use the built in python server
python -m SimpleHTTPServer
Then i can download the assembled file to riscos over the lan, i then use the middle mouse button on the file and go to file type and change it to Absolute,
then click ok and then you can double click on the file to run it.
Batteries not included, Some assembly required.

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

Re: RISC OS Assembly Tutorials.

Thu Oct 31, 2013 8:38 pm

Sorry about the delay in posting todays example. I have been looking at some of the better known WIMP programming tutorials, and am restructuring my future examples. As such there is a gap that I have to fill in. The next example is going to put an icon on the IconBar and wait for the user to Adjust click (right mouse button click) to exit.

In looking at the example of the existing WIMP programming tutorials I realized that I may be jumping ahead just a bit much in going directly into the full window based example.
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
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: RISC OS Assembly Tutorials.

Fri Nov 01, 2013 2:03 am

*** UPDATED SOURCES ***

As I do not have as much time today as usual, I am providing an archive with some changes to the three examples already given.

The modifications are:
  • Better !Help files describing how to build each example.
  • A BBC BASIC V assembler source for each file.
  • The assembly source has been changed to !extASM format.
On that last item:

I decided to change over to !extASM, as the syntax makes translating from BBC BASIC V simpler, and it also makes it much easier to translate from FasmARM.

extASM may be obtained from:
http://home.broadpark.no/~terjesl/eng/extasm.html


To view the !Help in any of these:
  1. Middle click over the application Icon
  2. select the sub menu having the name of the application
  3. select Help
These are all given in source only form. Though they all include scripts for building, and clean. I have tested each of these samples.


As usual only extract in RISC OS, and copy everything out of the ZIP file before using.
Attachments
UPDATED.zip
All of the first three samples, updated for !extASM
(16.58 KiB) Downloaded 313 times
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
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: RISC OS Assembly Tutorials.

Sat Nov 02, 2013 10:43 pm

I like the new !extASM format examples, as you said it much more fasmarm like format.
Batteries not included, Some assembly required.

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

Re: RISC OS Assembly Tutorials.

Sat Nov 02, 2013 10:55 pm

DexOS wrote:I like the new !extASM format examples, as you said it much more fasmarm like format.
Well thank you for that, I will then choose extASM with ExtAOF as my assembler of example from now on. I do wish that there were a better free linker than drlink, though I do not know of any.

Could you recomend how I should continue forward from the three that I already gave?

With the newer samples that I have written there seems to be to much new to introduce in a single post and I am attempting to take this in small steps so that any one that is fairly new to ARM assembly will be able to keep up.

If I move forward as planed the next post would have to be about 40,000 words to cover all of the new ground, and this seems inefecient.

I had not thought about how much is introduced until I attempt to explain it in an educational maner. I have been working with ARM assembly so long that it is second nature to me, so I do not think about the small things.
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
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: RISC OS Assembly Tutorials.

Sat Nov 02, 2013 11:45 pm

The best tut i followed was for something i was interested in doing, that started with the basics and work up, each tut was simple in them selfs, but together make a large project.

Example a game, you start off showing how to changing modes, then setting buffers up, then drawing a single pixel to screen, then a block of pixel, then moving a block of pixels etc.
And so on, graphic do grab people attention.

By having a goal and saying i going to teach how to do this ?, like the game example.
Then beginners will look forward to the next example.
I think we learn better if we are interested in the project, for example i learnt more by doing game tut's, than coding tut on maths.
If i can hep in anyway let me know.
Batteries not included, Some assembly required.

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

Re: RISC OS Assembly Tutorials.

Sun Nov 03, 2013 12:15 am

DexOS wrote:The best tut i followed was for something i was interested in doing, that started with the basics and work up, each tut was simple in them selfs, but together make a large project.

Example a game, you start off showing how to changing modes, then setting buffers up, then drawing a single pixel to screen, then a block of pixel, then moving a block of pixels etc.
And so on, graphic do grab people attention.
Ok that does make sence. And I do like the idea of a game tutorial, that seems to the kind of thing that would grab some attention. And a game would be easy to build up in baby steps. Will have to think what direction to go.

I would like to work into a simple 3D game of some form, though that would require some significant math as well.

Maybe a game, I will think about that.
By having a goal and saying i going to teach how to do this ?, like the game example.
Then beginners will look forward to the next example.
I think we learn better if we are interested in the project, for example i learnt more by doing game tut's, than coding tut on maths.
If i can hep in anyway let me know.
Do you think that a simple BBC BASIC V compiler may be a project goal also? Most RISC OS users have at least a passing familiarity with BBC BASIC V, and writing the suport functions for all of the statements would also give a good deal of insite into the way to do many things in RISC OS.

Further a BBC BASIC V Compiler would also necisitate an assembler. I am thinking that writing a complete assembler as a back end could make a fairly complete set of tutorials. Begin with the assembler (fairly simple) and move into compiling expresions using a simple recursive decent parser, then push on to blocks of code, including those with a prefix and postfix such as loops, then on into the input and output functions etc.

We do not have a good BBC BASIC Compiler at this time (ABC is the best that is available), so this could be a kill two birds with one stone project.

Regardless of the compiler idea, I realy think that I will do one set of examples by implementing a good graphical game, just need to design one for the purpose.

Thank you for the suggestions
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

Return to “RISCOS”