[Asm] 纯文本查看 复制代码
stacksegment stack
db 100 dup (?)
stack ends
data segment
xsl db 'Input a string end with dollar char:', 13, 10, '$'
showop db 13, 10, 'Your input string is:$'
showop2 db 13, 10, 'It has $'
showop3 db ' chars.',13, 10, '$'
userstr db 100 dup(0)
data ends
code segment
assume cs:code, ds:data, ss:stack
start:
mov ax, data
mov ds, ax
lea dx, xsl
mov ah, 09h
int 21h
lea si, userstr
mov dx, si
xor dx, dx
callin:
call inputr
inc si
cmp ax, 0
je outp
jmp callin
;采用的计数方法:首先把地址复制到dx,每次读入字符串si偏移量+1,最后si-dx就是读入的字符串长度(+20...)
outp:
lea dx, showop
mov ah, 09h
int 21h
lea dx, userstr
mov ah, 09h
int 21h
lea dx, showop2
mov ah, 09h
int 21h
lea dx, userstr
mov cx, si
sub cx, dx
;sub cx, 20;不减20不对。这里是因为字符串头部还有一些东西?NONONO(因为之前的操作改变了dx)
mov ax, cx
call showint
lea dx, showop3
mov ah, 09h
int 21h
jmp fini
inputr proc
mov ah, 01h
int 21h
mov [si], al
cmp al, '$'
je ret0
mov ax, 1
ret
ret0:
xor ax, ax
ret
inputr endp
showint proc;显示数字函数
cmp ax, 0;0直接输出
jnz notzero
mov dx, '0'
mov ah, 02h
int 21h
jmp endshow
notzero:
xor cx, cx;cx表示位数,初始为0
pushtostack:
xor dx, dx;被除数是dx和ax的组合。。。不清空会溢出
mov bx, 10
div bx
add dx, '0'
push dx
inc cx
cmp ax, 0
jz popandshow
jmp pushtostack
popandshow:
mov ah, 02h
pop dx
int 21h
loop popandshow
endshow:
ret
showint endp
fini:
mov ah, 01h
int 21h
mov ah, 4ch
int 21h
code ends
end start