x86基础实验-进制转换(堆栈实现)

简介

通过堆栈传参的方式实现进制转换

程序运行结果

代码
    ASSUME  CS:CODE1,DS:DATA1,SS:STACK1

STACK1  SEGMENT STACK
STACK_AREA      DW      200H DUP(?)
STACK_BTM       EQU     $-STACK_AREA
STACK1          ENDS

DATA1   SEGMENT
EXPLAIN     DB  13,10,"please select function: 1. HEX TO DEC 2.DEC TO HEX 3.BIN TO DEC 4.DEC MUL 5.DEC DIV other.QUIT",13,10,'$'
EXPLAIN_INPUT   DB  13,10,"please input number",13,10,'$'
NUMBER  DD  1235H
ASCII   DB  20H DUP(0)
FUNC_TABLE  DW  HEX2DEC,DEC2HEX,BIN2DEC,DEC_MUL,DEC_DIV
DATA1   ENDS

CODE1 SEGMENT

OUTPUT_NUM  PROC    NEAR    ;[SP+2]:num(byte)
        PUSH    BP
        MOV     BP,SP
        PUSH    AX
        PUSH    DX

        MOV     DL,[BP+4]
        CMP     DL,9
        JA      OUTPUT_NUM_ALPHA 

        ADD     DL,'0'
        JMP     OUTPUT_NUM_OUTPUT
    OUTPUT_NUM_ALPHA:
        ADD     DL,'A'-10
        
    OUTPUT_NUM_OUTPUT:
        MOV     AH,02H
        INT     21H

        POP     DX
        POP     AX
        POP     BP

        RET     2
OUTPUT_NUM  ENDP

OUTPUT_NUM_HEX      PROC    NEAR ;[SP+2]:num

        PUSH    BP
        MOV     BP,SP
        PUSH    AX
        PUSH    BX
        PUSH    CX
        PUSH    DX
        MOV     BX,[BP+4]

        MOV     CX,4

    DEC2HEX_LOOP2:

        ROL     BX,1
        ROL     BX,1
        ROL     BX,1
        ROL     BX,1

        MOV     DL,BL
        AND     DL,0FH

        PUSH    DX
        CALL    OUTPUT_NUM  

        LOOP    DEC2HEX_LOOP2

        POP     DX
        POP     CX
        POP     BX
        POP     AX
        POP     BP

        RET    2 

OUTPUT_NUM_HEX      ENDP



OUTPUT_NUM_DEC  PROC    NEAR ;[SP+2]:num

        PUSH    BP
        MOV     BP,SP
        PUSH    SI
        PUSH    DI
        PUSH    AX
        PUSH    BX
        PUSH    CX
        PUSH    DX

        MOV     AX,[BP+4]
        MOV     SI,10
        MOV     DI,0
        MOV     BP,OFFSET ASCII

    HEX2DEC_LOOP2:

        MOV     DX,0

        MOV     CX,10 
        DIV     CX

        MOV     DS:[BP+DI],DL 
        INC     DI
        CMP     AX,0
        JNE     HEX2DEC_LOOP2

        MOV     CX,DI

    HEX2DEX_LOOP3:

        DEC     DI  
        MOV     DL,DS:[BP+DI]

        PUSH    DX
        CALL    OUTPUT_NUM

        LOOP    HEX2DEX_LOOP3

        POP     DX
        POP     CX
        POP     BX
        POP     AX
        POP     DI
        POP     SI
        POP     BP

        RET     2
        

OUTPUT_NUM_DEC  ENDP

MAIN    PROC    FAR

        MOV     AX,STACK1
        MOV     SS,AX
        MOV     SP,STACK_BTM
        MOV     AX,DATA1
        MOV     DS,AX
        
        MAIN_LOOP:

        MOV     DX,OFFSET EXPLAIN
        MOV     AH,09H
        INT     21H

        MOV     AH,01H
        INT     21H
        CMP     AL,'1'
        JB      EXIT
        CMP     AL,'5'
        JA      EXIT

        SUB     AL,'1'
        MOV     AH,0
        MOV     BX,AX
        SHL     BX,1

        MOV     DX,OFFSET EXPLAIN_INPUT
        MOV     AH,09H
        INT     21H

        MOV     SI,OFFSET FUNC_TABLE 
        CALL     [SI+BX]

    CONTINUE:
        JMP     MAIN_LOOP

    EXIT:
        MOV     AH,4CH 
        INT     21H

MAIN    ENDP


