用汇编语言编写10个任意数相加求和的乘坐

如题所述

用汇编语言编写10个任意数相加求和的程序:

        CODE  SEGMENT
              ASSUME    CS:CODE
              ORG       100H
      START:  PUSH      CS
              POP       DS
              PUSH      CS
              POP       ES
              CALL      INPUTNUM
              MOV       AH,2
              MOV       DL,13
              INT       21H
              MOV       DL,10
              INT       21H
              LEA       SI,@ARRAY
              MOV       CX,@NUM
              CLD
         @1:
              LODSW
              add word ptr sum,ax
              CALL      DISPAXS
              LOOP      @1
              
              MOV       AH,2
              MOV       DL,13
              INT       21H
              MOV       DL,10
              INT       21H
              mov ax,sum
              call dispaxs
              
              MOV       AH,4CH
              INT       21H

sum         dw 0

;===============================================
;  键盘输入数值数组子程序(数组名为@array,元素个数存放在@num中)
;  可以是负数
    INPUTNUM  PROC      NEAR
              PUSH      AX
              PUSH      BX
              PUSH      CX
              PUSH      DX
              PUSH      SI
              PUSH      DI
              PUSHF
      ; 对数组@array清零
              MOV       CX,100
              MOV       AX,0
              LEA       DI,@ARRAY
              REP       STOSW
              MOV       WORD PTR @NUM,0
      ; 输入的数据以一个空格分隔,以回车符结束输入
              LEA       DX,@STRA
              MOV       AH,10
              INT       21H
              MOV       CL,@STRN
              MOV       CH,0
              MOV       WORD PTR @STRM,CX
              LEA       SI,@STR
              CLD

              LEA       DI,@ARRAY   
              MOV       WORD PTR @NUM,0
      @STIN:
              MOV       AX,0
              PUSH      AX
              MOV       BYTE PTR @SIGNN,0
    @AGAIN1:
              ;MOV       AH,1
              ;INT       21H
              CMP       WORD PTR @STRM,0
              JE        @LINE0
              LODSB
              DEC       WORD PTR @STRM
              ; MOV       BYTE PTR @CHAR,AL
              ; CMP       AL,13
              ; JE        @LINE0
              CMP       AL,' '
              JE        @LINE0
              CMP       AL,'-'
              JNE       @INPUT1
              MOV       BYTE PTR @SIGNN,1
              JMP       @AGAIN1
    @INPUT1:
              SUB       AL,30H
              MOV       AH,0
              MOV       BX,AX
              POP       AX
              MOV       CL,10
              MOV       CH,0
              MUL       CX
              ADD       AX,BX
              PUSH      AX
              JMP       @AGAIN1
     @LINE0:
              POP       AX
              CMP       BYTE PTR @SIGNN,1
              JNE       @INPUT2
              NEG       AX
    @INPUT2:
              MOV       WORD PTR[DI],AX
              INC       WORD PTR @NUM
              ; CMP       BYTE PTR @CHAR,13
              CMP       WORD PTR @STRM,0
              JE        @STINEND
              INC       DI
              INC       DI
              JMP       @STIN
   @STINEND:
              POPF
              POP       DI
              POP       SI
              POP       DX
              POP       CX
              POP       BX
              POP       AX
              RET
       @STRA  DB        255
       @STRN  DB        ?
        @STR  DB        255 DUP(?)
       @STRM  DW        0
      @ARRAY  DW        100 DUP(0)
        @NUM  DW        0
       @CHAR  DB        ?
      @SIGNN  DB        0
    INPUTNUM  ENDP

;=========================================
 ; 将要显示的有符号数置于 ax 中
     DISPAXS  PROC      NEAR
              PUSH      AX
              PUSH      BX
              PUSH      CX
              PUSH      DX
              PUSH      SI
              PUSH      DI
              PUSH      BP
              PUSH      DS
              PUSH      ES
              PUSHF
              PUSH      CS
              POP       DS
              PUSH      CS
              POP       ES
              MOV       CX,6
              LEA       DI,DISPAXSS
   @DISPAXS:
              MOV       BYTE PTR [DI],32
              INC       DI
              LOOP      @DISPAXS
              PUSH      AX
              MOV       DL,32
              MOV       AH,2
              INT       21H
              POP       AX
              MOV       BYTE PTR NZS,0
              MOV       BYTE PTR SIGNS,0
              CMP       AX,0
              JGE       @DISPAXS0
              MOV       BYTE PTR SIGNS,1
              NEG       AX
  @DISPAXS0:
              PUSH      AX
              LEA       SI,DIVARRS
              LEA       DI,DISPAXSS
              INC       DI
              MOV       CX,5
  @DISPAXS1:
              POP       AX
              MOV       DX,0
              MOV       BX,[SI]
              DIV       BX
              PUSH      DX
              CMP       AL,0
              JNE       @DISPAXS2
              CMP       BYTE PTR NZS,1
              JE        @DISPAXS2
              CMP       CX,1
              JE        @DISPAXS2
              MOV       DL,20H
              JMP       @DISPAXS3
  @DISPAXS2:
              ADD       AL,30H
              MOV       DL,AL
              MOV       BYTE PTR NZS,1
  @DISPAXS3:
              MOV       BYTE PTR[DI],DL
              INC       SI
              INC       SI
              INC       DI
              LOOP      @DISPAXS1
              POP       DX

              CMP       BYTE PTR SIGNS,1
              JNE       @DISPAXS6
              LEA       SI,DISPAXSS
              ADD       SI,5
  @DISPAXS4:
              CMP       BYTE PTR [SI],32
              JE        @DISPAXS5
              DEC       SI
              JMP       @DISPAXS4
  @DISPAXS5:
              MOV       BYTE PTR [SI],'-'
  @DISPAXS6:
              LEA       DX,DISPAXSS
              MOV       AH,9
              INT       21H
              POPF
              POP       ES
              POP       DS
              POP       BP
              POP       DI
              POP       SI
              POP       DX
              POP       CX
              POP       BX
              POP       AX
              RET
     DIVARRS  DW        10000,1000,100,10,1
         NZS  DB        0
       SIGNS  DB        0
    DISPAXSS  DB        32,32,32,32,32,32,'$'
     DISPAXS  ENDP
;==================================
        CODE  ENDS
              END       START

温馨提示:答案为网友推荐,仅供参考
相似回答