data segment
prompt1 byte "input a string:",'$'
prompt2 byte "error!",'$'
string byte 100 dup (?)
num
word ?
outstring byte 5 dup (?),'$'
data ends
code segment 'code'
assume ds:data,cs:code
start:
mov ax,data
mov ds,ax
mov si,0
mov ah,9
lea dx,prompt1
int 21h
AGN:mov ah,1
;输入部分,遇回车退出输入
int 21h
cmp al,0dh
jz EXT
mov string[si],al
inc si
jmp AGN
EXT:mov cx,si
mov si,0
cmp string[si],'-'
;如果是负号就跳到ERR.输入"ERROR!"
jz ERR
mov bx,0
xor ax,ax
ATOD:mov al,string[si]
;依次取一个字符,然后转换为整数,加在BX里
sub al,30h
add bx,ax
inc si
loop ATOD
mov num,bx
;放在num里储存
jmp OUTP
ERR:mov dl,0dh
;输出ERROR!
mov ah,2
int 21h
mov dl,0ah
int 21h
lea dx,prompt2
mov ah,9
int 21h
jmp EXIT
OUTP:mov si,4
;输出部分
mov ax,num
mov bx,10
AGN1:xor dx,dx
;对num除10取余.然后逆序放入outstring变量里
div bx
or dl,30h
mov outstring[si],dl
dec si
cmp ax,0
jnz AGN1
mov dl,0dh
;输出回车换行后输出outstring里的内容
mov ah,2
int 21h
mov dl,0ah
int 21h
lea dx,outstring
mov ah,9
int 21h
EXIT:mov ah,4ch
int 21h
code ends
end start