汇编实现输入字符串以十进制数字输出字符个数

时间:2021-08-10 00:12:38
;**************************************************************
;程序功能:显示输入字符串字符个数
;编译环境:MASM611下编译通过
;程序作者:抓鱼鸟
;**************************************************************
TITLE STRING_LENGHT(EXE) 显示输入的字符串的字符个数(以十进制)
     .MODEL SMALL
     .STACK 256
;===========================================
     .DATA
;===========================================
;  这里我们不用这个结构,只利用输入字符缓存区
PARLIST            LABEL       BYTE             ;定义输入结构
MAXNLEN          DB            40                ;输入最大字符个数
ACTULEN          DB            ?                  ;实际输入字符个数
INPUT_DATA     DB            40 DUP(' ')    ;键盘输入字符缓冲区
;-----------------------------------------------------------------------------
TITLE_INPUT     DB  'Please Input:',0DH
TITLE_ERROR    DB  'Input charactor error,please retry!',0DH
DATA_TEMP      DW ?                             ;数据临时变量
DATA_BUFFER   DB 40 DUP(0)                ;数据转换缓冲区
DATA_BIT         DB ?                              ;记录数据位数
DATA_LEN        DB ?
;============================================
     .386
     .CODE
;---------------------------------------------------------------------------
; MAIN :程序入口函数
;---------------------------------------------------------------------------
MAIN PROC FAR

        MOV         AX,@DATA
        MOV         DS,AX

        CALL        CLEARSCREEN                 ;清屏

        LEA          DX,TITLE_INPUT
        PUSH        DX
        CALL        STRLEN                           ;计算字符个数

        PUSH        AX
        PUSH        DX
        CALL         PRINT                             ;打印输入提示符  

        MOV          AH,0AH                           ;请求键盘输入
        LEA           DX,INPUT_DATA
        INT           21H

        PUSH        DX
        CALL        CONVERTBUFFER              ;整理字符输入缓冲区

        LEA          DX,INPUT_DATA
        PUSH        DX
        CALL        STRLEN

        PUSH        AX
        CALL        CONVERTASCII

        PUSH        AX
        CALL        SETCURSOR

        LEA          DX,DATA_BUFFER
        PUSH        DX
        CALL        STRLEN

        LEA          DX,DATA_BUFFER
        PUSH        AX    
        PUSH        DX
        CALL        PRINT

        MOV         AX,4C00H
        INT          21H
MAIN endp
;---------------------------------------------------------------------------
; PRINT:显示传入的字符串
; 参数:szBuffer 传入的字符串缓冲区
; 参数:iLen 传入字符个数
;---------------------------------------------------------------------------
PRINT PROC NEAR

        PUSH       BP
        MOV        BP,SP
        PUSHAD

        XOR         SI,SI
        MOV         SI,
        XOR         BX,BX
        MOV         BX,

        MOV         AL,'$'
        MOV         [SI+BX],BYTE PTR AL  

        MOV         AH,09                           ;请求屏幕输出
        MOV         DX,SI
        INT          21H

        POPAD
        POP         BP
        ret          4
PRINT endp
;---------------------------------------------------------------------------
; STRLEN:检查字符串长度
; 参数:szBuffer 传入要检测的字符串地址
; 返回值:返回字符长度。放在AX中
;---------------------------------------------------------------------------
STRLEN PROC NEAR

        PUSH        BP
        MOV         BP,SP
        MOV         SI,

        MOV         AL,BYTE PTR [SI]
        XOR         BX,BX
A10:
        CMP          AL,0DH
        JZ             A20
        INC           BX
        INC           SI
        MOV          AL,BYTE PTR [SI]
        JMP           A10
A20:
        MOV          AX,BX
        POP           BP
        ret            2
STRLEN endp
;---------------------------------------------------------------------------
; COVERTASCII:将HEX转换成ASCII十进制字符串
; 参数:Numeric 要转换的16进制数字
; 返回值:返回字符长度。放在DATA_BUFFER中并已0DH结尾
;---------------------------------------------------------------------------
CONVERTASCII PROC NEAR
        PUSH         BP
        MOV          BP,SP
        PUSHAD


        XOR            DX,DX
    MOV          AX,
    LEA            SI,DATA_BUFFER
    MOV          BX,000AH
C10:
     DIV            BX
     CMP          AX,BX
     JB               C20

ADD         DX,30H
     MOV         [SI],BYTE PTR DL
    INC             SI
    XOR           DX,DX
     JMP          C10
C20:
     TEST        AX,-1
     JZ             C30
    ADD         AX,30H
     MOV       [SI],BYTE PTR AL
     INC         SI
C30:
    ADD         DX,30H
    MOV        [SI],BYTE PTR DL
    INC           SI
    MOV        AX,000DH
    MOV        [SI],BYTE PTR AL  

    POPAD
    POP           BP
     ret 2
CONVERTASCII endp
;---------------------------------------------------------------------------

; CLEARSCREEN:清屏
; 参数:无
; 返回值:无
;---------------------------------------------------------------------------
CLEARSCREEN PROC NEAR

        PUSHAD

        MOV         AX,0600H                         ;请求卷屏
        MOV         BH,07H
        MOV         CX,0000H
        MOV         DX,184FH
        INT           10H

        POPAD
        ret
CLEARSCREEN endp
;---------------------------------------------------------------------------
; SETCURSOR:设置光标
; 参数:STRING_LENGTH 显示的字符串长度
; 返回值:无
;---------------------------------------------------------------------------
SETCURSOR PROC NEAR

        PUSH        BP
        MOV         BP,SP
        PUSHAD

        XOR         BX,BX
        MOV         AX,004FH    ;列数
        MOV         CX,0018H    ;行数
        MOV         BX,

        SUB AX,BX
        SHR AX,1
        MOV BX,AX
        SHR CX,1

        MOV AH,02H                                ;请求设置光标
        MOV BH,00
        MOV DH,CL
        MOV DL,BL
        INT 10H

        POPAD
        POP BP
        ret 2
SETCURSOR endp
;---------------------------------------------------------------------------
; CONVERTBUFFER:整理缓存,将字符输入缓存重新整理
; 参数:INPUTBUFFER 输入缓存
; 返回值:无
;---------------------------------------------------------------------------
CONVERTBUFFER PROC NEAR

        PUSH        BP
        MOV         BP,SP
        PUSHAD

        MOV         SI,
        MOV         DI,SI
        ADD         SI,2                            ;输入缓存前两位为数据位

        XOR         AX,AX
        MOV         AL,BYTE PTR [SI]
        XOR         DX,DX
C10:
        CMP          AL,20H
        JB            C20
        CMP         AL,7EH
        JA            C20
        CMP         AL,0DH
        JZ            C20
        MOV        [DI],BYTE PTR AL
        INC          SI
        MOV         AL,BYTE PTR [SI]
        INC          DI
        INC          DX
        JMP          C10
C20:
        MOV         AL,0DH
        MOV         [DI],BYTE PTR AL

        POPAD
        POP          BP
        ret           2
CONVERTBUFFER endp
;===============================================
                       END MAIN