用汇编语言产生0到100之间的随机数

如题所述

code  segment
      assume cs:code
      org 100h
start:
      push cs
      pop ds
      push cs
      pop es
      
      mov cx,100
@main1:
      push cx
      call rand
      mov ax,cx
      call dispaxs
      call delay
      pop cx
      loop @main1
      
      mov ah,4ch
      int 21h
;===============================================
        RAND  PROC      NEAR
              ; cx 中保存16进制数
              ; ax 中保存BCD数,高位在ah,低位在al
              PUSH      BX
              PUSH      DX
              STI
              MOV       AH,0        ;读时钟计数器值
              INT       1AH
              CLI
              MOV       AX,DX       ;清高6位
              AND       AH,3
              MOV       DL,101      ;除101,产生0~100余数
              DIV       DL
              MOV       AL,AH       ;余数存AL,作随机数
              MOV       AH,0
              MOV       CX,AX
              MOV       BL,10
              DIV       BL
              XCHG      AL,AH
              ADD       AL,30H
              ADD       AH,30H
              POP       DX
              POP       BX
              RET
        RAND  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
;================================================
delay       proc near
            push cx
            mov cx,2000
@delay1:
            push cx
            mov cx,14400
@delay2:
            nop
            nop
            loop @delay2
            pop cx
            loop @delay1
            pop cx
            ret
delay       endp
;===========================
code  ends
      end start

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