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

RISC OS Pi Assembly Tutorials.

Tue Feb 17, 2015 2:08 am

I intend to get into the tutorials tomorrow. Tonight I am just giving an introduction to these.

It is assumed that the reader have some experience in programming before begining here. It does not matter if it is just a little playing around in BASIC or some serious coding in an assembly language. So long as the consept of code being executed in order to instructions, with the exception of branching (including looping) is understood these should be easy to follow.

In this series we will use ARM assembly to write two games for RISC OS. First will be a WIMP based Break OUT clone that I will call Shatter Brick. That will be followed by a side scroller that will evolve into a simple 3D game to teach more consepts.

The reason for the break out game is that it gives a good way to take things one step at a time. We will start with simple input stuff, then move that to add graphics using the standard OS SWI's for the purpose, from there we will add WIMP suppoert, then multitasking, and in the end we will have a full WIMP based Shatter Brick game with preferences, high scores, and multiple levels.

It is going to take a lot of baby steps, though by the time the game is completed I hope that the reader has a good grasp on ARM assembly language and the RISC OS API, including the WIMP and cooperative multitasking.

The follow on game will fill in most of the other basic consepts that Shatter Brick did not cover.

For this tutorial I will be using !ASM for the assembler, and will be repeating every example using the BBC BASIC V assembler, as the BBC BASIC V assembler is included with RISC OS.

There will be a seperate series that will follow the same steps to teach completely new programmers to use BBC BASIC V to program for RISC OS. These will have the same exact steps along the way, and will produce the same two games in the end. The BBC BASIC beginners course will be in a seperate thread.

I hope that someone finds these tutorals useful.
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

dfeugey
Posts: 119
Joined: Mon Nov 03, 2014 2:38 pm
Location: France
Contact: Website

Re: RISC OS Pi Assenly Tutorials.

Tue Feb 17, 2015 1:27 pm

Promising :)
RISC OS FR - All RISC OS, in french (but not only)
http://www.riscos.fr/

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

Re: RISC OS Pi Assenly Tutorials.

Tue Feb 17, 2015 8:43 pm

While we are moving forward to creat a game there are some basics that will need to be covered. So do not get board by the first few lessons, as they have there purpose.

