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

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 '$'

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

mov ax,3
int 10h

mov bx,7fffh
x:mov cx,0ffffh
x1:loop x1
dec bx
jnz x


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'
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

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

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

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

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

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

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

mov dx,eol
call write
jmp key

mov dx,zhhex
call write
jmp key

mov dx,zlhex
call write
jmp key

mov dx,yhhex
call write
jmp key

mov dx,ylhex
call write
jmp key

mov dx,xhhex
call write
jmp key

mov dx,xlhex
call write
jmp key

mov dx,whhex
call write
jmp key

mov dx,wlhex
call write
jmp key

mov dx,vhhex
call write
jmp key

mov dx,vlhex
call write
jmp key

mov dx,uhhex
call write
jmp key

mov dx,ulhex
call write
jmp key

mov dx,thhex
call write
jmp key

mov dx,tlhex
call write
jmp key

mov dx,shhex
call write
jmp key

mov dx,slhex
call write
jmp key

mov dx,rhhex
call write
jmp key

mov dx,rlhex
call write
jmp key

mov dx,qhhex
call write
jmp key

mov dx,qlhex
call write
jmp key

mov dx,phhex
call write
jmp key

mov dx,plhex
call write
jmp key

mov dx,ohhex
call write
jmp key

mov dx,olhex
call write
jmp key

mov dx,nhhex
call write
jmp key

mov dx,nlhex
call write
jmp key

mov dx,mhhex
call write
jmp key

mov dx,mlhex
call write
jmp key

mov dx,lhhex
call write
jmp key

mov dx,llhex
call write
jmp key

mov dx,khhex
call write
jmp key

mov dx,klhex
call write
jmp key

mov dx,jhhex
call write
jmp key

mov dx,jlhex
call write
jmp key

mov dx,ihhex
call write
jmp key

mov dx,ilhex
call write
jmp key

mov dx,hhhex
call write
jmp key

mov dx,hlhex
call write
jmp key

mov dx,ghhex
call write
jmp key

mov dx,dlhex
call write
jmp key

mov dx,fhhex
call write
jmp key

mov dx,flhex
call write
jmp key

mov dx,ehhex
call write
jmp key

mov dx,elhex
call write
jmp key

mov dx,dhhex
call write
jmp key

mov dx,dlhex
call write
jmp key

mov dx,chhex
call write
jmp key

mov dx,clhex
call write
jmp key

mov dx,bhhex
call write
jmp key

mov dx,blhex
call write
jmp key

mov dx,space
call write
jmp key

mov dx,ahhex
call write
jmp key

mov dx,alhex
call write
jmp key

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

call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc
call dispasc

mov ah,02h
mov dx,178
int 21h

mov ah,02h
mov dx,178
int 21h

[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

Edited 7 Years Ago by Goalatio: n/a

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

 add al, 0x30
 cmp al, 0x39
 jle printasc_e
 add al, 0x7
 mov dl, al
 mov ah, 0x2
 int 0x21
This article has been dead for over six months. Start a new discussion instead.