DEC_MUL     PROC    NEAR
        PUSH    AX
        PUSH    BX
        PUSH    CX
        PUSH    DX

        CALL    READ_DEC
        PUSH    AX

        CALL    READ_DEC
        POP     BX
        MUL     BX

        MOV     BX,AX
        PUSH    BX
        CALL OUTPUT_NUM_DEC
        
        POP     DX
        POP     CX
        POP     BX
        POP     AX

        RET

DEC_MUL     ENDP



DEC_DIV     PROC    NEAR

        PUSH    AX
        PUSH    BX
        PUSH    CX
        PUSH    DX

        CALL    READ_DEC
        PUSH    AX

        CALL    READ_DEC
        MOV     BX,AX

        POP     AX
        MOV     DX,0

        DIV     BX
        MOV     BX,AX

        PUSH    BX
        CALL OUTPUT_NUM_DEC

        POP     DX
        POP     CX
        POP     BX
        POP     AX
        RET

DEC_DIV     ENDP
        
        


DEC2HEX     PROC    NEAR
        PUSH    AX
        PUSH    BX
        PUSH    CX
        PUSH    DX
        
        MOV     BX,0
    DEC2HEX_LOOP:
        MOV     AH,01
        INT     21H
        CMP     AL,'0'
        JB      DEC2HEX_LOOP_END
        CMP     AL,'9'
        JA      DEC2HEX_LOOP_END
        SUB     AL,'0'     
        MOV     AH,0

        PUSH    AX
        MOV     AX,BX
        MOV     BX,10
        MUL     BX
        MOV     BX,AX
        POP     AX

        ADD     BX,AX

        JMP     DEC2HEX_LOOP
    DEC2HEX_LOOP_END:
        PUSH    BX
        CALL OUTPUT_NUM_HEX

        POP     DX
        POP     CX
        POP     BX
        POP     AX

        RET
DEC2HEX     ENDP
        
    


BIN2DEC     PROC    NEAR

        PUSH    AX
        PUSH    BX
        PUSH    CX
        PUSH    DX

        MOV     BX,0 
    BIN2DEC_LOOP:
        MOV     AH,01

        INT     21H
        CMP     AL,'0'
        JB     BIN2DEC_LOOP_END
        CMP     AL,'1'
        JA     BIN2DEC_LOOP_END
        SUB     AL,'0'
        MOV     AH,0

        SHL     BX,1
        ADD     BX,AX
        JMP     BIN2DEC_LOOP

    BIN2DEC_LOOP_END:

        PUSH    BX
        CALL    OUTPUT_NUM_DEC

        POP     DX
        POP     CX
        POP     BX
        POP     AX

        RET
BIN2DEC ENDP


HEX2DEC     PROC    NEAR

        PUSH    AX
        PUSH    BX
        PUSH    CX
        PUSH    DX

        MOV     BX,0
    HEX2DEC_LOOP:
        MOV     AH,01
        INT     21H
        CMP     AL,'0' 
        JB      HEX2DEC_LOOP_END
        CMP     AL,'9'
        JA      HEX2DEC_ASCII
        SUB     AL,'0'
        JMP     HEX2DEC_READ  

    HEX2DEC_ASCII:
        OR      AL,32
        CMP     AL,'a'
        JB      HEX2DEC_LOOP_END
        CMP     AL,'f'
        JA      HEX2DEC_LOOP_END
        SUB     AL,'a'-10

    HEX2DEC_READ:
        SHL     BX,1
        SHL     BX,1
        SHL     BX,1
        SHL     BX,1

        MOV     AH,0
        ADD     BX,AX
        
        JMP     HEX2DEC_LOOP

    HEX2DEC_LOOP_END:
        PUSH    BX
        CALL    OUTPUT_NUM_DEC

        POP     DX
        POP     CX
        POP     BX
        POP     AX


        RET 

HEX2DEC     ENDP



READ_DEC    PROC    NEAR

        PUSH    BX
        PUSH    CX
        PUSH    DX
        
        MOV     BX,0
    READ_DEC_LOOP:
        MOV     AH,01
        INT     21H
        CMP     AL,'0'
        JB      READ_DEC_LOOP_END
        CMP     AL,'9'
        JA      READ_DEC_LOOP_END
        SUB     AL,'0'     
        MOV     AH,0

        PUSH    AX
        MOV     AX,BX
        MOV     BX,10
        MUL     BX
        MOV     BX,AX
        POP     AX

        ADD     BX,AX
        JMP READ_DEC_LOOP

    READ_DEC_LOOP_END:

        MOV     AX,BX
        
        POP     DX
        POP     CX
        POP     BX

        RET  
READ_DEC    ENDP

CODE1   ENDS



END     MAIN

你可能感兴趣的:(x86基础实验-进制转换(堆栈实现))