PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

ARM Assembler Programming PPC FAQ

Can anyone give me any information an writing assembler for arm using VC++ embedded tools.

An example would make all my dreams come true
simonjacobs · 20 Feb 2002, 11:29 UTC
This is a FAQ for ARM assembler programming on the Pocket PC.

Maintained by Simon Jacobs

Version 1.0 (22'nd Jan 2002)

I have done my best to make this accurate and useful, but I make no guarantees. Remember that assembler programming is complex and if you are unsure of yourself you should ask for help. Please email me if there are any questions you would like to see added to the list.


--------------------------------------------------------------------------------

1) Is there a good book about ARM assembler?
2) Can I use inline assembler in my c code with Embedded Visual C++?
3) Can I see what assembler code the c compiler creates?
4) How do I add an assembley source file to my project, and get EVC to assemble it?
5) How do I write a function in assembler and call it from the c code?
6) How are values passed to and from the assembler function?
7) Can I use any of the 16 registers for anything?
8) Useful resources.


--------------------------------------------------------------------------------

1) Is there a good book about ARM assembler.

ARM System-on-chip architecture (second edition)
Steve Furber
Addison-Wesley

As well as some chapters aimed more at hardware designers, it has an explanation of the ARM instruction set plus a complete reference of the assembler format for each one. It doesnt teach programming in any way, but if you are experienced in c programming and just need full details of the instruction set and info on how the processor works it is great.

2) Can I use inline assembler in my c code with Embedded Visual C++?

There is no supported way of doing this. You need to put your assembler in seperate functions which can be called from your c code. See below for details of how to do this.

3) Can I see what assembler code the c compiler creates?

Yes, and this is very useful for a few reasons. You can see how optimal the code being produced is, and also learn how the compiler does things. You need to:

From the 'Project' menu choose 'Settings'. Go to the 'C/C++' tab. In the 'Category' drop-down-list choose 'Listing Files'. In the 'Listing file type' drop-down-list choose 'Assembley with Source Code'.

Now the compiler will produce .asm files in the output directory which contain the assembler code with the c source as comments.

4) How do I add an assembley source file to my project, and get EVC to assemble it?

Create a blank file 'myfile.asm' in your project directory with notepad. From the menu in EVC select 'Project->Add to Project->Files' set the 'Files of type' field to 'All Files' and select the myfile.asm file. It will appear in the file list on the left, drag it into the 'Source Files' section.

Select 'Project->Settings' from the menu. On the left of the dialog: select the 'ARM release' configuration and select the 'myfile.asm' file. On the right of the dialog: select the 'Custom Build' tab. In the 'Commands' box add the text 'armasm myfile.asm' and in the 'Outputs' box add the text 'myfile.obj'. Repeat this for the 'ARM debug' configuration.

Now you can open and edit myfile.asm from EVC and it will be assembled into your project.

5) How do I write a function in assembler and call it from the c code?

First a note about the assembler source code layout. All command lines should begin with a tab. Any lines that have text on the left are labels. You need to use labels for function names and as targets for branch instructions. Use a semicolon to mark the rest of a line as a comment. The file should finish with an END command.

Example file:
Code:

AREA ArmGfxLib,CODE,READONLY
EXPORT FunctionA
EXPORT FunctionB

FunctionA
MOV r12, sp
stmfd sp!, {r4-r11,lr}

; useful code to do something goes here!

ldmfd sp!, {r4-r11,pc}

FunctionB
MOV r12, sp
stmfd sp!, {r4-r11,lr}

; useful code to do something goes here!

ldmfd sp!, {r4-r11,pc}

END

Explanation: the AREA command gives a name to this code section. The EXPORT command makes those labels available as functions. The stmfd/ldmfd commands save and restore the registers to the stack, so you dont corrupt values that the calling function depends on.

In your c++ code add the line:

#ifdef ARM
extern "C" void FunctionA(int a, int b);
#endif

The #ifdef is because we have written the function in ARM assembler so it isnt available in MIPS/SH3 builds.

6) How are values passed to and from the assembler function?

If you declare a function of the form:

extern "C" int armfunction(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6);

The first 4 arguments will be passed in the registers r0-r3. These registers will contain the passed values at the start of your assembler function. Further arguments are passed on the stack, see the code below to read them.

The return value should be placed into register r0 at the end of your assembler function (just before you return to the caller).

