Example of a Program Written in the ARM Assembly Language
The following program computes the perimeter and area of a rectangle from coordinates of two points, which are opposite vertices of the rectangle.
.global main @ entry point
/* rectangle : Program that computes the perimeter and area of
a rectangle from coordinates of two points (X1,Y1)
and (X2,Y2).
Auteur : Richard St-Denis, Universite de Sherbrooke, 2015.
*/
.section .text @ code section
main:
mov v1,#-2 @ two points must be read
ldr v2,=points
rec00:
ldr a1,=ptfmt1 @ 1st: address of the prompt message
add a2,v1,#3 @ 2nd: point number = 3-|v1|
bl printf @ display the prompt message for X coordinate
ldr a1,=scfmt1 @ 1st: address of the format control string
mov a2,v2 @ 2nd: address of the X coordinate
bl scanf @ read the X coordinate of a point
ldr a1,=ptfmt2 @ 1st: address of the prompt message
add a2,v1,#3 @ 2nd: point number = 3-|v1|
bl printf @ display the prompt message for Y coordinate
ldr a1,=scfmt1 @ 1st: address of the format control string
add v2,v2,#4
mov a2,v2 @ 2nd: address of the Y coordinate
bl scanf @ read the Y coordinate of a point
add v2,v2,#4
adds v1,v1,#1
bne rec00 @ Another point to read?
mov v1,#0
ldr a1,=points
ldr v2,[a1] @ point 1 (X1, ...)
ldr v3,[a1,#8] @ point 2 (X2, ...)
subs v4,v2,v3 @ X1-X2
submi v4,v1,v4 @ |X1-X2|
ldr v2,[a1,#4] @ point 1 (..., Y1)
ldr v3,[a1,#12] @ point 2 (..., Y2)
subs v5,v2,v3 @ Y1-Y2
submi v5,v1,v5 @ |Y1-Y2|
mul a3,v4,v5 @ calculate the area
add v4,v4,v4 @ calculate the perimeter
add v5,v5,v5
add a2,v4,v5
ldr a1,=ptfmt3 @ 1st: address of the format control string
bl printf @ display the results
mov a1,#0 @ 1st: return code
bl exit @ program exit
.section .bss @ uninitialized data section
.align 4
points: .space 4*4 @ X1, Y1, X2, Y2
.section .rodata @ read only data section
scfmt1: .asciz "%u"
ptfmt1: .asciz "Enter the X coordinate of point %u: "
ptfmt2: .asciz "Enter the Y coordinate of point %u: "
ptfmt3: .ascii "The perimeter is: %u\n"
.asciz "The area is: %u\n"
Under Ubuntu, the following commands allow for assembling, link-editing and execution of the program on a PandaBoard.
echo "Assembling ..."
as rectangle.asm -o rectangle.o
echo "Link-editing ..."
gcc rectangle.o -o rectangle
echo "Execution ..."
./rectangle
Under Ubuntu 4.6.3, the following commands allow for assembling, link-editing and execution of the program on a PandaBoard by avoiding the use of gcc.
echo "Assembling ..."
as rectangle.asm -o rectangle.o
echo "Link-editing ..."
ld rectangle.o -o rectangle
echo "Execution ..."
./rectangle
where ld is an alias for the following command, which has been derived from the compiler gcc with the option -v.
alias ld='/usr/lib/gcc/arm-linux-gnueabihf/4.6/collect2
-dynamic-linker
/lib/ld-linux-armhf.so.3
/usr/lib/arm-linux-gnueabihf/crt1.o
/usr/lib/arm-linux-gnueabihf/crti.o
-L/usr/lib/arm-linux-gnueabi/4.6.3
-lc /usr/lib/arm-linux-gnueabihf/crtn.o'
The alias can be inserted in the .bashrc file.