Loops/Downward for: Difference between revisions
Content added Content deleted
(added 8080 assembly example) |
|||
Line 107: | Line 107: | ||
DBRA D0,loop ;repeat until D0.W = $FFFF |
DBRA D0,loop ;repeat until D0.W = $FFFF |
||
rts</syntaxhighlight> |
rts</syntaxhighlight> |
||
=={{header|8080 Assembly}}== |
|||
This assumes the CP/M operating system. |
|||
<syntaxhighlight lang="asm"> |
|||
;------------------------------------------------------- |
|||
; some useful equates |
|||
;------------------------------------------------------- |
|||
bdos equ 5h ; location ofjump to BDOS entry point |
|||
wboot equ 0 ; BDOS warm boot function |
|||
conout equ 2 ; write character to console |
|||
;------------------------------------------------------- |
|||
; main code |
|||
;------------------------------------------------------- |
|||
org 100h |
|||
lxi sp,stack ; set up a stack |
|||
; |
|||
lxi h,10 ; starting value for countdown |
|||
loop: call putdec ; print it |
|||
mvi a,' ' ; space between numbers |
|||
call putchr |
|||
dcx h ; decrease count by 1 |
|||
mov a,h ; are we done (HL = 0)? |
|||
ora l |
|||
jnz loop ; no, so continue with next number |
|||
jmp wboot ; otherwise exit to operating system |
|||
;------------------------------------------------------- |
|||
; console output of char in A register |
|||
; preserves BC, DE, HL |
|||
;------------------------------------------------------- |
|||
putchr: push h |
|||
push d |
|||
push b |
|||
mov e,a |
|||
mvi c,conout |
|||
call bdos |
|||
pop b |
|||
pop d |
|||
pop h |
|||
ret |
|||
;--------------------------------------------------------- |
|||
; Decimal output to console routine |
|||
; HL holds 16-bit unsigned binary number to print |
|||
; Preserves BC, DE, HL |
|||
;--------------------------------------------------------- |
|||
putdec: push b |
|||
push d |
|||
push h |
|||
lxi b,-10 |
|||
lxi d,-1 |
|||
putdec2: |
|||
dad b |
|||
inx d |
|||
jc putdec2 |
|||
lxi b,10 |
|||
dad b |
|||
xchg |
|||
mov a,h |
|||
ora l |
|||
cnz putdec ; recursive call! |
|||
mov a,e |
|||
adi '0' ; make printable |
|||
call putchr |
|||
pop h |
|||
pop d |
|||
pop b |
|||
ret |
|||
;---------------------------------------------------------- |
|||
; data area |
|||
;---------------------------------------------------------- |
|||
stack equ $+128 ; 64-level stack to support recursion |
|||
; |
|||
end |
|||
</systaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
10 9 8 7 6 5 4 3 2 1 |
|||
</pre> |
|||
=={{header|8086 Assembly}}== |
=={{header|8086 Assembly}}== |