This works well with int's and pointers (all pointers are 32 bits). If you try and pass a structure directly as an argument it gets more complex. I advise you to only pass/return integers and pointers to/from your assembler functions, at least to start with.

Here is an example function (declared as above) which adds its six arguments and returns the result.
Code:

armfunction
MOV r12, sp ; these two lines are
stmfd sp!, {r4-r11,lr} ; standard setup code

; r0-r3 are the first 4 arguments passed

ldr r4, [r12] ; load 5'th argument into r4
ldr r5, [r12, #4] ; load 6'th argument into r5

add r0, r0, r1 ; r0 = r0 + r1
add r0, r0, r2 ; r0 = r0 + r2
add r0, r0, r3 ; r0 = r0 + r3
add r0, r0, r4 ; r0 = r0 + r4
add r0, r0, r5 ; r0 = r0 + r5

ldmfd sp!, {r4-r11,pc} ; standard cleanup and return code

7) Can I use any of the 16 registers for anything?

No. You can use r0-r12 and r14 for anything you like. r15 is the program counter, if you write a value to it then execution will jump to that location. Use the branch instruction, dont alter it directly. r13 is the stack pointer, do not alter it unless you are sure you know what you are doing. r14 (which you can use) is the link register. If you want to call another function you need to set this to the return address, I will include an example in a future version of this FAQ.

8) Useful resources.

  • <a href="http://www.ukscene.org/sulph/">sulphur's gba development site</a>

  • <a href="http://www.intel.com/design/strong/manuals/">Intel StrongARM Manuals</a>

  • <a href="http://www.geocities.com/wonglinhoo/Arm.htm">The Arm Microprocessor</a>

  • <a href="http://www.soup-kitchen.net/armcode/">Soup Kitchen - Arm Tutorial</a>

  • <a href="http://www.heyrick.co.uk/assembler/">ARM Assembler Programming</a>

  • <a href="http://www.ee.ic.ac.uk/pcheung/teaching/ee2_computing/">ARM Assembly Lecture Notes</a>

  • <a href="http://wheelie.tees.ac.uk/users/a.clements/ARMinfo/ARMnote.htm">The ARM Processor</a>

  • <a href="http://re-eject.gbadev.org/">ARM Quick-Instruction reference sheet</a>

  • <a href="http://www.gbadev.org/">gameboy advance development</a>
wrecked · 20 Feb 2002, 11:40 UTC — thanks
Thats great mate..
Im now in ecstasy

cheers!!!!!!!
wrecked · 20 Feb 2002, 11:43 UTC — address
What is the url for soup kitchen and other stuff
simonjacobs · 20 Feb 2002, 11:44 UTC
8) Useful resources.

  • <a href="http://www.ukscene.org/sulph/">sulphur's gba development site</a>

  • <a href="http://www.intel.com/design/strong/manuals/">Intel StrongARM Manuals</a>

  • <a href="http://www.geocities.com/wonglinhoo/Arm.htm">The Arm Microprocessor</a>

  • <a href="http://www.soup-kitchen.net/armcode/">Soup Kitchen - Arm Tutorial</a>

  • <a href="http://www.heyrick.co.uk/assembler/">ARM Assembler Programming</a>

  • <a href="http://www.ee.ic.ac.uk/pcheung/teaching/ee2_computing/">ARM Assembly Lecture Notes</a>

  • <a href="http://wheelie.tees.ac.uk/users/a.clements/ARMinfo/ARMnote.htm">The ARM Processor</a>

  • <a href="http://re-eject.gbadev.org/">ARM Quick-Instruction reference sheet</a>

  • <a href="http://www.gbadev.org/">gameboy advance development</a>
Hiya.

Firstly, you can't (easily) embed ARM assembler in EVC++ (which *stinks*).

So, you must make external ARM files.

For example, take this and save it as "clear_screen.s":

Code:
; TITLE("clear_screen")
;++
;
;
; Module Name:
;
; clear_screen.s
;
; Abstract:
;
; Test procedure to compile and run on my IPAQ... (keyword there is test)
;
;

AREA screen_writer, CODE, READONLY ; name this block of code

EXPORT clear_screen_arm

; simple, unoptimised CLS routine, hard-coded to a 16bpp 320x240 screen.

; Takes address of screen in R0
; colour in R1

