The Programming Project

Sunday, September 29, 2013

PROGRAMMING IN 8086

Fact = N * (N–1)*….2*1  (all are unsigned Word variable)


 TITLE factorial 
;TO COMPUTE
;factorial N <= 8
;----------------------------------------------------------------
     .MODEL SMALL
     .STACK 64
     .DATA
     VAR_N DW 9 
     VAR_FACT DW 1
;----------------------------------------------------------------
     .CODE
MAIN PROC FAR
     MOV AX,@DATA
     MOV DS,AX
     MOV AX,VAR_N 
     MOV DX,01
     CMP AX,00h
     JE SPECIAL
     MOV CX,AX
     MOV BX,AX
     DEC BX      
     DEC CX  
     JZ SPECIAL
     LOOP: MUL BX
           DEC BX 
           DEC CX
           JNZ LOOP         
     MOV DX,AX
     SPECIAL:MOV VAR_FACT,DX
     MOV AX,4C00H
     INT 21H
MAIN ENDP
     END MAIN
    

ROGRAMMING IN 8086

  1. Write an assembly program that converts a number in the word variable D, 0 <= D <= 15 to an ASCII character in the byte variable HEXD which is its hex value. For instance, if D = 3 then HEXD = ‘3’ and if D = 14 then HEXD = ‘E’.


TITLE  word variable to ASCII character
;----------------------------------------------------------------
     .MODEL SMALL
     .STACK 64
     .DATA
     ;ACODE   DB '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
     ACODE DB '0123456789ABCDEF'
     VALUE   DW 7  
     INDEX   DB 0
     HEXVAL  DB '?'  
;----------------------------------------------------------------
     .CODE
MAIN PROC FAR
     MOV AX,@DATA
     MOV DS,AX
     MOV ES,AX
     MOV CX,15
     LOOP:
            MOV AX,CX
            CMP AX,VALUE
            JE GOT
            INC INDEX
            DEC CX
            JNZ LOOP
     GOT:MOV AL,15
     SUB AL,INDEX
     MOV INDEX,AL
     MOV CL,INDEX
     INC CL
     LEA SI,ACODE
     LOOP1:MOV AL,[SI]
           MOV HEXVAL,AL
          INC SI
          DEC CL
          JNZ LOOP1
     MOV AX,4C00H
     INT 21H
MAIN ENDP
     END MAIN  ;END MAIN PROCEDURE
    

Programming in 8086

  1. Write a program to find out the number of positive and negative numbers from a given array of 10 16-bit numbers


TITLE POSITIVE NEGATIVE
;TO COMPUTE THE NUMBER OF
;POSITIVE AND NEGATIVE NUMBERS
;----------------------------------------------------------------
     .MODEL SMALL
     .STACK 64
     .DATA
     VAR_POS DW 00
     VAR_NEG DW 00  
     ARRAY DW 2,-3,4,5,6,-7,8,9,-2,3 ; input to the program
;----------------------------------------------------------------
     .CODE
MAIN PROC FAR
     MOV AX,@DATA  ; SET ADDRESS OF DATA SEGMENT IN DS
     MOV DS,AX
     MOV CX,10d  ; LOOP COUNTER
     MOV SI,OFFSET ARRAY ; LOADING BX WITH OFFSET
     LOOP:      
         CLC
         XOR AX,AX
         MOV AX,[SI] ; MOVING THE POINTED DATA
         RCL AX,1    ; ROTATE TO CHECK FOR MSB
         JC NEGA
         INC VAR_POS ; IF CARRY FLAG IS RESET (0), INCREASE VAR_POS
         JMP NEXT
         NEGA:
             INC VAR_NEG ; IF CARRY FLAG IS SET (1), INCREASE VAR_NEG
         NEXT:
             INC SI   ;TO FETCH THE
             INC SI   ; NEXT WORD
             DEC CX   ; LOOP UNTIL DATA ARE PROCESSED
         JNZ LOOP                                                                    
     MOV AX,4C00H
     INT 21H
MAIN ENDP
     END MAIN  ;END MAIN PROCEDURE
    

Tuesday, September 24, 2013

Combination in Lexicographical Order




