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

Calling C printf from ARM ASM

A while back I started to learn some ARM assembly on the Raspberry Pi. In my previous snippets, I was using SWI to do a system call to write to stdout, but it appears syscall 4 to write to stdout only writes Strings – if you have a byte value, this approach doesn’t work (without converting to a String first?)

From this question here, it appears you can call C functions like printf, which seems like cheating a bit, but I guess it gets the job done.

Here’s a quick snippet to add two numbers, and then print the result using printf with a string containing %d to substitute the result into the String:

[code]
.global main
.extern printf
main:
push {ip, lr}
mov r3, #1 /* move 1 to r3 */
mov r4, #2 /* move 2 to r4 */
add r4, r3, r4 /* r4 = r3 + r4 */
_ouput:
ldr r0,=output /* load address of output string */
mov r1, r4 /* move r4 result to r1 to include as param in string */
bl printf /* call printf */
_exit:
MOV R1, #0
MOV R7, #1
SWI 0
.data
output:
.asciz "Result: %d\n"
[/code]

Where previously I was compiling and linking in two steps with as and ld, it seems this doesn’t work if you are referencing C functions too, so for this example, compile with: ‘gcc add.s -o add’

This snippet and a few others I’ve started to collect I’ve shared in a github repo 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).

The legacy of the BBC Micro computer is all around us… literally everywhere

If you were interested in computers in the 1980s in the UK, then you were aware and maybe even owned a BBC Micro computer. Outside of the UK, the BBC Micro never saw wide success, but in the UK it was widely used for computer science education in schools and was also popular as a home computer. Originally the BBC Micro was developed as part of the BBC’s Computer Literacy Project, and was showcased on the BBC TV show ‘The Computer Programme’, used as the demo hardware to teach viewers of the show how to use a home computer and program in BASIC.

After the BBC Micro, Acorn, the company behind the BBC Micro, went on to design a RISC based CPU architecture that was used in the Acorn Archimedes computer. This machine was the first home computer based on a RISC architecture, and was the first computer based on Acorn RISC Machines (ARM) CPU architecture. The Archimedes was first sold in 1987. ARM Holdings, conceived by and spun off from Acorn was born.

Fast forward to the present day, devices containing ARM based processors are literally everywhere, thanks especially to the success of the Apple iPhone, iPad, and iPod – all the most recent devices all are based around an ARM CPU. Prior to the Apple device success, ARM CPUs have been used in many mobile phones, tablets, PDAs, as well as being used in many other electronic devices.

The Apple A4 CPU was introduced in the original iPad, and then later reused in the iPhone 4, the 4th gen iPod Touch, and second gen Apple TV. The A4 is based on the ARM Cortext A8 CPU. Later generations of Apple devices have also been based on ARM CPUs. The Apple A5 CPU is based on the ARM Cortex A9, and powers the iPad2, iPhone 4S, iPad (3rd gen) and the iPod Touch (5th gen).

Most recent Apple devices are powered by the Apple A6 and A6x, based on ARMv7 dual core CPU.

In case you’re thinking the widespread success of ARM powered devices is limited to Apple devices, ARM CPUs also form the basis of Nvidida Tegra chipsets – the Tegra 2 System-on-a-Chip is based on ARM Cortext A9 (yes, also the basis of the Apple A4 CPU) and is used in many highend Android devices (Motorola Atrix, Droid X2 and Photon phones, Xoom tablets, Acer Iconia tablets to name a few).  The more recent Tegra 3 chipset, also based on ARM Cortex A9 but in quad-core configuration is used in the latest Android tablets, for example ASUS Transformer range and the Nexus 7, to name just a couple.

While it’s true to say much has obviously changed since the original days of the BBC Micro and the first ARM CPUs, I find it incredibly fascinating to realize that the heritage of today’s widely used consumer electronics devices can be traced back to origins in 1980s home and educational computing.