clear_screen_arm
stmfd sp!, {r0-r12,r14}
add r1,r1,r1,lsl #16 ; colour into both pixels please
mov r2,r1
mov r3,r1
mov r4,r1
mov r5,r1
mov r6,r1
mov r7,r1
mov r8,r1
mov r9,r1
mov r10,r1
mov r11,r1
mov r12,r1

mov r14,#320
clear_screen_arm_inner
stmia r0!,{r1-r12} ; store 24 pixels in one shot.
stmia r0!,{r1-r12} ; store 24 pixels in one shot.
stmia r0!,{r1-r12} ; store 24 pixels in one shot.
stmia r0!,{r1-r12} ; store 24 pixels in one shot.
stmia r0!,{r1-r12} ; store 24 pixels in one shot.
stmia r0!,{r1-r12} ; store 24 pixels in one shot.
stmia r0!,{r1-r12} ; store 24 pixels in one shot.
stmia r0!,{r1-r12} ; store 24 pixels in one shot.
stmia r0!,{r1-r12} ; store 24 pixels in one shot.
stmia r0!,{r1-r12} ; store 24 pixels in one shot.

subs r14,r14,#1
bne clear_screen_arm_inner

ldmfd sp!, {r0-r12,pc}

END


Now, you need to compile it. With your path set correctly, at a command/dos prompt run:

armasm clear_screen.s

(you can include a macro to compile your *.s files elsewhere in you project - this has already been discussed somewhere on this board, IIRC).

Now, move the clear_screen.obj file to where you want it, and make sure that the directory is in the:

Tools -> Options ->Directories -> Library files

list.

In the C file that calls the function, put (I put it just after the #includes):

Code:
extern "C" void clear_screen_arm(DWORD,unsigned short);


... and here's a function that calls it and sets the screen pink (uses GAPI to get the screen location, then munges it for my iPAQ):

Code:
bool ClearScreen()
{
unsigned short * pusLine = (unsigned short *)GXBeginDraw();
if (pusLine == NULL) return false; // NOT OK TO DRAW, return failure.

// WARNING hard-coded for the purposes of it actually running on my iPAQ 3600
// gxbegindraw doesn't give me the first pixel of the display buffer,
// but the END pixel of the first line.

pusLine-=319;

clear_screen_arm((DWORD)pusLine,0xf81f); // 0xf81f = 1111100000011111 = red + blue = pink
GXEndDraw();
return false;
}


I might be doing silly things with the C code.. ('cos I haven't done C for *years*)... but it compiles and has the desired effect on my iPAQ - I put the code into the FirstGX example and called it instead of their:
Code:
bool ClearScreen(int colRed,int colGreen, int colBlue)


function. Adding the parameters to the C function to pass the "correct" value to the ARM function is trivial - see the thread about "How to calculate the real RGB color for GAPI? "
http://forums.pocketmatrix.com/viewtopic.php?t=4048 for that.

(a pink screen isn't much, but it's a start. :-D).

If anybody can point out errors in the C, I'd appreciate it (do I need to cast it to a DWORD?).

Hope that helps, cheers,

Refractor.
simonjacobs · 20 Feb 2002, 12:16 UTC
just a minor note: you only need to save r4-r11, not r0-r12

I prefer the method in my FAQ for adding seperate files just because I can then edit the asm in the evc editor, and it will automatically reassemble it when it changes.
simonjacobs · 20 Feb 2002, 12:20 UTC
the c looks fine, you do have to compute the offset to the first pixel as you say, the only thing i would change is adding a test for the machine type. different machines will need different offsets to work.

my approach is to do all drawing to my own screen buffer then copy to display memory at the end. That is FAR faster on the iPaq than drawing directly to the screen, because the iPaq screen memory is not write-cached.
True, true, point taken... for this example.

I tend to optimise the stacking later in the process - a lot of the time I ignore the call standard conventions because I call some ARM functions only from ARM functions (I do a lot of ARM stuff largely because I'm more proficient in ARM than I am in C), later on in the process I know better what does and doesn't need to be stacked for each function, and can start eliminating excess stacking.

I always err on the side of caution to begin with and stack everything; if I called it from another ARM function, I might not expect it to corrupt the first four registers - always expecting the first 4 registers and r12 to be corrupted for every ARM routine is wasteful if you're only ever going to call one or two functions from C.

Your other comment was what I meant when I said:
(you can include a macro to compile your *.s files elsewhere in you project - this has already been discussed somewhere on this board, IIRC).


:-)

Thanks for looking at the C. Sure, it needs to add a test for the machine type, etc, - it was just a quick example I knocked up to help the guy who started the thread.

My implementation uses double buffering at the moment, too. Whether it's faster to do so I haven't checked with my code - my code writes to the buffer in sequential order, but what it doesn't do is 24-pixel writes in a single instruction, which is more cache coherant than my STRs if nothing else.

Cheers,

Refractor.
simonjacobs · 20 Feb 2002, 12:52 UTC
fair enough :)

