Hi, I need this program to print ascii code that I entered to al.
The problem is that it is always print the char of the value ; i.e If I type a, al=61.
How I print just the '61' and not the 'a' ?

Here is my code :

.model small
.stack 100h
.data

logo db ' ___                     ',10,13
     db '| _ )_  _                ',10,13
     db '| _ \ || |               ',10,13
     db '|___/\_, |               ',10,13
     db ' |   |__/                ',10,13
     db ' |________       ______  ',10,13
     db '___  __ \      ___  __ \ ',10,13
     db '__  / / /________  /_/ / ',10,13
     db '_  /_/ /_/_____/  ____/  ',10,13
     db '/_____/        /_/       $',10,13

msg1 db 'Press any key to get his ASCII Code. Press Esc to quit.',10,13
	 db '$'
	 
msg2 db ' .ASCII Code :  . Press another key, to get his ASCII Code. Press Esc to quit.',10,13
	 db '$'

.code
mov ax,@data
mov ds,ax

call clear

mov dx, offset logo ;print my logo.
mov ah, 9
int 21h
	   
call delay ;call delay function for taking time.
call clear ;give me a clean MS-Dos screen.

mov dx, offset msg1 ;print first msg.
mov ah, 9
int 21h

coun:  mov ah, 1h ; keyboard input subprogram.
	   int 21h ; read character into al and print it.
	   
       cmp al, 27  ; if key is 'esc' then exit. 
       je stop

       inc bx ; increase bx on every key press. 

	   mov dx, offset msg2 ;print second msg.
	   mov ah, 9
	   int 21h
	   
       jmp coun

stop:  mov dx, offset logo
       mov ah, 9
       int 21h

mov ah, 0
int 16h





	  
;---------------------------------------------------------	  
	  
mov ah,7
int 21h
mov ah,4ch
int 21h

;---------------------------------------------------------
clear:
mov ax,3
int 10h
ret

delay:
mov bx,7fffh
x:mov cx,0ffffh
x1:loop x1
dec bx
jnz x
ret
;---------------------------------------------------------

end

You're not reading in an 'a', it's reading in a binary value that when your O/S prints recognizes as the value for 'a' and since you're putting this value onto an output stream in video it's assumed to be an 'a'.

So your ascii 'a' is 0x61h in hex and since the 'a' and 0x61 are just other ways to view the binary value of

0110 0001

An easy way to turn this into a string is to use a lookup table.
So as an example I'll and out the first byte

0110 0001
& 0000 1111

Which gives me a 1 now I'll use this as an index into a lookup table

lkuptable db '0123456789ABCDEF'
[lkuptable + 1]

Well look at that if I index anywhere from 0 to 15 into that table it'll give me a character equiv of whichever byte I and out.

'a' == 0x61h == [lkuptable+6] [lkuptable+1]

I dont understand anything.
My asm knowledge is totally basic.
Can u give me a source code for something I hit a key and it is give me back the ascii code :) ?

Jackal hex digit hunt, hunts digits and runs!
Hello coder,
it is simple.
Two hexdigits make up one byte, one making up the
high nibble (4-bits) and the other the low nibble (4-bits).
To get the high nibble simply bitshift the byte to the
right by four bits.
And to get the low nibble simply bitshift the same byte to
the left four bits and then to the right by four bits.
For 5C for example:
High nibble:
5C >> 4 = 05 + offset of table to get ASCII character
Low nibble:
5C << 4 = C0
C0 >> 4 = 0C + offset of table to get ASCII character

The following code assembles with NASM, it is 16-bit 8088
assembler as an example.

bits 16
org 100h

jmp l0_jump
hexdigit db '0123456789ABCDEF'
l0_jump:
mov al,5ch ; Place the value 5ch into al to be printed
mov bl,al  ; make backup of al
shr al,4   ; get high nibble
call printh ; print corresponding hex digit
mov al,bl  ; retrieve our backup
shl al,4   ; shift to the left to clear high nibble
shr al,4   ; get low nibble
call printh
int 20h

