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




Friday, 28 March 2014

Induction v/s Deduction

Induce and Deduce

Induce : a form of reasoning under which a conclusion is drawn on the basis of a large number of positive examples. For example, after seeing a large number of cows, we conclude a cow has four legs, white colour, two horns symmetrically placed on the head etc. Inductive reasoning, though usually leads to correct conclusions, yet the conclusions may not be irrefutable.

Deduce/Infer : to derive as a conclusion from something known or assumed. It is an irrefutable form of reasoning. By irrefutable form of reasoning we mean that the conclusion arrived at through deductive (i.e., any irrefutable) reasoning process is always correct, if the hypotheses (or given facts) are correct. This is the form of reasoning which is dominantly applied in Mathematics.

String Search and Replacement function in C Language

/* A case-sensitive string replacement program
The Basic principle behind this program depends upon three cases
Case 1: If replacement string equals to 'find' string in length
    then no. more space is required so character are simply replaced
Case 2:    If replacement string is less than 'find' string in length
    then some is created after replacing the characters
    so another loop is required for removing that holes
Case 3:    If replacement string is greater than 'find' string in length
    then more space is required to be created between characters for
    replacement string, after that replacement is done.
*/

//Note: This program can be modified by using list and stack
//    to make it more efficient, robust and  flexible.
#include<stdio.h>
#include<conio.h>
#include<string.h>

char* replace(char[],char[],char[]);

void main ()
{
    char str[50]="Look Hook Nook";
    clrscr();
    printf("Before: %s",str);
    printf("\nAfter: %s", replace(str,"k","am"));
    printf("\nBefore: %s",str);
    printf("\nAfter: %s", replace(str,"Loo","S"));
    printf("\nBefore: %s",str);
    printf("\nAfter: %s", replace(str,"Hoo","Dre"));
    printf("\nBefore: %s",str);
    printf("\nAfter: %s", replace(str,"ooam","ame"));
    getch();

}

// This function can work with max. of 100 occurence of 'to find' string
// due to static declaration of memory limit.
char* replace(char str[],char find[],char rep[]){
    int occ[100],i,j,loc,c=0,next,start;
    int lens,lenf,lenr;
    int diff,noOfBytes;
    lens=strlen(str);
    lenf=strlen(find);
    lenr=strlen(rep);
    //find the no. of occurence of 'find' string
    for(i=0;i<lens;i++) {
        loc=i;
        for(j=0;j<lenf;j++){
            if(str[i+j]!=find[j]){
                loc=-1;
                break;
            }
        }
        if(loc!=-1){
            occ[c]=loc;
            c++;
        }

    }
    //check difference between length of 'find' and 'replace' strings
    diff=lenr-lenf;
    //Case 1: When length of both strings is equal
    if(diff==0){
        for(i=0;i<c;i++){
            for(j=0;j<lenr;j++){
                str[occ[i]+j]=rep[j];
            }
        }
    }
    //Case 2: When length of replace string < length of find string
    else if(diff<0){
        //replace characters
        for(i=0;i<c;i++){
            for(j=0;j<lenr;j++){
                str[occ[i]+j]=rep[j];
            }
        }
        //fill holes
        for(i=0;i<c;i++){
            next=lens+diff*c;
            if(i<c-1) next=occ[i+1]-1;
            for(j=occ[i]+lenr-i;j<=next;j++){
                str[j]=str[j-diff*(i+1)];
            }
        }
        //append null character at end of string
        for(i=lens-1;i>=lens+diff*c;i--){
            str[i]='\0';
        }
    }
    //Case 3: When length of replace string > length of find string
    else if(diff>0){
        //make space
        next=lens-1;
        for(i=c-1;i>=0;i--){
            for(j=next;j>=occ[i];j--){
                str[j+diff*c-c+i+1]=str[j];
            }
            next=occ[i];
        }
        //replace characters
        for(i=0;i<c;i++){
            for(j=0;j<lenr;j++){
                str[occ[i]+j+i]=rep[j];
            }
        }

    }
    return str;                 //return result
}    //End of function
//END OF PROGRAM