are you working on a game in assembler?

i'm busy trying to write a 3d engine, its taking a lot of effort.
I've been working on a 3d engine in dribs and drabs. At the moment I'm worked off my feet (not PocketPC related), *and* I've been offered another contract so I'm gearing up for the interview for that.. there just never seems to be enough time right now.

Essentially, yes, I'm working on several games (I never could just do one thing at once, but most of them share the same engine constituents):

a "micro-machines" clone with added 3d-ness (3d cars, obstacles, etc, but tiled-graphics thrown in too for the majority of the 'ground').

a parallax platformer game with *huge* levels (I actually started that one with the GBA in mind.. but the iPAQ will do just as nicely, if not better).. that one's stymied at the moment for technical reasons - real-time decompression of the tiled map is a sticky issue. Well, I know how to decompress it (very) efficiently.. but bizarrely it's the *compression* routine I'm stuck with (it's non-trivial to say the very least). I know how I want to compress it (in a weird way :wink: )... I just don't know how to go about doing it algorithmically without running into travelling-salesman type issues (exponential growth of the function's time to run). I'm still attacking some research papers on the techniue though, so it may turn out to be ok in a couple of months.

...after I've either played with those enough, or released them (unfortunately, don't hold your breath), I'll be playing with getting a 3d terrain engine up... or a "fake" 3d platformer.

I know *how* I'm going to do a lot of it, but I just haven't had the time to sit down and do it... Due to the way I study things I'm probably 'missing' some rudimentary knowledge too so my 'know how' may turn out to be flawed. I won't know until I do it, yet I haven't got time to do it. Aaargh, the eternal dilemma. :-( It gets worrying when I'm annoyed at my girlfriend for arranging a one week ski holiday because I'll be without a real development environment for a *whole week*. :oops:

If you need any help with anything that I may be able to help with (general comments, ARM stuff), give me a yell - I'm always happy when people give me things to think about.

Cheers,

Refractor
wrecked · 20 Feb 2002, 13:49 UTC — terrain
Id like any info on how to build a terrain engine..i kinda know what you need but putting it altogether is the problem.

On another point...ive been asked to write a couple of games .. which is great , but ive been offered a profit share as payment...this is ok too, but do these things sell..is there any money in developing for the pocket pc or is it just a fun tool TO PLAY WITH..????/
Terrain engine details and some nice references here:
http://www.gamasutra.com/features/20000403/turner_pfv.htm (you may need to register to read it, but I can highly recommend it and it's free). If you want to program games, there's an awful lot of useful information at GamaSutra.

I'm doing my games as a side-line hobby, 'cos I'm pretty sure I'll always earn more doing what I do than being a games programmer, *especially* on a platform that's not really designed for gamers.

If you want to make money then maybe it's worth considering targeting the ARM based Gameboy Advance... but the problem there is getting a publisher.

I'm messing about with mine simply 'cos I like (where's the 'fawning' smiley when you need one?) ARM processors. iPAQs, etc, make better toys than development boards with no screen and limited upgrade potential.

I would *guess* that the games that have sold the most are aimed at middle-aged businessmen with no lives (thus, Pocket Strip Poker, SimCity2000, boring damn Golf games).

Looking at Handango, it seems that SimCity2000 has been downloaded 29511 times ($30 shareware - no idea how many people actually registered, unfortunately)... which surprised me (very much!).
... and that's just from Handango.

Hrrrrm... maybe I should try and do more to my games after all! :-D

Cheers,

Refractor.
wrecked · 20 Feb 2002, 14:22 UTC — top one
Great info..top read..ROAM that is...MY DAY STARTED BAD AND HAS ONLY GOT BETTER..cheers for all the help
Moved and bumped (only took 15 months to get around to it).

Jacco, I checked and you have admin access for the Overloaded forum now.

Simon, I edited two of your posts to fix formatting that was mangled when the board was converted to phpBB.

Dan East
Oooh, a Spanish language, members only eGroup with 8 posts total (in 6 months). How useful. :P
Hi simonjacobs,
i have used cut and paste the 'myfile.asm' you jave posted earlier on and adjusted the project settings acordingly but couldnt get it to compile. May i know what have i miss out?? Btw, i created a new project in eVC++ 3.0 to test compiling the 'myfile.asm'. Thanks
couldnt get it to compile


You're going to have to give more detail if you want help (like error messages).
I wuv you!!
refractor wrote:Firstly, you can't (easily) embed ARM assembler in EVC++ (which *stinks*).

Hi Refractor, sorry for quoting you from so long ago ;).

