Posted By : Murugan Andezuthu Dharmaratnam
Posted On : 16 November 2014
Keywords : Read Numeric value in 8086,From numeric to character or hex, Read Numeric Value in 8086 Assembly Language

Assemly Language Code To Convert From Character To Numeric Or Hex Value In 8086 Assembly Language

Problem Statment

Read a numeric character from keybaord and convert into numeric integer or hex so that it can be used for further operations like additiona division or multiplication. this article will give students the basic knowledge read a single a single digit numeric character using Keyboard and convert it into Integer / Hex . Now once you know how to convert one character all you neeed to do is improve the logic and read longer characters.

Read Single Digit Numeric Character And Convert It To Numeric Or Hex

Some one today send me a piece of code which is used to add two numbers. Its a very simple program so it adds just two single digit numbers.
Interesting part of the code is it gives you an easy way to convert the number from character to numeric.
mov ah,1h ;read character
int 21h
sub al,30h
mov num1,al
Now if I am entering a value = 5 ,
Its corresponding value from the below table is Hex 35H or Decimal 53D
Now the interesting part is that they subtract Hex value of character 0
30H or 48D
So ideally if you are subtracting the decimal values 53 - 48 = 5 or 35H - 30H = 5
5 which is the converted value in 8086 from Character to Numeric.

ASCII Table Reference

Assembly Language Code To Convert Single Digit Numeric Character To Integer

Sample Code

.model tiny
org 100h

; add your code here
main proc near
    mov ah,1h   ;Read Character From Console
    int 21h
    sub al,30h
    mov num1,al 
    mov ah,4ch  ; Terminate & Return Back To Dos Prompt
    mov al,00
    int 21h   
num1 db ?;
end main 


Now Lets Debug The Output

We are not displaying anything . So I would like to teach you on how to debug in emu8086 and find out wether you have the right value 5 after you subtract in the AL Register .

STEP 1 : Read Numeric Character

STEP 2 : Convert To Number Or Integer Or Hex

Murugan Andezuthu Dharmaratnam

Murugan is a technology leader with 14 years of hands-on experience in full life cycle systems design, development, implementation, and management. In addition to systems design and development, his core competencies include cloud design and architecture, interactive web applications and websites development, mobile computing, and Healthcare Information Technology. His experience is very diverse with a broad range of technologies within multiple industry settings including private, public, and non-profit. Coupled with his technology expertise, Murugan is a well-organized, result-oriented, and a highly analytical leader with proven track record in developing and implementing very complex systems.