Hardware Reference
In-Depth Information
result
rmb
5
; reserve 5 bytes to store the result
org
$1500
ldd
data
; make a copy of the number to be converted
ldy
#result
ldx
#10
; divide the number by 10
idiv
;
addb
#$30
; convert to ASCII code
stab
4,Y
; save the least significant digit
xgdx
; swap the quotient to D
ldx
#10
idiv
addb
#$30
; convert to ASCII code
stab
3,Y
; save the second-least significant digit
xgdx
ldx
#10
idiv
addb
#$30
stab
2,Y
; save the middle digit
xgdx
ldx
#10
idiv
; separate the most significant and second-most
; significant digits
addb
#$30
stab
1,Y
; save the second-most significant digit
xgdx
; swap the most significant digit to B
addb
#$30
; convert to ASCII code
stab
0,Y
; save the most significant digit
end
2.6 Program Loops
Many applications require repetitive operations. We can write programs to tell the com-
puter to perform the same operation over and over. A finite loop is a sequence of instructions
that will be executed by the computer for a finite number of times; an endless loop is a se-
quence of instructions that the computer will execute forever.
There are four major loop constructs.
DO STATEMENT S FOREVER
This is an endless loop in which statement S is repeated forever. In some applications, we
might add the statement “If C then exit” to leave the infinite loop. An infinite loop is shown in
Figure 2.4.
F OR i 5 N 1 TO N 2 DO S OR F OR i 5 N 2 DOWNTO N 1 DO S
Here, the variable i is the loop counter, which keeps track of the current iteration of the loop.
The loop counter can be incremented (the fi rst case) or decremented (the second case). State-
ment S is repeated n 2 2 n 1 1 1 times. The value of n 2 is assumed to be no smaller than that of n 1 .
 
Search WWH ::




Custom Search