What does this test ASM ARM code (for GBA) exactly mean? (Noob question)

Started by Mar99troid, May 25, 2018, 02:40:48 PM

Previous topic - Next topic

Mar99troid

Hello, I'm trying to learn ASM for the GBAtroids, but I have some difficulties understanding this test asm file so any help would be highly appreciated.  :^_^:
Would somebody be so kind and tell me what EXACTLY happens after every line of code. How do the registers change and what values do they hold.
For example write something like:

subs r2, r2, #1
@ r2 = r2 - 1
@ the value of r2 is now *insert number in here*


Also make a clear distinction between the address of a register and the number the register itself holds, because that confuses me as well and people just write r0 is something in online tutorials and I never know if they mean the address or not.

Here's the example code. It gives out a crimson red screen when built into a .gba file:

.arm
.text
.global main
main:
mov r0, #0x4000000
mov r1, #0x400
add r1, r1, #3
str r1, [r0]
mov r0, #0x6000000
mov r1, #0xFF
mov r2, #0x9600
loop1:
strh r1, [r0], #2
subs r2, r2, #1
bne loop1
infin:
b infin


Also I'd like to know why you can't just write mov r1, #0x403 and what the difference between ldr, str and mov is, because they all seem the same to me.  :lol:

If somebody would be so kind and example every little line of code to me, that would help me very much and improve my understanding of ASM so I won't need any help anymore when looking up new ASM instructions.

PS: I hope I posted this in the right place on the forum, because I didn't find an ASM thread or something like that.

Mar99troid

Okay, I think I'm starting to understand this code a little more, but I'll still need help.
I found out that the address at the first line in the main points to the display control register, 0x400 defines BG2 and 0x403 (0x400 + 3) defines MODE 3 (a bitmap mode with a resolution of 240 by 160 (the GBA's native resolution) and 16-bit colors, but no double buffering).
I still don't really know why you can't just write
mov r1, #0x403
instead of
mov r1, #0x400
add r1, r1, #3

but I guess it has something to do with the binary values of hex 400 and hex 403 which are
0000 0000 0000 0000 0000 0100 0000 0000
and
0000 0000 0000 0000 0000 0100 0000 0011
apparently you can't create 0x403 by shifting up an 8-bit value, but I have no idea why it is necessary to shift the value in the first place.

But after hours of research on the web, writing new code and looking at the results with no$gba I think I FINALLY understand the following code. I wrote comments all over the place, so a noob like me can comprehend what the hell is happening. Please tell me if I made a mistake.

.arm @arm mode instructions
.text @the following will be put in the text/code section (ROM in the case of GBA)
.global main  @necessary, so the main function can be found
main: @start of main function
mov r0, #0x4000000
mov r1, #0x400
add r1, r1, #3

@LDR = LOAD FROM MEMORY (LOAD THE FIRST THING BEFORE COMMA, SECOND THING IS ADDRESS OF WHAT YOU WANT TO LOAD)
@STR = WRITES INTO MEMORY (INTO SECOND THING AFTER COMMA!)
@MOV = MOVES NUMBERS BETWEEN REGISTERS (INTO FIRST THING)

@ r0 = 03000404
@ r1 = 00000000
@ r2 = 00000003
@ r3 = 03000000
@ r4 = 02000000
@ cpsr = 4000003F
@r0 = 0x4000000 (Normal Number, not the address itself)
@r1 = 0x400 (Normal Number, not the address itself)
@ r1 = r1 + 3 = 0x403 (Normal Number, not the address itself)
@at offset 0x4000000 it says 00080h,00000h,000004h,00000h

strh r1, [r0]

@ what is in r1 is written to the address of r0 (0x4000000)
@r0 = 04000000
@r1 = 00000403
@...
@at offset 0x4000000 it says 00403h,00000h,000004h,00000h

mov r0, #0x6000000 @address of VRAM (Video Random Access Memory, all the pixels displayed are saved here)
mov r1, #0xFF    @red color
mov r2, #0x9600    @the amount of writes it'll take to fill the screen (resolution of GBA: 240*160 = 38400 is 9600 in hex)

loop1:
strh r1, [r0], #2
@first: halfword (16bit/2byte value) of r1 (0xFF) into the address of r0, then r0 = r0 + 2 (not the address of r0, r0 itself!)

subs r2, r2, #1
@r2 = r2 - 1
@the s after SUB means to set the flags in CPSR based on result

bne loop1
@ if an instruction is not zero (in this case r2) --> continue ; else --> break
infin:
@an infinite loop
b infin


@Registers after the loop is finished:
@r0 = 06012C00
@r1 = 000000FF
@r2 = 00000000
@r3 = 08000228
@r4 = 02000000
@ cpsr = 6000001F

Cpt.Glitch

The GBAtroids are mostly written in the thumb subset of arm. In fact, most if not all GBA games are this way. So for the purposes of witting asm for GBAtroids, you'll want to focus on thumb.

Mar99troid

Alright, thanks. Where can I learn more about ARM THUMB specifically? Could you provide some links please, because it's difficult to find the right tutorial online.

Cpt.Glitch

The resources thread in the fusion subforum has all the links you'd need. Pay special ateention to gbatek and the ram and rom maps. Also look at some of my and others asm patches for the GBAtroids to help you. That is how I started out. I'll be glad to answer any specific questions related to instructions and the GBAtroids in PMs or discord! :)

Also read this for some juicy yet condensed info on ARM and THUMB structure.