Blog Archive

Thursday, May 6, 2010

How to print value of a register to screen.

How many times you thought to print result stored in a register to print on screen. This is rather difficult but there is no built in feature to print value stored in a register to screen(like printf("%d") in c).
So i have wrote a code to print result on screen. You can put this code in a separate
subroutine and you can call it any where in your program.

Basic Concept:
You know that only array of ASCII values can be print on screen by interrupt 21h.
I have just converted a number into ASCII string to print it.

So following is the code I wrote.

data segment
numberToBePrint dw 9870
charArray db 10 DUP('$')
stack segment
code segment
mov ax,data
mov ds,ax
;setting ax register to ziro
sub ax,ax
;assigning number to be print on screen

mov ax,numberToBePrint

;first loop
mov cx,10
;divide ax by 10, remainder of divide will store in dx
div cx

;assigning address of first block of charArray to di register
mov di,offset charArray

mov cl,[di]
;assigning remainder in first block of char array
mov [di],dl
;changing numeric number into its ascii equivalent.
;by adding 48 to its value, 48 is ascii code of 0
add [di],48

;second loop
;increment di to point to next block of array.
inc di
;logic for swaping array element to next block
mov bl,[di]
mov [di],cl
mov cl,bl
;if current block contains 0 then get out from loop
cmp cl,'$'
jnz Reapet1

;clearing remainder from dx
sub dx,dx
;checking ax for 0, if ax is 0 then get out from loop
cmp ax,0
jnz Reapet

;printing char array on the screen

mov dx, offset charArray
;9 is the interrupt service number used for screen display
mov ah,9
;calling dos for printing characters starting from location stored in dx
int 21h

;waiting for a key press.
mov ah,1
int 21h
mov ah,1
int 21h

;exit from program
mov ax,4c00h
int 21h
end start

No comments:

Post a Comment