print: pop dx cmp dx, -1 je exit add dl, '0' int 21h jmp print
exit: mov ah, 01h int 21h ret
d dw 10 string db "1", 0
FPC
var s : string; i, c : integer; begin readln (s); c := 0; for i := 1 to length (s) do if (s[i] in ['0'..'9']) then inc (c, ord (s[i]) - ord ('0')); writeln (c); end.
Fasm
org 100h
mov si, string
cld
my_loop:
lodsb
cmp al, 0
jz loop_exit
cmp al, '0'
jb my_loop
cmp al, '9'
ja my_loop
sub al, '0'
add bx, ax
jmp my_loop
loop_exit:
push -1
mov ax, bx
loop_push:
xor dx, dx
div [d]
push dx
cmp ax, 0
jne loop_push
mov ah, 2h
print:
pop dx
cmp dx, -1
je exit
add dl, '0'
int 21h
jmp print
exit:
mov ah, 01h
int 21h
ret
d dw 10
string db "1", 0
FPC
var
s : string;
i, c : integer;
begin
readln (s);
c := 0;
for i := 1 to length (s) do
if (s[i] in ['0'..'9']) then inc (c, ord (s[i]) - ord ('0'));
writeln (c);
end.
Например на ассемблере в синтаксисе fasm под дос:
org 100h
mov si,string
cld
mov cx,16
xor ax,ax
mov ah,02h
xor bx,bx
m1:
mov dl,[si]
push cx
mov cx,10
mov di,num
m2:
cmp dl,[di]
jnz m3
;int 21h
sub dl,30h
add bl,dl
m3: inc di
loop m2
pop cx
inc si
loop m1
xor ax,ax
mov al,bl
mov bx,10
xor cx,cx
m4:
xor dx,dx
div bx
push dx
inc cx
cmp ax,0
jnz m4
m5:
pop dx
add dx,30h
mov ah,2h
int 21h
dec cx
jnz m5
mov ah,01h
int 21h
mov ax,4C00h
int 21h
string db "1nr112t3brj9me18",0
num db "0123456789",0
Для строки "1nr112t3brj9me18" сумма будет равна 26.