I'm not sure where to post this since it's not actually bare metal in the sense that you don't need an OS to run, but on the other hand this is where most asm topics are.
I'm learning ARM assembly, never having done any assembly before and I made the "99 bottles of beer" program.
Here is my result:
- Code: Select all
.globl _start
_start:
mov r4, $99
loop:
ldr r0, =string1
ldr r1, =bottles
ldr r3, =plural
mov r2, r4
cmp r2, $1
addeq r3, $1
bl sprintf
sub r4, $1
ldr r0, =string2
ldr r1, =bottles
ldr r3, =plural
mov r2, r4
cmp r2, $1
addeq r3, $1
bl sprintf
ldr r0, =lyric
ldr r1, =string1
ldr r2, =string2
bl printf
cmp r4, $0
bne loop
mov r0, $0
bl exit
lyric:
.asciz "%1$s on the wall, %1$s.\nTake one down, pass it around, %2$s on the wall.\n\n"
bottles:
.asciz "%d bottle%s of beer"
plural:
.asciz "s"
.data
string1:
.space 19
string2:
.space 19
This needs Linux to run (tested on Raspbian). It would probably work on other OSes too since there is nothing specific to Linux here, just needs a c library.
To compile, do the following:
- Code: Select all
as -o beer.o beer.s
ld -dynamic-linker /lib/ld-linux.so.3 -o beer beer.o -lc
Since I'm learning, any comments on how to improve this are welcome