Tuesday 1 March 2016

Assembly Program to convert a number to ASCII string


;Assembly program to convert a number to ASCII string
org 100h   
jmp start               ;to start of code
number dw 19348         ;number to display
string db 10 dup('$')   ;string to store number in string format

start:
mov ax, number          ;load number

printnum:
mov cx, 0               ;set counter to 0, this will count no. of digits in number   
mov bx, 10              ;set divider value 10
repeat:      
mov dx, 0               ;set remainder empty
div bx                  ;divide ax by bx
add dx, '0'             ;convert to byte to digit
push dx                 ;push digit on stack
inc cx                  ;count digit
cmp ax, 0               ;check remaining num is 0
jne repeat              ;if not then goto repeat label

mov bx, offset string   ;load address of first byte of string
loadstring:
pop dx                  ;load digit from stack
mov [bx], dl            ;move digit to string
inc bx                  ;move to next byte of string
dec cx                  ;decrease counter
cmp cx, 0               ;check counter is 0
jne loadstring          ;if not then goto label loadstring

;print string generated
printstring:           
mov ah, 09h            
mov dx, offset string   ;load address of first byte of string ended by $ sign

int 21h                 ;DOS interrupt

ret                     ;return control to O/S




No comments:

Post a Comment