#include<stdio.h>
#include<malloc.h>
#include<conio.h>
int main()
{
char alpha[]={'?','A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int n,i,pos,r,tmp,j,flag=1;
long int tot=0;
char *combination;
printf("\n Enter the number of objects (max value is 26):");
scanf("%d",&n);
printf("\n Enter the value of r ( 1<= r <= n):");
scanf("%d",&r);
combination =(char *)malloc(n*sizeof(char));
for(i=1;i<=n;i++)
combination[i]=alpha[i];
printf("\n The combinations are (is):\n");
printf("\t\t      ");
for(i=1;i<=r;i++)
printf("%c",combination[i]);
tot=1;
while(flag)
{
for(i=r;i>=1;i--)
{
if(combination[i]-64==n-r+i)
{
}
else
{
pos=i;
break;
}
}
if(i==0)
{
flag=0;
break;
}
for(j=1;j<=26;j++)
if(combination[pos]==alpha[j])
break;
combination[pos]=alpha[j+1];
for(i=pos+1;i<=r;i++)
combination[i]=alpha[(combination[i-1]-64)+1];
printf("\n");
printf("\t\t      ");
for(i=1;i<=r;i++)
printf("%c",combination[i]);
tot++;
}
printf("\n Total number of combination is: %ld",tot);
return 0;
}


Friday, September 20, 2013

Permutation in Lexicographical Order

This C program prints the permutation of n distinct objects (0 < n <= 26 )  in Lexicographical Order.



#include<stdio.h>
#include<malloc.h>
void sort(char *permutation,int n,int pos);
void replace_min (char *permutation,int n,int pos);
int main()
{
char alpha[]={'A','B','C','D','E','F','G','H','I','J','K','L','M',
          'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int n,i,flag=1,pos;
long int tot=0;
char *permutation;
printf("\n Enter the numbr of objects (max values is 26):");
scanf("%d",&n);
permutation =(char *)malloc(n*sizeof(char));
for(i=0;i<n;i++)
    permutation[i]=alpha[i];
printf("\n The permutations are:\n");
printf("\t\t      ");
for(i=0;i<n;i++)
    printf("%c",permutation[i]);
    tot=1;
while(flag)
    {
    pos=-1;
    for(i=n-1;i>=1;i--)
        {
        if(permutation[i-1]<permutation[i])
             {
             pos=i-1;
             flag=1;
             break;
             }
        }
        if(pos==-1)
            {
            flag=0;
            break;
            }
    replace_min(permutation,n,pos);
    sort(permutation,n,pos);
   printf("\n");
    printf("\t\t      ");
    for(i=0;i<n;i++)
        printf("%c",permutation[i]);
    //tot++;
    //getche();
    }
//printf("\n Total number of permutation is: %ld",tot);
return 0;
}
void sort(char *permutation,int n,int pos)
{
    int i,j;
   char temp='A';
    for(i=pos+1;i<n-1;i++)
        {
        for(j=pos+1;j<n-1;j++)
            {
            if(permutation[j]>permutation[j+1])
                {
                temp=permutation[j+1];
                permutation[j+1]=permutation[j];
                permutation[j]=temp;
                }
            }
        }
  return;
}
void replace_min(char *permutation,int n,int pos)
{
  char min='Z',temp;
  int k,min_pos=0;
  for(k=pos+1;k<n;k++)
    {
        if(permutation[k]>permutation[pos])
            {
            if(permutation[k]<min)
                {
                min=permutation[k];
                min_pos=k;
                }
            }
    }
    temp=permutation[min_pos];
    permutation[min_pos]=permutation[pos];
    permutation[pos]=temp;
    return;
}

Monday, September 16, 2013

Intersection of Sets



To find intersection of two sets through C

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void inter_set(int *s1,int*s2,int n,int m);
int main()
{
int n,m,i;
int *s1,*s2;
printf("\n Enter the number of elements in the sets:");
scanf("%d %d",&n,&m);
s1=(int *)malloc(n*sizeof(int));
s2=(int *)malloc(m*sizeof(int));
printf("\n Enter the elements of the set 1:");
for(i=0;i<n;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s1[i]);
}
printf("\n Enter the elements of the set 2:");
for(i=0;i<m;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s2[i]);
}
inter_set(s1,s2,n,m);
return 0;
}
void inter_set(int *s1,int*s2,int n,int m)
{
int *s,i=0,j,k;
int flag=0;
s=(int *)malloc((m<n ? m: n)*sizeof(int));
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
if(s2[j]==s1[k])
{
flag=1;
break;
}
}

if(flag==1)
{
s[i]=s2[j];
         i++;
flag=0;
}
 if(flag==0)
continue;
 }
 if(i==0)
printf("\n Intersection is Null:");
else
{
  printf("\n Intersection of the entered sets is:");
  printf("\n { ");
  for(j=0;j<i-1;j++)
printf(" %d,",s[j]);
  printf(" %d }\n ",s[i-1]);
}


return;
}