Back
Next
SPARC (32 bit)
! Note that syntax is opcode, source, destination
! In example, arr represents an assembler label for a 32 bit memory address
! which is start of an array of values.
start: sethi %hi(arr),%r2 ! Set high 22 bits of %r2
! to memory address labelled "arr"
or %r2,%lo(arr),%r1 ! OR with lower 10 bits of arr's address
! for full address
! %r1 now holds full address of arr
!
! Initialize %r3 and %4 to zero, $r0 = 0 always
mov %r0,%r3 ! %r3 will be an index register
mov %r0,%r4 ! Use %r4 to hold sum
set 20,%r5 ! %r5 counter, set to specified immediate value.
loop: ld [%r1+%r3],%r6 ! (indirect, indexed) fetch the next element
! and store it in register 6
add %r4,%r6,%r4 ! add contents of sum and retrieved value and
! restore in sum
! Implies that actual math done in an unnamed
! accumulator register and explicitly moved back.
!
subcc %r5,1,%r5 ! decrement counter register
bne loop ! if %r5 > 0 get next element
add %r3,4,%r3 ! increment the index (DELAY SLOT)
! executed whether loop taken or not.