Counting Characters
Three versions of a program to count the number of characters in a character string. Exercise: Work through each of the versions to see what they do and write written descriptions flow charts.
Version 1
data equ $6000 program equ $4000
org data
start ds.l 1 length ds.w 1
cr equ $0d
org program
movea.l start, a0 moveq #0, d0
loop cmpi.b #cr, (a0)+ beq.s done addq.w #1, d0 bra loop
done move.w d0, length
rts end
Version 2
data equ $6000 program equ $4000
org data
start ds.l 1 length ds.w 1
cr equ $0d
org program
movea.l start, a0 moveq #-1, d0 moveq #cr, d1 loop addq.w #1, d0
cmp.b d1, (a0)+
bne loop
move.w d0, length
rts end
Version 3
data equ $6000 program equ $4000
org data
start ds.l 1 length ds.w 1
cr equ $0d
org program
movea.l start, a0 moveq #256-1, d2 move.w d2, d0 moveq #cr, d1
loop cmp.b d1, (a0)+ dbeq d0, loop sub.w d0, d2 move.w d2, length
rts end