By "(easily)" in your text above, did you mean that there is a "hard" way to inline or embbed some asm in eVC? If so, how??

Thanks!

--Eric
chuawenching · 21 Oct 2003, 11:19 UTC
Why do we need ARM assembler for if we can use embedded visual c++?

Actually what is ARM? The microprocessor for pocket pc?

I am a total noob in pocket pc.

Thanks.
C and C++ languages use compilers that produce assembly source code. An assembler turns that into native binary understandable by the target processor. Yes, all modern Pocket PCs use chips based on the ARM family. While C / C++ compilers usually do a good job converting their high level language into assembly, an experienced assembly programmer can almost always generate ASM code that will run faster.

Dan East
chuawenching wrote:
Why do we need ARM assembler for if we can use embedded visual c++?

Actually what is ARM? The microprocessor for pocket pc?



ARM is the name of the architecture of the processors found in most PocketPC devices.

Both StrongARM and Xscale processors uses the ARM instruction set.

Assembly language is necessary when you need maximum speed, you could expect 50~100% speed gain over an optimised C code, but it depends on the complexity of the operation and how good the compiler is for that operation.
flaixred · 29 Oct 2003, 20:51 UTC — Small site
I have this small site which have some resources where to find more about ARM assembly. Hope this helps you out. Here is the url http://assembly.ath.cx for the site. Its quite new site, and I havent got any feedback yet so feel free to say ;>

-flaixred
just another question for the faq ;)

do you know of any resources explaining how macros work? an evc armasm manual link would be great ;)

----

answering to myself:

http://www.cs.umass.edu/~bob/cs201/armsw/refMan3.pdf
hm · 07 Apr 2004, 02:59 UTC — ARM System Developer's Guide
Just received my copy of the new book

ARM System Developer's Guide : Designing and Optimizing System Software
by Andrew Sloss (Author), Dominic Symes (Author), Chris Wright (Author)
# Hardcover: 704 pages
# Publisher: Morgan Kaufmann; (March 26, 2004)
# ISBN: 1558608745

I found it quite unique in that it really has a focus on software and on writing compilers and other system software targeting ARM. Has specific discussions on stuff like caching, efficient arithmetic, scheduling and register allocation. So it kinda fills the gap between "Hacker's Delight" and Muchnick.

Anyway, just wanted to share this.

- HM
ChavoDelOcho · 01 Jun 2004, 13:39 UTC — Newbie wants help to set some registers in Xscale
Hi, I´m a newbie (everyone has to be someday...), and im having some difficult to find out how to change some values in some ARM Xscale registers, to be more specific, my problem is to make a frequence changing routine (deals with SDRAM Clock registers, Core Clock Configuration Register and Clock Enable Register). I found some code in assembly but I think that I must first obtain the supervisor mode status (do i?), and i dont know how to get it... To be honest, I was looking for some ways to change those registers in the c code in order to make a c routine...

Can anyone explain me what and how do I have to do to get supervisor mode status, and if that way I can change those registers?
Or just tell me how to change those things in C code?

Thanks!!
I just wanted to add this resource is excellent! Clear asm module explanation and arm asm links all in one place. I've literally been looking for this stuff for weeks...apparently I'm a bad searcher because it took me this long to find pocketmatrix :lol:
The gameboy advance also uses an arm processor (16mhz ARM7TDMI) and there are many files and demo source codes available that could be used on an arm based pocket pc. Much of the code is tailored for gcc or ads toolchains, but there is a wealth of knowledge in the gba development scene that can benefit pocket pc developers. Here's a few links:

http://gbadev.org
http://devrs.com/gba
http://pdroms.com - roms website, some roms come with code
http://darkfader.net/gba

