Tuesday, 1 March 2016

Assembly Program to count no. of character in a string

;Assembly program to count no. of character in a stringorg 100h 
;code section
mov cl, 0               ;clear counter
mov bx, offset string   ;load address of string

repeat:                
mov al, [bx]            ;load first byte of string
cmp al, '$'             ;check is it end of string
je endit                ;if yes then goto endit label
inc bx                  ;else increase bx by 1
inc cl                  ;count this byte as string character
jmp repeat              ;goto repeat label to check next byte

endit:
add cl, '0'             ;convert byte to ASCII code
mov offset len, cl      ;save in memory

mov cx, 0               ;clear counter

;print output message
mov ah, 09h            
mov dx, offset msg
int 21h
mov dx, offset len
int 21h

ret

;data section
len db '0','$' 
string db "abcdefgh",'$'  
msg db "Length is : ",'$'



Assembly language program to read an array of numbers and find the minimal and maximal elements


;Asm program to compare an array to find minimal and maximum number
;maximal no. is stored in ah and minimal in al at the end of program
org 100h
; add your code here
Lea bx, ar          ;load address of array
mov cx, 1           ;start counter
mov al, [bx]        ;load first no. in al
mov ah, [bx]        ;load first no. in ah too

repeat:
cmp cx, 6           ;check counter for end of array
je endit            ;if yes then goto endit label
inc cx              ;increase counter by 1
inc bx              ;increase pointer move to next no. in array
mov dl, [bx]        ;load next no. in dl

cmp al, dl          ;compare previous and next no. in al and dl
jl skip1            ;if al < dl then goto skip1 label
mov al, dl          ;else load value of dl into al
jmp repeat          ;continue to next no.

skip1:
cmp ah, dl          ;compare ah and dl
jg repeat           ;if ah > dl then continue to next no.
mov ah, dl          ;else load value of dl into ah
jmp repeat          ;continue to next no.

endit:
ret

ar db 15,4,25,64,9,12  ;array



Assembly Program to convert string from lower case to upper case

 
;Program to convert string from lower case to upper case      org 100h
; add your code here
mov bx, offset str      ;load address of fisrt byte of str

repeat:
mov al, [bx]            ;load first byte of address store in bx
cmp al, '$'             ;check is it $ i.e. end of string
je print                ;if yes then goto print label

sub al, 20h             ;otherwise convert it into upper case by subtracting 20h
mov [bx], al            ;move back to str
inc bx                  ;move to next byte of str
jmp repeat              ;goto repeat

;this code section print str
print:
mov ah, 09h
mov dx, offset str
int 21h
ret
;data section
str db 'abcdefghijklmnopqrstuvwxyz','$'

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