单片机汇编 求N个数的和 程序

单片机汇编 求N个数的和 程序,第1张

启动入口

ORG 0000H 复位启动

LJMP START

ORG 0100H

主程序

START: MOV DPTR,#NUM 数据区首地址

MOV B,#32 32字节求和

CLR A

MOV X,A 清零和存储单元

MOV Y,A

MOV Z,A

LOOP: CLR A

MOVC A,@A+DPTR 取当前字节数据

ADD A,X 累加和

MOV X,A

MOV A,Y

ADDC A,#0

MOV Y,A

INC DPTR 指向下一单元数据

DJNZ B,LOOP 加完32字节

JMP $ 死循环

ORG 0200H 开辟数据区

NUM DB 32,2,5,4,9,220,234,7

DB 11,22,51,42,93,220,234,17

DB 31,32,35,43,39,220,234,37

DB 91,82,45,74,89,220,234,97

X EQU 70H 和存储单元

Y EQU 71H

END

用汇编语言编写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

这是个1位数运算,2位以上运算你给的分真不够我忙活的

data segment

num1 db '1','2','3','4','5','6','7','8','9','1'

num2 db '8','1','6','5','4','3','2','1','0','8'

sum db 10 dup(?)

data ends

code segment

assume cs:code,ds:data

start:

mov ax,data

mov ds,ax

mov cx,10

mov si,0

next:

push cx

mov bx,offset num1

mov dl,[bx+si]

mov ah,02h

int 21h

mov dl,'+'

mov ah,02h

int 21h

mov bx,offset num2

mov dl,[bx+si]

mov ah,02h

int 21h

mov dl,'='

mov ah,02h

int 21h

mov bx,offset num1

mov dl,[bx+si]

mov bx,offset num2

add dl,[bx+si]

sub dl,30h

mov bx,offset sum

mov [sum+si],bl

mov ah,02h

int 21h

inc si

mov ah,02h

mov dl,0ah

int 21h

mov ah,02h

mov dl,0dh

int 21h

pop cx

loop next

mov ah,4ch

int 21h

code ends

end start


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12052768.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存