Sum of the digits of n is substring of n: Difference between revisions

Add 8080 assembly
(→‎{{header|BASIC}}: fix the bugs)
(Add 8080 assembly)
Line 4:
Find and show numbers n with property that the sum of the digits of n is substring of n, where '''n < 1000'''
<br><br>
 
=={{header|8080 Assembly}}==
<lang 8080asm>puts: equ 9
org 100h
lxi h,-1 ; Number
loop: inx h
push h ; Keep number
lxi d,-1000 ; Are we there yet?
dad d
pop d
rc ; If so, stop
push d ; Keep number
lxi h,buf0
call digits ; Get digits
push h ; Keep pointer to digits
call dgsum ; Sum digits
lxi h,buf1
call digits ; Get digits for sum
pop d ; Retrieve pointer to digits of original
push d
call find ; Does the original contain the sum of the digits?
pop d ; Retrieve digit pointer
pop h ; And number
jc loop ; If the sum of the digits is not found, try next
push h
call print ; Otherwise, print it
pop h
jmp loop
;;; Find digits of number in DE, store at HL.
;;; Beginning of string returned in HL.
digits: lxi b,-10 ; Divisor
mvi m,'$' ; String terminator
push h ; Output pointer on stack
digit: xchg ; Number in HL
lxi d,-1 ; Quotient
dgtdiv: inx d ; Trial subtaction
dad b
jc dgtdiv
mvi a,10 ; Calculate value of digit
add l
pop h ; Store digit
dcx h
mov m,a
push h
mov a,d ; Done?
ora e
jnz digit ; If not, find next digit
pop h ; Remove pointer from stack
ret
;;; Calculate sum of digits starting at HL
dgsum: lxi d,0
dgloop: mov a,m
cpi '$'
rz
add e
mov e,a
inx h
jmp dgloop
;;; See if the string at DE contains the string at HL
find: ldax d ; Load character from haystack
cpi '$' ; Reached the end?
stc ; Then it is not found
rz
push d ; Save pointers
push h
xchg ; Swap pointers
floop: ldax d ; Load character from needle
cpi '$' ; Reached the end?
jz found ; Then we found it
cmp m ; Compare to haystack
inx h ; Increment the pointers
inx d
jz floop ; If equal, keep going
pop h ; Restore pointers
pop d
inx d ; Try next position
jmp find
found: pop h ; Clean up stack
pop d
ret
;;; Print number
print: push d
ploop: ldax d
cpi '$'
jz pdone
adi '0'
stax d
inx d
jmp ploop
pdone: xchg
mvi m,13
inx h
mvi m,10
inx h
mvi m,'$'
pop d
mvi c,puts
jmp 5
buf0: equ $+32
buf1: equ $+64</lang>
{{out}}
<pre style='height:50ex;'>0
1
2
3
4
5
6
7
8
9
10
20
30
40
50
60
70
80
90
100
109
119
129
139
149
159
169
179
189
199
200
300
400
500
600
700
800
900
910
911
912
913
914
915
916
917
918
919</pre>
 
=={{header|ALGOL 68}}==
2,114

edits