回 帖 发 新 帖 刷新版面

主题:一个小程序啊,帮我一下

;从键盘输入一个数字N,然后计算1+2+....N,最后把结果输出
;我自己写的,可是有问题

data   segment
str1   db 'Input a number 1-9,please $'
sum    dw 0
data   ends
stack  segment stack
       db 200 dup(0)
stack  ends
code   segment
assume ds:data,ss:stack,cs:code
start: mov ax,data
       mov ds,ax
       mov ah,9
       mov dx,offset str1
       int 21h
       mov ah,1
       int 21h
       and al,0fh
       mov ah,0
       mov cx,ax
       mov ax,0
       mov bx,1
next:  add ax,bx
       inc bx
       dec cx
       jnz next
       mov sum,ax
       mov dl,sum
       mov ah,2
       int 21h  
       mov ah,4ch
       int 21h
code   ends
       end  start
[em10]

回复列表 (共1个回复)

沙发

我修改了一下,你试试

data   segment
str1   db 'Input a number 1-9,please $'
sum    dw 0
data   ends
stack  segment stack
       db 200 dup(0)
stack  ends
code   segment
assume ds:data,ss:stack,cs:code
start: mov ax,data
       mov ds,ax

       mov ah,9
       mov dx,offset str1
       int 21h

       mov ah,1
       int 21h
       and al,0fh

       mov ah,0
       mov cx,ax
       mov bx,0
       mov ax,1
next:
       add bx,ax
       inc ax
       loop next

       call crlf

       call  ShowDec
 //      mov dl,sum
 //      mov ah,2
 //      int 21h  
       mov ah,4ch
       int 21h

ShowDec  proc near
    mov cx, 10000d
    call Data_Div 
    mov cx, 1000d
    call Data_Div 
    mov cx, 100d
    call Data_Div 
    mov cx, 10d
    call Data_Div 
    mov cx, 1d
    call Data_Div 
    ret
ShowDec  Endp


Data_Div proc near
    mov  ax,bx
    mov  dx,0
    div  cx
    mov  bx,dx
    mov  dl,al
    add  dl,30h
        cmp  dl,'0'
    je   exit0
    mov  ah,2h
    int  21h
exit0:
    ret
Data_Div Endp

crlf  proc near
    mov dl ,0ah
    mov ah,02h
    int 21h
    
    mov dl,0dh
    mov ah,02h
    int 21h,
    ret
crlf    endp

code   ends
       end  start

我来回复

您尚未登录,请登录后再回复。点此登录或注册