1

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
  • Your `PSTRING` macros uses [int 21h/ah=9](http://www.ctyme.com/intr/rb-2562.htm) to output to the console. From the link you'll see the string is suppose to be terminated with a `$`.What you can do is place a `$` in memory right after the location you write your character to. You should be able to do it by changing `CHAR db ?` to `CHAR db ?, '$'` . That should solve the problem of junk being displayed. – Michael Petch Apr 10 '16 at 05:51
  • That solves one problem lol. The main problem is something i'm working on now. Take a Look at my revised code. I made a loop to divide the Hex number by 10, then output each number. The problem is that my number comes out esrever... sorry, i mean reverse. For example, i can get the value for 'a' to be 97, but it comes out 79 –  Apr 10 '16 at 06:05
  • I don't see your code for it, but I gather you are looping doing division by 10 repeatedly. It comes out backwards because you are processing the numbers from the least significant digits to the most significant digits. There are a number of ways to deal with that, and there are some examples even on StackOverflow if you look (one is stack based and another would be to write characters to an output buffer storing them from right to left) – Michael Petch Apr 10 '16 at 06:09
  • @MichaelPetch Forgot to include your name so you see response. Thanks for your time Michael –  Apr 10 '16 at 06:09
  • @MichaelPetch (sorry, it took longer to edit than I thought) Most of the push/pop method's i've seen push the number to ax, but I always get errors when trying to do so. Any way you can show me an example of push/pop in 8086. I'll continue to look –  Apr 10 '16 at 06:11
  • Here is one method that uses a buffer (not push pop): http://stackoverflow.com/a/15621644/3857942 . The code I linked to is for NASM but the general idea should be usable with minor changes to work with EMU8086. – Michael Petch Apr 10 '16 at 06:12

1 Answers1

1

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