Will be using text output for the first few to teach things like how to use the BBC BASIC V assembler, branching, calling OS functions (using SWI's) and subroutines.

Also I have decided that thesetutorials will only be done in BBC BASIC V assembler. The reason is that the only assembler that you are sure to have on any RISC OS system is the BBC BASIC V assembler, do to the fact that BBC BASIC V is an intragal part of RISC OS.

I have not spent much time on this yet, so it will be this evening (I am at GMT-5) before I post the first actual lesson. I am also looking at what I had done before in the realm of showing RISC OS assembly lanmguage programming.

I hope that this results in more people being interested in ARM assembly and RISC OS.

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

Bare with me if things are a little slow for a while. I had taken a leave from ARM based computers, and using RISC OS for nearly one year, and just returned a couple of days ago. As such there will be some remembering for me to do.

Though I must say that I have missed RISC OS. I can not seem to be as productive using AROS, Linux, Atari TOS, or any other OS (with the possible exception of Commodore BASIC v2 on the C=64), there is just something about RISC OS that has always made everything easy to do.
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

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

Re: RISC OS Pi Assenly Tutorials.

Wed Feb 18, 2015 1:46 am

Ok first one:

Ok so let us get started. The example program for this section is a simple put text on the screen using SWI "OS_WriteS". This is what is comonly called a hello World program, though in our case it is a "Hello RISC OS" program.

To start with here is the full source code:

Code: Select all

      ON ERROR PRINT "LINE: " + STR$(ERL) + " ERROR: " + REPORT$:END
      
      DIM Code% 4096
      
      FOR pass%=4 TO 7 STEP 3
      P% = &8000
      O% = Code%
      [OPT pass%
        .start
        ALIGN
     
        MOV R0,R13
        ADR R13,StkEnd
        STMFD R13!,{R0,R14}
      
        SWI "OS_WriteS"
        EQUS "Hello RISC OS!"
        EQUB 0
        ALIGN
      
        LDMFD R13!,{R0,R14}
        MOV R13,R0
        MOV R0,#0
        SWI "OS_Exit"
      
      .Stk
        EQUS STRING$(248,CHR$(0))
      .StkEnd
      ]
      NEXT
      
      SYS "OS_File", 10, "!RunImage", &FF8,,Code%,O%
      
           
Only part is the Assembly Source, the rest is just to tell BBC BASIC V how to assemble it.

The first part:

Code: Select all

      ON ERROR PRINT "LINE: " + STR$(ERL) + " ERROR: " + REPORT$:END
      
      DIM Code% 4096
      
      FOR pass%=4 TO 7 STEP 3
      P% = &8000
      O% = Code%
      [OPT pass%
Is for the BBC BASIC V Assembler. That first line will tell us if there is any error, and what the error is, during assembly. The line [OPT pass% lets the BBC BASIC V Assembler know what pass of the assembly we are on, and indicates that this is the begining of the assembly listing.

Now we have a little house keeping to get started. Actualy this is not required for this program, though it is a good idea.

Code: Select all

      .start
        ALIGN
     
        MOV R0,R13
        ADR R13,StkEnd
        STMFD R13!,{R0,R14}
The .start is a label, its purpose is to give a name to the address that corrisponds to that place, this is a convinience for programming, as it makes it so we do not have to remember the numberaric addresses, or offsets.

The ALIGN just tells the assembler to align the out put to a four byte word. This is important in ARM assembly, as all ops must be aligned, and Data is faster to load if aligned.

The first actual instruction comes next. MOV R0,R13 takes what is in CPU register R13 and copies it into CPU register R0. R13 is the stack pointer, so we are saving the old stack pointer in doing this. Next is ADR R13,StkEnd This loads the adress indicated by the label .StkEnd into R13, our stack pointer. More on that in a few. Though for now know that the stack grows in such a way that things are added at lower and lower addresses, and removed in the oposite order.

Now we save a few registers onto the stack. In this case just the old stack address, and the link register (we do not use the link register, still a good habit).

The section we just covered is the prologue. Our little program would work just as well with out it. Though you want to get into the habit of including it, there is always the risk of overflowing the parrents stack frame.

Next is the part of our program that does most of the work:

Code: Select all

      
        SWI "OS_WriteS"
        EQUS "Hello RISC OS!"
        EQUB 0
        ALIGN
      
SWI, calls a software inturupt. This is a way of calling a function provided by the Operating system or a module to do something for you. In this case we are calling SWI "OS_WriteS", OS_WriteS will take the string that folows the SWI opcode and display it until encountering a null character. The EQUS directive just stores the string provided exactly as is in the code. The EQUB stores a byte value, in this case null=0 to indicate the end of the string.

Then we allign again as our bytes may have taken us out of alignment.

Next up is the epolouge (the reverse of the prolouge):

Code: Select all

        LDMFD R13!,{R0,R14}
        MOV R13,R0
We take the values we saved on the stack earlier and restore them to the registers they came from, and restore the previous stack pointer. That is that.

After that we have to exit the program:

Code: Select all

        MOV R0,#0
        SWI "OS_Exit"
We set the value in R0 to 0, then we call the SWI named OS_Exit, and this exits our program returning control to the system, with no error code (hence R0=0).

And then we define some constant space for our stack:

Code: Select all

      
      .Stk
        EQUS STRING$(248,CHR$(0))
      .StkEnd
The EQUS is as normal. Using the BBC BASIC statement STRING$(248,CHR$(0)) is a nice little trick to initialize 248 bytes to 0, and then StkEnd points above the highest word of the alocated space, and that is where our stack is set to.

The next line contains only one character, and indicates the end of the assembly language part of the program.

Then the last line:

Code: Select all

      SYS "OS_File", 10, "!RunImage", &FF8,,Code%,O%
Tells BBC BASIC V to writhe the assembled binary to a file named !RunImage of type &FF8 in the currently set directory.

To assemble once you have entered and saved the BBC BASIC File menu click in the dirrectory you want this !RunImage saved in, and select set directory, then double select click on the BBC BASIC file and it will assemble if you made no errors, you will have the !RunImage and it will work as planned.
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

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

Re: RISC OS Pi AssemblyTutorials.

Thu Feb 19, 2015 2:11 am

I seem to have ran out of time today. Spent part of the day repairing a couple of AirRifles, part of the day target shooting, part of the day studying, and a little eating in there somewhere. Now I have to pack up a couple of AirRifles to ship out.

Managed not to have enough time to get to todays tutorial, so I will try to get two out tomorrow. I apologize for this.
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

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

Re: RISC OS Pi Assembly Tutorials.

Thu Feb 19, 2015 2:46 pm

Just reading what I already posted, I noticed some omissions in the introductory two posts. So I will attempt to correct this,

First thing is that I will be kusing examples to show how to program in ARM assembly in RISC OS. I have found that for many teaching by example is the best way.

The next thing is that in RISC OS most applications consist of an application directory. That is all of the applications that have there own Icon, and have a name beginning in '!', are actualy directories, you can open these by shift double clicking.

For an example I am attaching a zip containing the Hello RISC OS! application with icon and all that. Only extract in RISC OS.

Inside of an application directory are at least two files, usualy at least 5, though two minimum. These 5 are:

!Boot : Is a script that sets up the enviroment, as well as telling RISC OS what sprite file to use for the pictorial icon of the application in a file window. Here is the contents of the !Boot file in the attached example:

Code: Select all

Set HelloROS$Dir <Obey$Dir>
IconSprites <HelloROS$Dir>.!Sprites
As you can see it just sets the directory that is the application to a name, and then sets the IconSprite file, note the actual name of the IconSprites file is !Sprites22, not !Sprites, there is a reason for this, though that is for another lesson, for now just know that the sprite in that file that has the same name as the application directory (except all lower case) will be used in filer windows.

!Run
This is another script. When you double click on the application Icon this is the file that is actualy run, and it can be the actualy binary, though it is better to use a script for a few reasons. Here is the example included in the attached example:

Code: Select all

Set HelloROS$Dir <Obey$Dir>
IconSprites <HelloROS$Dir>.!Sprites
WimpSlot -min 64k -max 64k
TaskWindow <HelloROS$Dir>.!RunImage -quit
The first two lines are repeats of the !Boot file, this is just incase the application is ran before the filer sees it. The WimpSlot line tells the WIMP how much memory to allocate to our applications main heap, in multiples of 32KB. The next line in this example runs our example program in a task window, this will be different with other applications. Other things are often set up using the !Run script, our example is as simple as it gets.

!RunImage
This is tha actual program, in this case a flat binary with an origin of &8000, and type Absolute. The name !RunImage is the normal convention, you do not have to use that name, though it is kind of expected by others.

!Sprites22
This contains the applications IconSprite, as well as any other graphic sprites for use by our application. It is not always the only sprite file in an application, though it is the one that the filer display gets the icon from.

!Help
A file of any type that RISC OS can open on the system in use, this is the file that is opened when you sellect the help option from the applications sub menu in the filer. It is intended to be of help to the user.



That is it for this one, later today I intend to do a second tutorial on assembly language in this thread.
Attachments
0HelloROS.zip
(2.61 KiB) Downloaded 175 times
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

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

Re: RISC OS Pi Assembly Tutorials.

Sun Jan 10, 2016 5:43 am

Sorry about my absence. Shortly after beginning this thread I was forced to move do to a combination of financial situation, and the health of my mom. Unfortunately in the process of moving (taking a few months to do) I seem to have lost all of my Raspberry Pi systems, and had given away my other RISC OS computers (thinking I no longer needed them, having a few RPi). So I will not be able to continue this until I can order another RPi.

That is OK, as it gives me an excuse to get an RPi 2. I have been looking to getting the chance to play with the ARM v7 and extra memory of the RPi 2, and it appears I will get my chance as soon as I have a bit of money.

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

As soon as I get another RPi I will be able to continue the tutorials, as I had written a few more after loosing internet service though before loosing the RPi's. I look forward to providing some more info on the RISC OS Operating system, as well as learning a bit more from others.

Thank you again everyone. I learn as much from you all as what I attempt to teach.
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

User avatar
Burngate
Posts: 5774
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: RISC OS Pi Assembly Tutorials.

Sun Jan 10, 2016 12:55 pm

Welcome back. I look forward to reading them

dfeugey
Posts: 119
Joined: Mon Nov 03, 2014 2:38 pm
Location: France
Contact: Website

Re: RISC OS Pi Assembly Tutorials.

Sun Jan 10, 2016 2:39 pm

So we need to wait now. I hope you'll be able to play with multicore things :)
RISC OS FR - All RISC OS, in french (but not only)
http://www.riscos.fr/

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

Re: RISC OS Pi Assembly Tutorials.

Sun Jan 10, 2016 11:43 pm

dfeugey wrote:So we need to wait now. I hope you'll be able to play with multicore things :)
I hope so as well. I have some ideas if how to possibly put the second core to use to aid RISC OS, though I will not know until I get a chance to play with it.

Perhaps even a software graphics accelerator, as a stop gap until some VideoCore code is written for use with RISC OS.

Was also thinking about making a PS/2 Keyboard and mouse interface that is bit banged from the second core, thus getting rid of the need to use USB at all (With the exception of Ethernet, at least for now [have been considering a Wiznet based interface for the RPi, though that is off topic]). I do not want to ever see anything related to low level USB again in my life, I will let others play with USB stuff at the low level.

Though mostly I just want to get back to RISC OS. I can not stand these x86 based Operating Systems.
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

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

Re: RISC OS Pi Assembly Tutorials.

Fri Jan 15, 2016 5:21 am

Having looked at the examples I had already given, I am going to begin from the ground up. I noted some things I could have done much better than what I have done.

I am thinking that doing everything in the BASIC V assembler may not be the best way around it. Perhaps I will give the simpler examples both with the BASIC V assembler and with more standard assemblers. Though I think that I will focus on using !ExtASM and !ASM, both as there are differing preferences (and creating AOF object files for linking is difficult with !ExtASM, despite !ExtASM having better support for newer instructions).

I will begin with in a few days in a new thread, with some more specific goals toward the outcome. I will be using RPCEmu running RISC OS 5.22 in order to get some good tutorials done before I replace my Raspberry Pi. I do hope to eventually include some RPi specific stuff in the tutorials, though that will be after I get a new RPi.

==================================================================================================
The game plan for the tutorials to come is:

First introduce the core concepts of ARM assembly on RISC OS, again by breaking down examples. For this I intend to write n examples as follows:
  • 1: Simple hello world style program.
    2: A for loop, with displayed output of count variable.
    3: A simple multitasking program, using VDU 7 to beep, no visible output, quit by a timer after 5 seconds. Will include the use of procedures.
    4: Add some event handling to the example from 3 above, so the user can quit from the Tasks window.
    5: Add a single window to the multitasking example, with the event handler causing the program to exit when the window is closed.
    6: Place some Icon Sprites in the window, at the current mouse position.
    7: Add a menu to the program.
    8: Add an IconBar icon sprite to the program, with its menu.
    9: Draw to the windows work area, thus requiring the app to update the window.
    10 through 16: expand this into a simple game, in the style of Missile Command.
I may need to split some of the above into more parts to give small enough steps, though that is the idea for the introduction to ARM assembly on RISC OS.

Once those are done I intend to go on to a series of module programming tutorials by example. Including things like accessing the GPIO (so long as the GPIO module is not loaded), patching some portions of RISC OS for better results, etc. All will be simple examples.

Then I will take it to the next level, introducing a number of concepts, by showing how to write a compiler, for a subset of PASCAL. This will be a simple compiler that is a look ahead recursive decent compiler (recursive decent with better optimization). Then is the task of writhing a pre-compile optimizer (redundant sub expression consolidation and similar), and a peephole post compile optimizer.

Once these topics are covered I am not sure where I will go from there. These will show a great amount of what is needed for writing software in ARM Assembly, and should give enough for anyone to go on there own with future projects.
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

dfeugey
Posts: 119
Joined: Mon Nov 03, 2014 2:38 pm
Location: France
Contact: Website

Re: RISC OS Pi Assembly Tutorials.

Fri Jan 15, 2016 11:04 am

Though mostly I just want to get back to RISC OS. I can not stand these x86 based Operating Systems.
I can't agree more :)
RISC OS is not really useful (today), but it's fun. It's a 32bit OS the 8bit way.
I am thinking that doing everything in the BASIC V assembler may not be the best way around it.
True... and not. I like too dedicated software. But some people appreciate to be able to go back and forth from BBC Basic to ASM.
For this I intend to write n examples as follows:
Good plan. It remembers me the "Wimp Programming for All" approach (paint to sprite, put sprite in a window). IMHO, the best way to go from CLI to GUI.
Then I will take it to the next level, introducing a number of concepts, by showing how to write a compiler, for a subset of PASCAL
It remembers me a series of articles around "how to make goto in ASM, then print, then cls, etc.".
May I suggest that your compiler could also output ASM code?

Then it could be a very good way to generate ASM code for BBC Basic from a simple DSL. And it could be extend to support 6502 and x86 ASM later.
RISC OS FR - All RISC OS, in french (but not only)
http://www.riscos.fr/

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

Re: RISC OS Pi Assembly Tutorials.

Sat Jan 16, 2016 4:12 am

dfeugey wrote:
Though mostly I just want to get back to RISC OS. I can not stand these x86 based Operating Systems.
I can't agree more :)
RISC OS is not really useful (today), but it's fun. It's a 32bit OS the 8bit way.
I guess that it depends on what you need it to do. For me the OS provides everything I could ask of any OS. Admittedly RISC OS is lacking HW graphics acceleration on the RPi, though that I believe will be solved with time, now that the full documentation of VideoCore IV is out.

Now there are some applications that need to be written for RISC OS, or updated to support a few modern things that are nice to have. Though that is applications, NOT the OS.

I must agree that it is fun, very much so. Though I would not call it "the 8-bit way". Yes it is based on Acorn MOS to a large degree, though the API is so much more advanced than that.
I am thinking that doing everything in the BASIC V assembler may not be the best way around it.
True... and not. I like too dedicated software. But some people appreciate to be able to go back and forth from BBC Basic to ASM.
Yes, indeed. I do not like mixing BASIC and Assembly, though the BASIC V assembler is very good at producing the code, so long as it can be done with out needing to produce linkable files.

I do prefer a good dedicated assembler, I really like !ASM, though it does not support most of the newer extensions to the ARM instruction set. So I use !ExtASM a good bit.
For this I intend to write n examples as follows:
Good plan. It remembers me the "Wimp Programming for All" approach (paint to sprite, put sprite in a window). IMHO, the best way to go from CLI to GUI.
:) . I only hope someone finds it useful. Perhaps someone that is proficient with HTML5, JS, CSS, and video encoding formats (so we may see an HTML5 complaint browser, before HTML5 is deprecated).
Then I will take it to the next level, introducing a number of concepts, by showing how to write a compiler, for a subset of PASCAL
It remembers me a series of articles around "how to make goto in ASM, then print, then cls, etc.".
May I suggest that your compiler could also output ASM code?
Well yes. It will definitely include the option for compiling to assembly listing.
Then it could be a very good way to generate ASM code for BBC Basic from a simple DSL. And it could be extend to support 6502 and x86 ASM later.
For BBC BASIC V, perhaps for those that would so apply it. I do hope that no one attempts to make it into a cross compiler for other CPU targets, though that is just me.

My main goal is to see RISC OS continue to thrive. I spend a good deal of my free time working on 3 software projects for RISC OS that I will not disclose until they are working, all with the goal of showing off RISC OS on the Raspberry Pi.

Thank you very much.
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

dfeugey
Posts: 119
Joined: Mon Nov 03, 2014 2:38 pm
Location: France
Contact: Website

Re: RISC OS Pi Assembly Tutorials.

Sat Jan 16, 2016 11:22 am

I must agree that it is fun, very much so. Though I would not call it "the 8-bit way". Yes it is based on Acorn MOS to a large degree, though the API is so much more advanced than that.
Of course it is. With 8bit, I mean a framework, not a jail. Today, we must use PMT, system calls, etc. RISC OS is still a toolbox. Use it, or not. You're free. RISC OS is bare metal with an API :D
Now there are some applications that need to be written for RISC OS, or updated to support a few modern things that are nice to have.
Yes, and I think that a RB like tool around Basic could help to speed things. AppBasic is perhaps a good candidate, but needs to evolve a bit.
Yes, indeed. I do not like mixing BASIC and Assembly
Yep, there are two different uses. Prototype in Basic and accelerate with ASM (RAD approach) VS make it in ASM (slowfast approach. Slow to code, fast to run). Both are interesting.

ASM is today in top10 of TIOBE, so there is an huge interest. On RISCOS.fr, we have a BBC Basic section, as RISC OS is the only OS still alive to provide an old school Basic that can generate GUI apps. I now think that an ASM section would be useful too. I thougt it was before, but I did have no time left . Perhaps I could make translation of your work, or something like this.

RISC OS is perfect for ASM, and what you do here can be ported to ARMLinux, where the demand for ARM ASM coders is very big.
I only hope someone finds it useful.
I think it is. Problem, ROOL forum and others are really popular, so there is not much activity on this forum. Our fault probably, as many RISC OS users read this forum.
It will definitely include the option for compiling to assembly listing.
Good :)
I do hope that no one attempts to make it into a cross compiler for other CPU targets, though that is just me.
I mean to use the same language specs for other versions of this compiler. It could be fantastic to have an integer Basic that generate BBC Basic ARM Code, or BBC Basic for Windows ARM code, or BBC Basic code (for Brandy, etc.).
I spend a good deal of my free time working on 3 software projects for RISC OS that I will not disclose until they are working
Very cool. I hope too to have some time to finish my CLI spreadsheet.

For ASM, if you need a Pi2, just ask (mail is here : http://www.riscos.fr/english.html), and you'll have it.
RISC OS FR - All RISC OS, in french (but not only)
http://www.riscos.fr/

Return to “RISCOS”