在汇编中将 Char 转换为 ASCII 码(十进制)

Convert Char to ASCII Code (Decimal) in Assembly

我是 Assembly 的新手 - 不到一周的经验。我试图从提示中接收一个字符,然后以十进制显示 ASCII 代码。我已经为此工作了几个小时,并决定来这里。

当我在提示符中输入 'a' 时,我的模拟器显示我的 char 变量正在存储 61 个十六进制数,这是正确的。我似乎无法将其打印到屏幕上(十六进制或十进制)。

编辑:将代码更改为循环并除以 10。我有正确的字符打印出来,但相反。数字 97 打印出 79

到目前为止,这是我得到的:

; **************** MACROS *********************

; START PROGRAM
START MACRO

    MOV AX, DATA ; Data Segment to AX Register
    MOV DS, AX   ; HAVE DS Point to Data Segment  
ENDM          

; END PROGRAM
END MACRO  

    MOV AH, 4CH  ; END Program
    INT 21H      ; Call DOS Service
ENDM

; PRINT STRING TO OUTPUT        
PSTRING MACRO STR

    MOV AH,09H
    LEA DX,STR
    INT 21H
ENDM

; Creates a New Line
NEWLINE MACRO 
    MOV DX, 0AH   ; Input of string to DX
    MOV AH, 02H   ; Write Char to standard output
    INT 21H        

    MOV DL, 0DH   ; 
    MOV AH, 02H   ; Carriage Return
    INT 21H       ;
ENDM

; Get CHAR Input 
GETINPUT MACRO INPUT

    MOV AH, 01H     ; Receive input          
    INT 21H         ; Call DOS Service

    MOV INPUT, AL   ; Store into Input Variable  
ENDM 

; ********** END MACROS *******************


.MODEL   
.DATA   

   MSG1 db 'Choose A Char to Convert To ASCII: $' 
   CHAR db ?   ; Store Input Char
   REM  db ?   ; Remainder 8-bit storate
   QUOT db ?   ; Quotient  8-bit storage

.CODE 

  MAIN PROC 

    START 

    PSTRING MSG1 

    GETINPUT CHAR

    NEWLINE       

    MOV AX, 0     ; Clear AX Register
    MOV AL, CHAR  ; Move input to AL 

    MOV DX, 0     ; Clear DX Register
    MOV DL, 10    ; Add 10 to DL Register
    DIV DL        ; Divide By Number of Students For Average   

    ; ********************** ;
    ; QUOTIENT  STORED IN AL ;
    ; REMAINDER STORED IN AH ;
    ; ********************** ;                                               


    myLoop:  

        MOV REM, AH   ; Move Remainder into REM  Variable
        MOV QUOT, AL  ; Move Quotient  into QUOT Variable

        MOV DL, REM  ; Move Data we want printed into DL
        ADD DL, '0'   ; Make into Ascii Char

        MOV AH,02H    ; Output Char Service
        INT 21H       ; Call DOS Service


        MOV AL, QUOT  ; Place Quotient Into AL
        MOV AH, 0     ; AH was altered, Zero it out

        MOV DL, 10    ; Set Divisor to 10
        DIV DL        ; Divide AX by 10

        CMP AH, 0     ; If No Quotient Remains we can exit 

    JNZ myLoop        ; Jump if NOT zero 


    MOV AH, 4CH       ; End Program
    INT 21H     

    END               

  MAIN ENDP   
END MAIN

好的,我解决了这个问题。这是最终代码。请记住我只是一个初学者 - 所以如果这段代码不好,请不要给我一个新的。虽然它有效!

; **************** MACROS *********************

; START PROGRAM
START MACRO

    MOV AX, DATA ; Data Segment to AX Register
    MOV DS, AX   ; HAVE DS Point to Data Segment  
ENDM          

; END PROGRAM
END MACRO  

    MOV AH, 4CH  ; END Program
    INT 21H      ; Call DOS Service
ENDM

; PRINT STRING TO OUTPUT        
PSTRING MACRO STR

    MOV AH,09H
    LEA DX,STR
    INT 21H
ENDM

; Creates a New Line
NEWLINE MACRO 
    MOV DX, 0AH   ; Input of string to DX
    MOV AH, 02H   ; Write Char to standard output
    INT 21H        

    MOV DL, 0DH   ; 
    MOV AH, 02H   ; Carriage Return
    INT 21H       ;
ENDM

; Get CHAR Input 
GETINPUT MACRO INPUT

    MOV AH, 01H     ; Receive input          
    INT 21H         ; Call DOS Service

    MOV INPUT, AL   ; Store into Input Variable  
ENDM 

; ********** END MACROS *******************


.MODEL

.STACK 100H

.DATA 

   MSG1 db 'Choose A Char to Convert To ASCII: $' 
   CHAR db ?   ; Store Input Char
   REM  db ?   ; Remainder 8-bit storate
   QUOT db ?   ; Quotient  8-bit storage
   COUNT db 0  ; Counts the stacks  

.CODE 

  MAIN PROC 

    START 

    PSTRING MSG1 

    GETINPUT CHAR

    NEWLINE       

    MOV AX, 0     ; Clear AX Register
    MOV AL, CHAR  ; Move input to AL 

    MOV DX, 0     ; Clear DX Register

    ; ********************** ;
    ; QUOTIENT  STORED IN AL ;
    ; REMAINDER STORED IN AH ;
    ; ********************** ;                                               


    myLoop:  

        MOV DL, 10    ; Set Divisor to 10
        DIV DL        ; Divide AX by 10

        MOV REM, AH   ; Move Remainder into REM  Variable
        MOV QUOT, AL  ; Move Quotient  into QUOT Variable

        MOV AX, 0     ; Clear AX
        MOV AL, REM   ; Move REM to AL
        PUSH AX       ; Push AX to Stack
        INC COUNT     ; Increase Count by 1

        MOV AL, QUOT  ; Place Quotient Into AL
        MOV AH, 0     ; AH was altered, Zero it out

        CMP AL, 0     ; If No Quotient Remains we can exit 

    JNZ myLoop        ; Jump if NOT zero 

    myLoop2:  

        POP DX        ; Pop from the stack into DX

        ADD DX, '0'   ; To Ascii Char
        MOV AH, 02H   ; Print Char Command
        INT 21H       ; Call to DOS System

        DEC COUNT     ; Decrement COUNT
        CMP COUNT, 0  ; Compare COUNT to 0


    JNZ myLoop2    

    END               

  MAIN ENDP   
END MAIN