gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

ASM > ASM Examples for beginners

#100692 - Mr.Modem - Tue Aug 29, 2006 8:09 pm

Hi, everyone!
I have just decided to learn assembler of some sort. I chosed Gba because it's a powerful machine that seems to be quite easy to begin with. I consider myself to be a rather good C programmer but I have never done any asm before. So I need some advices from you guys.

I have read some tutorials including that gbaguy tutorial and have done some experimenting on my own. But It's hard for me to do stuff on my own because I have never done any assembler before. Do you have any recommendations on good stuff to read to learn the basic "syntax"? I would also appreciate if you can show me where I can download some well-documented asm example source code that I can learn from. Preferably something very easy to start with. It's much easier to learn how to use all the opcodes if you can see how they work, instead of just looking them up in some ARM reference.

#100696 - gmiller - Tue Aug 29, 2006 8:40 pm

You can think of learning an assembly language as three distinc steps.

Step 1: Lean the language mnemonics (op-codes) and the syntax associated with each one. Learn the register names and how they are used.

Step 2: Learn the assembler pseudo ops that are not associated with the language but with the assembler (and they can be different for the same machine with different assemblers)

Step 3: Learn the function calling conventions and return rules so you can write your own functions.

Now there are more step but with these you can get pretty far. The ARM instrucction documents that are referenced in the sticky threads can hel with step 1.

For step two you need to pick an assembler ... I use the gas assembler from the devkit Pro kit but there are other choices and maybe better. I use gas because I write in C/C++ and the gcc compilers will call gas if it see's a .S or .s suffixed file. The manual can be found here http://www.gnu.org/software/binutils/manual/gas-2.9.1/as.html.

For step 3 you will need to read the ATPCS (ARM-Thumb procedure call standard which is the standard that is used by C compilers to generate code for the ARM. I found it here http://www.soe.uoguelph.ca/webfiles/rmuresan/ATPCS.pdf#search=%22ATPCS%22

I realize that this does not constitue all that you were looking for but it is a start. Also realize that others will be shocked and dismayed that I use gas and not someother assembler. But what the heck, I still use the vi editor most of the time.

#100731 - keldon - Wed Aug 30, 2006 4:37 am

If you can learn algorithms then you will always find problem solving much easier.

#100738 - DekuTree64 - Wed Aug 30, 2006 5:33 am

Try this for starters, in an otherwise empty .S file:
Code:
.text         @ Put the code in the .text section
.arm          @ 32-bit ARM code
.align 2      @ Align to 4 bytes (2 means 1<<2, which is 4)
.global Foo   @ Make Foo a global symbol that other files can reference
Foo:
stmfd sp!, {r4-r11, lr}

@write code here

ldmfd sp!, {r4-r11, lr}
bx lr

That saves all registers that C code expects you not to corrupt onto the stack, and restores them before returning to the caller. That way, you're free to do whatever you want will all regs except r13, which is the stack pointer (sp and r13 mean the same thing to the assembler).

Then you can extern it in C, like
Code:
extern void Foo();

and call it like a normal function. If you add arguments to the extern, the compiler will put them into regs r0-r3 when calling the function, so you can use them. More than 4 arguments will start putting stuff on the stack, which is a bit complicated, so keep it 4 or less for now.

If you make the return type non-void, then the calling C code will expect the return value to be in r0 when your function exits.

Hopefully that will give you enough to get started. The instruction set is very straightforward, so learning the syntax shouldn't take too long just using a reference manual and trying them out.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#100899 - Mr.Modem - Thu Aug 31, 2006 3:29 pm

Sorry for the late reply but my internet connection have been a little weird lately. Your advices were very helpful to me. I would be very happy though If someone could point me to a place were I can download gba asm source code. It's easier to understand how things are done in practic if you see an example.

#100921 - keldon - Thu Aug 31, 2006 8:12 pm

Mr.Modem wrote:
Sorry for the late reply but my internet connection have been a little weird lately. Your advices were very helpful to me. I would be very happy though If someone could point me to a place were I can download gba asm source code. It's easier to understand how things are done in practic if you see an example.


Some things, but coding isn't as simple as that. You MUST learn algorithms otherwise you will have a hard time learning things and coding in general.

#101745 - HotChilli - Thu Sep 07, 2006 8:57 pm

keldon wrote:
Mr.Modem wrote:
Sorry for the late reply but my internet connection have been a little weird lately. Your advices were very helpful to me. I would be very happy though If someone could point me to a place were I can download gba asm source code. It's easier to understand how things are done in practic if you see an example.


Some things, but coding isn't as simple as that. You MUST learn algorithms otherwise you will have a hard time learning things and coding in general.


Algorithms is very very useful. Yeah. But asm gives opportunity to understand and make better some things. I.e. fast copy to VRAM or RAM. Low-level math optimization. And so on.

PS: "We will see asm source code! We will see asm source code!" =)

#101775 - keldon - Fri Sep 08, 2006 12:09 am

Here is some GB asm tutorials with a javascript asm scripting language (click and see what I mean).

http://www.dkauk.com/klogic/school.htm