GP32 and Zodiac devices are ARM9 based, there should be more code available for those as well.
subbu · 05 Apr 2006, 08:29 UTC — Problem in importing a C function in ARM assembly code
Hello,

You article up on how to go about mixing assembly with C was well written.

I have written a similar ASM file which successfully exports functions to C but I am unable to export a C function to the assembly file using the IMPORT command in assembly.

The error comes up as an unresolved signal "timer1_c_irq_handler".

My code in the assembly file is:

EXPORT timer1_asm_irq_handler
IMPORT timer1_c_irq_handler

;<A bunch of assembly statements to set the register values>

ldr r1, =timer1_c_irq_handler
mov r14, pc
bx r1

;<a bunch of assembly statements to return the status of all registers>

IRQ_EXIT
END

In my C code, the timer1_c_irq_handler is just defined - i.e.
void timer1_c_irq_handler(void)
{
<i fill in my processing statement here>
}

Is there something I am missing?

Please advise,

Subramanian

simonjacobs wrote:This is a FAQ for ARM assembler programming on the Pocket PC.

Maintained by Simon Jacobs

Version 1.0 (22'nd Jan 2002)

I have done my best to make this accurate and useful, but I make no guarantees. Remember that assembler programming is complex and if you are unsure of yourself you should ask for help. Please email me if there are any questions you would like to see added to the list.


--------------------------------------------------------------------------------

1) Is there a good book about ARM assembler?
2) Can I use inline assembler in my c code with Embedded Visual C++?
3) Can I see what assembler code the c compiler creates?
4) How do I add an assembley source file to my project, and get EVC to assemble it?
5) How do I write a function in assembler and call it from the c code?
6) How are values passed to and from the assembler function?
7) Can I use any of the 16 registers for anything?
8) Useful resources.


--------------------------------------------------------------------------------

1) Is there a good book about ARM assembler.

ARM System-on-chip architecture (second edition)
Steve Furber
Addison-Wesley

As well as some chapters aimed more at hardware designers, it has an explanation of the ARM instruction set plus a complete reference of the assembler format for each one. It doesnt teach programming in any way, but if you are experienced in c programming and just need full details of the instruction set and info on how the processor works it is great.

2) Can I use inline assembler in my c code with Embedded Visual C++?

There is no supported way of doing this. You need to put your assembler in seperate functions which can be called from your c code. See below for details of how to do this.

3) Can I see what assembler code the c compiler creates?

Yes, and this is very useful for a few reasons. You can see how optimal the code being produced is, and also learn how the compiler does things. You need to:

From the 'Project' menu choose 'Settings'. Go to the 'C/C++' tab. In the 'Category' drop-down-list choose 'Listing Files'. In the 'Listing file type' drop-down-list choose 'Assembley with Source Code'.

Now the compiler will produce .asm files in the output directory which contain the assembler code with the c source as comments.

4) How do I add an assembley source file to my project, and get EVC to assemble it?

Create a blank file 'myfile.asm' in your project directory with notepad. From the menu in EVC select 'Project->Add to Project->Files' set the 'Files of type' field to 'All Files' and select the myfile.asm file. It will appear in the file list on the left, drag it into the 'Source Files' section.

Select 'Project->Settings' from the menu. On the left of the dialog: select the 'ARM release' configuration and select the 'myfile.asm' file. On the right of the dialog: select the 'Custom Build' tab. In the 'Commands' box add the text 'armasm myfile.asm' and in the 'Outputs' box add the text 'myfile.obj'. Repeat this for the 'ARM debug' configuration.

Now you can open and edit myfile.asm from EVC and it will be assembled into your project.

5) How do I write a function in assembler and call it from the c code?

First a note about the assembler source code layout. All command lines should begin with a tab. Any lines that have text on the left are labels. You need to use labels for function names and as targets for branch instructions. Use a semicolon to mark the rest of a line as a comment. The file should finish with an END command.

Example file:
Code:

AREA ArmGfxLib,CODE,READONLY
EXPORT FunctionA
EXPORT FunctionB

FunctionA
MOV r12, sp
stmfd sp!, {r4-r11,lr}

; useful code to do something goes here!

ldmfd sp!, {r4-r11,pc}

FunctionB
MOV r12, sp
stmfd sp!, {r4-r11,lr}

; useful code to do something goes here!

ldmfd sp!, {r4-r11,pc}

END

