当前位置: 动力学知识库 > 问答 > 编程问答 >

Convert Char to ASCII Code (Decimal) in Assembly

问题描述:

I'm very new to Assembly - less than one week of experience. I'm trying to receive in a character from a prompt, then display out the ASCII Code in decimal. I have been working on this for hours and decided to come here.

When I put in 'a' into the prompt, my emulator shows that my char variable is storing 61 hexidecimal, which is correct. I just can't seem to get it to print to the screen (in hex or decimal).

EDIT: CHANGED CODE TO LOOP THROUGH AND DIVIDE BY TEN. I HAVE THE CORRECT CHARS PRINTING OUT, BUT IN REVERSE. THE NUMBER 97 PRINTS OUT 79

So far, this is what i've got:

; **************** 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

网友答案:

Okay, I solved this one. Here's the final Code. Please remember i'm just a beginner - so don't tear me a new one if this code is bad. It works though!

; **************** 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
分享给朋友:
您可能感兴趣的文章:
随机阅读: