Example of a Program Written in the SPARC 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.
/* Rectangle: Program that computes the perimeter and area of a rectangle
from coordinates of two points (X1,Y1) and (X2,Y2).
Author: Richard St-Denis, Universite de Sherbrooke, 2013.
*/
.global Rectangle ! entry point
.section ".text" ! code section
Rectangle:
mov 2,%l0 ! two points must be read
setx points,%l7,%l1
rec00:
setx ptfmt1,%l7,%o0 ! 1st: address of the prompt message
mov 3,%o1 ! 2nd: point number = 3 - %l0
sub %o1,%l0,%o1
call printf ! display the prompt message for X coordinate
nop
setx scfmt1,%l7,%o0 ! 1st: address of the format control string
mov %l1,%o1 ! 2nd: address of the X coordinate
call scanf ! read the X coordinate of a point
nop
setx ptfmt2,%l7,%o0 ! 1st: address of the prompt message
mov 3,%o1 ! 2nd: point number = 3 - %l0
sub %o1,%l0,%o1
call printf ! display the prompt message for Y coordinate
nop
setx scfmt1,%l7,%o0 ! 1st: address of the format control string
inc 4,%l1
mov %l1,%o1 ! 2nd: address of the Y coordinate
call scanf ! read the Y coordinate of a point
nop
inc 4,%l1
dec %l0
brnz %l0,rec00 ! Another point to read ?
nop
setx points,%l7,%l1
ldsw [%l1],%l2 ! point 1 (X1, ...)
ldsw [%l1+4],%l3 ! point 1 (... ,Y1)
ldsw [%l1+8],%l4 ! point 2 (X2, ...)
ldsw [%l1+12],%l5 ! point 2 (... ,Y2)
sub %l2,%l4,%l6 ! X1 - X2
sub %l3,%l5,%l7 ! Y1 - Y2
brgez %l6,rec05
nop
neg %l6 ! |X1 - X2|
rec05:
brgez %l7,rec10
nop
neg %l7 ! |Y1 - Y2|
rec10:
mulx %l6,%l7,%o2 ! calculate the area
add %l6,%l6,%l6 ! calculate the perimeter
add %l7,%l7,%l7
add %l6,%l7,%o1
setx ptfmt3,%l7,%o0 ! 1st: address of the format control string
call printf ! display the results
nop
clr %o0 ! 1st parameter
call exit ! program exit
nop
/* Data definitions */
.section ".bss" ! uninitialized data section
.align 4
points: .skip 4*4 ! X1, Y1, X2, Y2
.section ".rodata" ! read only data section
scfmt1: .asciz "%d"
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 Solaris, the following commands allow for assembling, link-editing and execution of the program.
echo "Assembling ..."
/usr/ccs/bin/as -xarch=v9 rectangle.as -o rectangle.o
echo "Link-editing ..."
/usr/ccs/bin/ld -e Rectangle -o rectangle rectangle.o -lc
echo "Execution ..."
rectangle