Explanation: the AREA command gives a name to this code section. The EXPORT command makes those labels available as functions. The stmfd/ldmfd commands save and restore the registers to the stack, so you dont corrupt values that the calling function depends on.

In your c++ code add the line:

#ifdef ARM
extern "C" void FunctionA(int a, int b);
#endif

The #ifdef is because we have written the function in ARM assembler so it isnt available in MIPS/SH3 builds.

6) How are values passed to and from the assembler function?

If you declare a function of the form:

extern "C" int armfunction(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6);

The first 4 arguments will be passed in the registers r0-r3. These registers will contain the passed values at the start of your assembler function. Further arguments are passed on the stack, see the code below to read them.

The return value should be placed into register r0 at the end of your assembler function (just before you return to the caller).

This works well with int's and pointers (all pointers are 32 bits). If you try and pass a structure directly as an argument it gets more complex. I advise you to only pass/return integers and pointers to/from your assembler functions, at least to start with.

Here is an example function (declared as above) which adds its six arguments and returns the result.
Code:

armfunction
MOV r12, sp ; these two lines are
stmfd sp!, {r4-r11,lr} ; standard setup code

; r0-r3 are the first 4 arguments passed

ldr r4, [r12] ; load 5'th argument into r4
ldr r5, [r12, #4] ; load 6'th argument into r5

add r0, r0, r1 ; r0 = r0 + r1
add r0, r0, r2 ; r0 = r0 + r2
add r0, r0, r3 ; r0 = r0 + r3
add r0, r0, r4 ; r0 = r0 + r4
add r0, r0, r5 ; r0 = r0 + r5

ldmfd sp!, {r4-r11,pc} ; standard cleanup and return code

7) Can I use any of the 16 registers for anything?

No. You can use r0-r12 and r14 for anything you like. r15 is the program counter, if you write a value to it then execution will jump to that location. Use the branch instruction, dont alter it directly. r13 is the stack pointer, do not alter it unless you are sure you know what you are doing. r14 (which you can use) is the link register. If you want to call another function you need to set this to the return address, I will include an example in a future version of this FAQ.

8) Useful resources.

  • <a href="http://www.ukscene.org/sulph/">sulphur's gba development site</a>

  • <a href="http://www.intel.com/design/strong/manuals/">Intel StrongARM Manuals</a>

  • <a href="http://www.geocities.com/wonglinhoo/Arm.htm">The Arm Microprocessor</a>

  • <a href="http://www.soup-kitchen.net/armcode/">Soup Kitchen - Arm Tutorial</a>

  • <a href="http://www.heyrick.co.uk/assembler/">ARM Assembler Programming</a>

  • <a href="http://www.ee.ic.ac.uk/pcheung/teaching/ee2_computing/">ARM Assembly Lecture Notes</a>

  • <a href="http://wheelie.tees.ac.uk/users/a.clements/ARMinfo/ARMnote.htm">The ARM Processor</a>

  • <a href="http://re-eject.gbadev.org/">ARM Quick-Instruction reference sheet</a>

  • <a href="http://www.gbadev.org/">gameboy advance development</a>
Forth has an assembler embedded in the kernel . create code , linked into a dictionary in seconds . Forth boots an ARM cpu , it prompts with a debugger to fix the boot code if it hangs .
But Free Forths , today ,are missing this good stuff !
Assembly is easy , if you can customise
it to eliminate the "rules" and grammar !
Forth allows you to change anything , anywhere . If you want to create 150 macros
to automate all your code , it takes imagination and 15 minutes !
as fast as you can type , you are rewritting Forth itself ! Its for the
modular structure of Forth , making it so easy to figure how the kernel does everything . I have not found a simple , free Forth yet , ill give mine away free ,
but i have not yet started , need h/w and
a tiny Basic ,just enuf to boot and do low level peek/poke ...
Maybe MPE ARM-Forth works ....

No manuals needed , you learn from the keyboard , create short routines in seconds . 100's of free Forths , and they all are Obtused ! ANS Forth is old , obsolete and belabored ..
werty@swissinfo.org
When you see an expert use Forth , you'll
want it .
bitbank · 24 Mar 2007, 23:43 UTC — I've created a blog for this topic
I've written a few articles about ARM optimization for C and ASM authors. Please have a look at my blog:

http://bitbank.wordpress.com

Feel free to suggest other topics to cover.

Larry B.