Implementing simple sort algorithms in ARM Assembly (part 1)

A while back I started to learn some ARM assembly on the Raspberry Pi (out of curiosity, for no other better reason). I thought it would be interesting to couple this with re-learning some of the basic/standard/common algorithm at the same time, such as common sort algorithms.

So as my first step, since this is turning out to be far more work than I expected (!), here’s my ARM ASM source so far to iterate through a list of 4 byte integer values and print the values to the console using C’s printf. I’ll post further updates as I make progress:

[code]
.global main
main:
push {ip, lr}
MOV R6, #0 @offset to data
loop:
LDR R0, =output @load addr of output string
LDR R5, =nums @ addr of string to R5
LDR R4,[R5,R6] @load current num from R5 with offset R6
MOV R1,R4 @move num for output
BL printf
CMP R6,#16 @ 0 plus 4*4bytes for 5 entries in array
ADD R6,R6, #4 @inc offset by 4 bytes
BNE loop
_exit:
POP {ip, lr}
MOV R1, #0
MOV R7, #1
SWI 0
nums:
.word 5,2,7,1,8
.data
output:
.asciz "%d\n"
[/code]

I’m sure there’s better ways I can approach this limited code so far, but I’ll come back and revisit this again later. If anyone wants to pull or browse the source so far (and other snippets), it’s on github here: https://github.com/kevinhooke/learning-arm-asm

Cross-compiling ASM using Eclipse on Windows for ARM on the Raspberry Pi

Wow. Let me start by saying getting the right combination of tools setup to do this took more time than learning and writing my first few lines of ARM assembly.

Each of the links below has install and config instructions, so I’m not going to cover all of these again here, but here’s the combination of tools that worked for me:

An alternative toolchain could be the GNU Tools for ARM Embedded, but I couldn’t get my (simple) assembly to compile without unfathomable errors using this one (I tried this first before I tried the Sorcery toolchain).

Raspberry Pi ARM ASM syscall notes

A few notes on useful syscalls:

SWI 0 : executes instruction based on value in R7

Write to screen: R7 = 4

MOV R7, #4 @ Syscall 4 = write to screen
MOV R0, #1 @ stdout: move 1 to R0
MOV R2, #4 @ length of string to R2
LDR R1, =string
SWI 0
string:
    .ascii "testn"

Read from keyboard: R7 = 3

MOV R7, #3 @ Syscall 3 = read from keyboard
MOV R0, #0 @ stdin?: move 0 to R0
MOV R2, #4 @ length of string to ready to R2
LDR R1, =string @ load R1 value to string location 
SWI 0
string:
    .ascii ""