printh:
xor ah,ah
mov si,ax ; copy value of hex digit into si
mov dl,[si+hexdigit] ; add value of hex digit to offset of table
mov ah,02
int 21h
ret

To get the ASCII value or scancode of a key pressed on the
keyboard simply use Function 06h (Direct Console I/O).

waitkey:
mov ah,06 ; Direct Console I/O
mov dl,0ffh ; Indicate to read from keyboard
int 21h
jz waitkey ; Jump until zeroflag is clear
mov [keycode],al ; character returned in al

For a special key the first character in is a 0 (zero).
You will need to call Function 06 again to get the
second byte containing the scancode.

Not sure if this is what you were looking for, but here is one of the projects I recently wrote. It's fairly long.

[BITS 16]		;Set code generation to 16 bit mode
[ORG 0100h]		;Set code start address to 0100h


[SEGMENT .text]		;Main code segment

start:
mov dx,main   ;Display main message
call write
mov dx,main2
call write

mov dx,eol
call write    ;Create two blank lines
mov dx,eol
call write

call ascii    ;Generate main line
call ascii
call ascii
call ascii
call ascii
call ascii
call ascii
call ascii1
call ascii1
call ascii1   ;End main line

mov dx,eol   ;Move down two lines
call write
mov dx,eol
call write   ;End move down two lines


jmp key

exit:
mov ax,04c00h  ;Exit
int 21h        ;Exit properly

key:
mov ah,0    ;Keyboard input function
int 16h      ;Call DOS

cmp al,1Bh   ;Check if escape
je near exit

cmp al,61h	;Check if "a" key
je near ahex

cmp al,41h	;Check if "A" key
je near Ahex

cmp al,08h	;Check if "TAB" key
je near tab

cmp al,20h	;Check if space
je near dspace

cmp al,62h	;Check if "b" key
je near bhex

cmp al,42h	;Check if "B" key
je near Bhex

cmp al,63h	;Check if "c" key
je near chex

cmp al,43h	;Check if "C" key
je near Chex

cmp al,64h	;Check if "d" key
je near dhex

cmp al,44h	;Check if "D" key
je near Dhex

cmp al,65h	;Check if "e" key
je near ehex

cmp al,45h	;Check if "E" key
je near Ehex

cmp al,66h	;Check if "f" key
je near fhex

cmp al,46h	;Check if "F" key
je near Fhex

cmp al,67h	;Check if "g" key
je near ghex

cmp al,47h	;Check if "G" key
je near Ghex

cmp al,68h	;Check if "h" key
je near hhex

cmp al,48h	;Check if "H" key
je near Hhex

cmp al,69h	;Check if "i" key
je near ihex

cmp al,49h	;Check if "I" key
je near Ihex

cmp al,6Ah	;Check if "j" key
je near jhex

cmp al,4Ah	;Check if "J" key
je near Jhex

cmp al,6Bh	;Check if "k" key
je near khex

cmp al,4Bh	;Check if "K" key
je near Khex

cmp al,6Ch	;Check if "l" key
je near lhex

cmp al,4Ch	;Check if "L" key
je near Lhex

cmp al,6Dh	;Check if "m" key
je near mhex

cmp al,4Dh	;Check if "M" key
je near Mhex

cmp al,6Eh	;Check if "n" key
je near nhex

cmp al,4Eh	;Check if "N" key
je near Nhex

cmp al,6Fh	;Check if "o" key
je near ohex

cmp al,4Fh	;Check if "O" key
je near Ohex

cmp al,70h	;Check if "p" key
je near phex

cmp al,50h	;Check if "P" key
je near Phex

cmp al,71h	;Check if "q" key
je near qhex

cmp al,51h	;Check if "Q" key
je near Qhex

cmp al,72h	;Check if "r" key
je near rhex

cmp al,52h	;Check if "R" key
je near Rhex

cmp al,73h	;Check if "s" key
je near shex

cmp al,53h	;Check if "S" key
je near Shex

cmp al,74h	;Check if "t" key
je near thex

cmp al,54h	;Check if "T" key
je near Thex

cmp al,75h	;Check if "u" key
je near uhex

