【Lecture7】Computer Systems & Networks
SDK6800

Simple Loop

For Loopを使った条件式の例
bra start
.org $0010
// Define the varibles
num .byte 12
temp .byte 0
numFactors .byte 0
i .byte 0
.org $0020
// temp = num && i = num
start ldaa num
staa temp
staa i
.org $0030
// Test if i = 0, and jump to "here" if that was
myloop tst i
beq here
// temp = temp - 1
ldaa temp
suba i
staa temp
// Check if temp = 0, and jump to "saveFactor" if that was
tst temp
beq saveFactor
// Check if temp < 0, and jump to "skipFactor" if that was
bmi skipFactor
// Check if temp > 0, and jump to "nextFactor" if that was
bpl nextFactor
// i --
saveFactor dec i
// numFactors ++
inc numFactors
// temp = num (tempの値を初期化)
ldaa num
staa temp
// Go back to the loop
bra myloop
skipFactor dec i
ldaa num
staa temp
bra myloop
nextFactor bra myloop
// End the loop
here nop
bra stop
stop .end
Algorithm
start with a variable equal to 12
num = 12
temp = num
for each number i from 12 to 1:
temp = temp - i
if temp == 0:
then count i as a factor of num
i = i - 1
temp = num
jump back to top of loop
else if temp < 0:
then i can't be a factor of num
i = i - 1
temp = num
jump back to top of loop
else temp > 0:
jump back to top of loop
Multiplication
bra start
.org $0010
x .byte 7
y .byte 9
.org $0020
product .byte 0
.org $0030
start clra
myloop adda x
dec y
tst y
beq saveans
bra myloop
saveans staa product
bra stop
stop .end
7を足していき、それぞれのターンで9から1を引く。そうすることで、7*9 == 7 + 7+ …..+7 なので、答えが導き出せる。