cmp al,55h	;Check if "U" key
je near Uhex

cmp al,76h	;Check if "v" key
je near vhex

cmp al,56h	;Check if "V" key
je near Vhex

cmp al,77h	;Check if "w" key
je near whex

cmp al,57h	;Check if "W" key
je near Whex

cmp al,78h	;Check if "x" key
je near xhex

cmp al,58h	;Check if "X" key
je near Xhex

cmp al,79h	;Check if "y" key
je near yhex

cmp al,59h	;Check if "Y" key
je near Yhex

cmp al,7Ah	;Check if "z" key
je near zhex

cmp al,5Ah	;Check if "Z" key
je near Zhex

jmp key

tab:
mov dx,eol
call write
jmp key

Zhex:
mov dx,zhhex
call write
jmp key

zhex:
mov dx,zlhex
call write
jmp key

Yhex:
mov dx,yhhex
call write
jmp key

yhex:
mov dx,ylhex
call write
jmp key

Xhex:
mov dx,xhhex
call write
jmp key

xhex:
mov dx,xlhex
call write
jmp key

Whex:
mov dx,whhex
call write
jmp key

whex:
mov dx,wlhex
call write
jmp key

Vhex:
mov dx,vhhex
call write
jmp key

vhex:
mov dx,vlhex
call write
jmp key

Uhex:
mov dx,uhhex
call write
jmp key

uhex:
mov dx,ulhex
call write
jmp key

Thex:
mov dx,thhex
call write
jmp key

thex:
mov dx,tlhex
call write
jmp key

Shex:
mov dx,shhex
call write
jmp key

shex:
mov dx,slhex
call write
jmp key

Rhex:
mov dx,rhhex
call write
jmp key

rhex:
mov dx,rlhex
call write
jmp key

Qhex:
mov dx,qhhex
call write
jmp key

qhex:
mov dx,qlhex
call write
jmp key

Phex:
mov dx,phhex
call write
jmp key

phex:
mov dx,plhex
call write
jmp key

Ohex:
mov dx,ohhex
call write
jmp key

ohex:
mov dx,olhex
call write
jmp key

Nhex:
mov dx,nhhex
call write
jmp key

nhex:
mov dx,nlhex
call write
jmp key

Mhex:
mov dx,mhhex
call write
jmp key

mhex:
mov dx,mlhex
call write
jmp key

Lhex:
mov dx,lhhex
call write
jmp key

lhex:
mov dx,llhex
call write
jmp key

Khex:
mov dx,khhex
call write
jmp key

khex:
mov dx,klhex
call write
jmp key

Jhex:
mov dx,jhhex
call write
jmp key

jhex:
mov dx,jlhex
call write
jmp key

Ihex:
mov dx,ihhex
call write
jmp key

ihex:
mov dx,ilhex
call write
jmp key

Hhex:
mov dx,hhhex
call write
jmp key

hhex:
mov dx,hlhex
call write
jmp key

Ghex:
mov dx,ghhex
call write
jmp key

ghex:
mov dx,dlhex
call write
jmp key

Fhex:
mov dx,fhhex
call write
jmp key

fhex:
mov dx,flhex
call write
jmp key

Ehex:
mov dx,ehhex
call write
jmp key

ehex:
mov dx,elhex
call write
jmp key

Dhex:
mov dx,dhhex
call write
jmp key

dhex:
mov dx,dlhex
call write
jmp key

Chex:
mov dx,chhex
call write
jmp key

chex:
mov dx,clhex
call write
jmp key

Bhex:
mov dx,bhhex
call write
jmp key

bhex:
mov dx,blhex
call write
jmp key

dspace:
mov dx,space
call write
jmp key

Ahex:
mov dx,ahhex
call write
jmp key

ahex:
mov dx,alhex
call write
jmp key

write:
mov ah,9  ;Function 09 - Display string
int 21h   ;Call vector 21 - CMD
ret


ascii:
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
ret

ascii1:
mov ah,02h
mov dx,178
int 21h
ret

dispasc:
mov ah,02h
mov dx,178
int 21h
ret

[SEGMENT .data]		;Initialised data segment

main db "               Press any key to display its hexidecimal value.", 13, 10, "$"
main2    db "                  Press ESCAPE to exit.", 13, 10, "$"
eol db " ", 13, 10, "$"

space db " ", "$"    ;Spacebar
alhex db "a = 61h, ", "$"   ;Lowercase a
ahhex db "A = 41h, ", "$"   ;Upper A
blhex db "b = 62h, ", "$"   ;Lower b
bhhex db "B = 42h, ", "$"   ;Upper B
clhex db "c = 63h, ", "$"   ;Lower c
chhex db "C = 43h, ", "$"   ;Upper C
dlhex db "d = 64h, ", "$"   ;Lower d
dhhex db "D = 44h, ", "$"   ;Upper D
elhex db "e = 65h, ", "$"   ;Lower e
ehhex db "E = 45h, ", "$"   ;Upper E
flhex db "f = 66h, ", "$"   ;Lower f
fhhex db "F = 46h, ", "$"   ;Upper F
glhex db "g = 67h, ", "$"   ;Lower g
ghhex db "G = 47h, ", "$"   ;Upper G
hlhex db "h = 68h, ", "$"   ;Lower h
hhhex db "H = 48h, ", "$"   ;Upper H
ilhex db "i = 69h, ", "$"   ;Lower i
ihhex db "I = 49h, ", "$"   ;Upper I
jlhex db "j = 6Ah, ", "$"   ;Lower j
jhhex db "J = 4Ah, ", "$"   ;Upper J
klhex db "k = 6Bh, ", "$"   ;Lower k
khhex db "K = 4Bh, ", "$"   ;Upper K
llhex db "l = 6Ch, ", "$"   ;Lower l
lhhex db "L = 4Ch, ", "$"   ;Upper L
mlhex db "m = 6Dh, ", "$"   ;Lower m
mhhex db "M = 4Dh, ", "$"   ;Upper M
nlhex db "n = 6Eh, ", "$"   ;Lower n
nhhex db "N = 4Eh, ", "$"   ;Upper N
olhex db "o = 6Fh, ", "$"   ;Lower o
ohhex db "O = 4Fh, ", "$"   ;Upper O
plhex db "p = 70h, ", "$"   ;Lower p
phhex db "P = 50h, ", "$"   ;Upper P
qlhex db "q = 71h, ", "$"   ;Lower q
qhhex db "Q = 51h, ", "$"   ;Upper Q
rlhex db "r = 72h, ", "$"   ;Lower r
rhhex db "R = 52h, ", "$"   ;Upper R
slhex db "s = 73h, ", "$"   ;Lower s
shhex db "S = 53h, ", "$"   ;Upper S
tlhex db "t = 74h, ", "$"   ;Lower t
thhex db "T = 54h, ", "$"   ;Upper T
ulhex db "u = 75h, ", "$"   ;Lower u
uhhex db "U = 55h, ", "$"   ;Upper U
vlhex db "v = 76h, ", "$"   ;Lower v
vhhex db "V = 56h, ", "$"   ;Upper V
wlhex db "w = 77h, ", "$"   ;Lower w
whhex db "W = 57h, ", "$"   ;Upper W
xlhex db "x = 78h, ", "$"   ;Lower x
xhhex db "X = 58h, ", "$"   ;Upper X
ylhex db "y = 79h, ", "$"   ;Lower y
yhhex db "Y = 59h, ", "$"   ;Upper Y
zlhex db "z = 7Ah, ", "$"   ;Lower z
zhhex db "Z = 6Ah, ", "$"   ;Upper Z

Another way to print out the value of a byte in hEx is:

printb: ; Byte with value to print is in AL
 push ax
 shr al, 4
 call printasc
 pop ax
 push ax
 and al, 0xf
 call printasc
 pop ax
 ret

printasc:
 add al, 0x30
 cmp al, 0x39
 jle printasc_e
 add al, 0x7
 printasc_e:
 mov dl, al
 mov ah, 0x2
 int 0x21
 ret
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.