Friday, 27 September 2013

DECIMAL TO OCTAL NUMBER.

org 00h
mov r0,#98d
mov 0f0h,#8d
mov a,r0
div ab
mov r6,a
mov r7,b
mov b,#8d
div ab
mov r5,a ;msb
mov a,b
swap a
add a,r7;a contains lsb bits
end

SORTING IN DESCENDING ORDER.

ORG 00H
MOV 30H,#05H
MOV 31H,#07H
MOV 32H,#09H
MOV 33H,#01H
MOV R7,#04H;ITERATION COUNTER

UP1:MOV R1,#03H ; NO OF ELEMENTS
MOV R0,#30H
UP: MOV A,@R0
INC R0
SUBB A,@R0
JC XCHANGE
SJMP LOOP
XCHANGE:MOV A,@R0
DEC R0
XCH A,@R0
INC R0
MOV @R0,A
LOOP: DJNZ R1,UP
DJNZ R7,UP1
END

DECIMAL TO HEXADECIMAL NUMBER.

;DEC TO HEX
 ORG 00H
 MOV R0,#56H; R0=Input byte
 MOV A,R0
 ANL A,#0F0H
 SWAP A
 MOV B,#0AH
 MUL AB
 MOV R1,A
 MOV A,R0
 ANL A,#0FH
 ADD A,R1
 MOV R1,A  ;R1=OUTPUT BYTE
 END 

HEXADECIMAL TO DECIMAL PROGRAM

org 00h
 MOV R0,#0FH ;Input no
 MOV A,R0
 MOV B,#64H; B=64h
 DIV AB
 MOV R1,A ;First dgt
 MOV A,B
 MOV B,#0AH; B=0Ah
 DIV AB
 MOV R2,A ;Second dgt
 MOV A,B
 MOV R3,B ;Third dgt
 MOV A,R2
 SWAP A
 ADD A,R3
 MOV R2,A
 end
;RESULT IS STORED IN R2

Assume that characters are available and stored at memory location as a,b,p,c,z,d,s,e store them at same location without using any buffer as a,z,b,p,c

org 00h
mov r2,#05h         ;give 5 values
up1:mov r3,#04h
mov r0,#30h
up:mov a,@r0
inc r0
subb a,@r0
jnc xchange
sjmp down
xchange:mov a,@r0
dec r0
xch a,@r0
inc r0
mov @r0,a
down: djnz r3,up
 djnz r2,up1
 mov r0,#30h
mov r1,#34h
mov r3,#02h
l2:inc r0
mov a,@r0
xch a,@r1
mov @r0,a
djnz r3,l2
end

Wednesday, 25 September 2013

QUICK SORT

#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
  int x[20],size,i;
  printf("Enter size of the array: ");
  scanf("%d",&size);
  printf("Enter %d elements: ",size);
  for(i=0;i<size;i++)
    scanf("%d",&x[i]);
  quicksort(x,0,size-1);
  printf("Sorted elements: ");
  for(i=0;i<size;i++)
    printf(" %d",x[i]);
  return 0;
}
void quicksort(int x[10],int first,int last){
    int pivot,j,temp,i;
     if(first<last){
         pivot=first;
         i=first;
         j=last;
         while(i<j){
             while(x[i]<=x[pivot]&&i<last)
                 i++;
             while(x[j]>x[pivot])
                 j--;
             if(i<j){
                 temp=x[i];
                  x[i]=x[j];
                  x[j]=temp;
             }
         }
         temp=x[pivot];
         x[pivot]=x[j];
         x[j]=temp;
         quicksort(x,first,j-1);
         quicksort(x,j+1,last);
    }
}
Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8

SELECTION SORT

#include<stdio.h>
int main(){
  int s,i,j,temp,a[20];
  printf("Enter total elements: ");
  scanf("%d",&s);
  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);
  for(i=0;i<s;i++){
      for(j=i+1;j<s;j++){
           if(a[i]>a[j]){
               temp=a[i];
              a[i]=a[j];
              a[j]=temp;
           }
      }
  }
  printf("After sorting is: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);
  return 0;
}

Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 21 7
The array after sorting is:  0 4 5 7 21

simple insertion sort

#include<stdio.h>
int main(){
  int i,j,s,temp,a[20];
  printf("Enter total elements: ");
  scanf("%d",&s);
  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);
  for(i=1;i<s;i++){
      temp=a[i];
      j=i-1;
      while((temp<a[j])&&(j>=0)){
      a[j+1]=a[j];
          j=j-1;
      }
      a[j+1]=temp;
  }
  printf("After sorting: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);
  return 0;
}
Output:
Enter total elements: 5
Enter 5 elements: 3 7 9 0 2
After sorting:  0 2 3 7 9

Wednesday, 11 September 2013

WRITE ASM PROGRAM TO SORT N NUMBERS.


org 00h
mov 20h,05h
mov 21h,04h
mov 22h,01h
mov 23h,07h
mov r1,#04h
mov r2,#20h
l3:mov r0,r2
inc r0
l2:mov r3,r1
mov a,@r2
inc r2
subb a,@r0
jc l1
xch a,@r0
mov @r0,a
l1:djnz r3,l2
djnz r1,l3
end

DESIGN A SIMPLE CALCULATOR.


;simple calculator
org 00h
mov r0,#05h
mov r1,#09h
lcall ad
lcall su
lcall mu
lcall di
ad:mov a,r0
add a,r1
mov r2,a
ret
su:mov a,r0
subb a,b
mov r3,a
ret
mu:mov a,r0
mov b,r1
mul ab
mov r4,a
mov r5,b
ret
di:mov a,r0
mov b,r1
div ab
mov r6,a
mov r7,b
ret
end

CHECK WEATHER THE GIVEN 8 BIT NUMBER IS PRIME OR NOT.


;to check the number is prime or not
org 00h
mov r0,#0ah;this is the no to check
mov r2,#00h ;for storing result
mov a,r0
mov r1,a
l2:mov a,r0
mov b,r1
div ab
mov a,b
cjne a,#00h,l1
inc r2;if r2 is 02 then the number is prime if not it is not prime
l1:djnz r1,l2
end

CHECK WEATHER THE GIVEN NUMBER IS PALINDROME OR NOT.


;to check a number is palindrom or not
org 00h
mov r0,#11h
mov a,r0
swap a
cjne a,00h,l1
mov r1,#09h
l1:mov r2,#09h
end ;if r1 and r2 are 09h then the number is palindrom if only r2 is 09h then the number is not palindrom

Tuesday, 3 September 2013

C Program to Implement Queue Data Structure using Linked List

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int info;
    struct node *ptr;
}*front,*rear,*temp,*front1;

int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();

int count = 0;

void main()
{
    int no, ch, e;

    printf("\n 1 - Enque");
    printf("\n 2 - Deque");
    printf("\n 3 - Front element");
    printf("\n 4 - Empty");
    printf("\n 5 - Exit");
    printf("\n 6 - Display");
    printf("\n 7 - Queue size");
    create();
    while (1)
    {
        printf("\n Enter choice : ");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("Enter data : ");
            scanf("%d", &no);
            enq(no);
            break;
        case 2:
            deq();
            break;
        case 3:
            e = frontelement();
            if (e != 0)
                printf("Front element : %d", e);
            else
                printf("\n No front element in Queue as queue is empty");
            break;
        case 4:
            empty();
            break;
        case 5:
            exit(0);
        case 6:
            display();
            break;
        case 7:
            queuesize();
            break;
        default:
            printf("Wrong choice, Please enter correct choice  ");
            break;
        }
    }
}

/* Create an empty queue */
void create()
{
    front = rear = NULL;
}

/* Returns queue size */
void queuesize()
{
    printf("\n Queue size : %d", count);
}

/* Enqueing the queue */
void enq(int data)
{
    if (rear == NULL)
    {
        rear = (struct node *)malloc(1*sizeof(struct node));
        rear->ptr = NULL;
        rear->info = data;
        front = rear;
    }
    else
    {
        temp=(struct node *)malloc(1*sizeof(struct node));
        rear->ptr = temp;
        temp->info = data;
        temp->ptr = NULL;

        rear = temp;
    }
    count++;
}

/* Displaying the queue elements */
void display()
{
    front1 = front;

    if ((front1 == NULL) && (rear == NULL))
    {
        printf("Queue is empty");
        return;
    }
    while (front1 != rear)
    {
        printf("%d ", front1->info);
        front1 = front1->ptr;
    }
    if (front1 == rear)
        printf("%d", front1->info);
}

/* Dequeing the queue */
void deq()
{
    front1 = front;

    if (front1 == NULL)
    {
        printf("\n Error: Trying to display elements from empty queue");
        return;
    }
    else
        if (front1->ptr != NULL)
        {
            front1 = front1->ptr;
            printf("\n Dequed value : %d", front->info);
            free(front);
            front = front1;
        }
        else
        {
            printf("\n Dequed value : %d", front->info);
            free(front);
            front = NULL;
            rear = NULL;
        }
        count--;
}

/* Returns the front element of queue */
int frontelement()
{
    if ((front != NULL) && (rear != NULL))
        return(front->info);
    else
        return 0;
}

/* Display if queue is empty or not */
void empty()
{
     if ((front == NULL) && (rear == NULL))
        printf("\n Queue empty");
    else
       printf("Queue not empty");
}

C Program to Implement a Queue using an Array

 #include <stdio.h>
    #define MAX 50
    int queue_array[MAX];

    int rear = - 1;

    int front = - 1;

    main()

    {

        int choice;

        while (1)

        {

            printf("1.Insert element to queue \n");

            printf("2.Delete element from queue \n");

            printf("3.Display all elements of queue \n");

            printf("4.Quit \n");

            printf("Enter your choice : ");

            scanf("%d", &choice);

            switch (choice)

            {

                case 1:

                insert();

                break;

                case 2:

                delete();

                break;

                case 3:

                display();

                break;

                case 4:

                exit(1);

                default:

                printf("Wrong choice \n");

            } /*End of switch*/

        } /*End of while*/

    } /*End of main()*/

    insert()

    {

        int add_item;

        if (rear == MAX - 1)

        printf("Queue Overflow \n");

        else

        {

            if (front == - 1)

            /*If queue is initially empty */

            front = 0;

            printf("Inset the element in queue : ");

            scanf("%d", &add_item);

            rear = rear + 1;

            queue_array[rear] = add_item;

        }

    } /*End of insert()*/

    

    delete()

    {

        if (front == - 1 || front > rear)

        {

            printf("Queue Underflow \n");

            return ;

        }

        else

        {

            printf("Element deleted from queue is : %d\n", queue_array[front]);

            front = front + 1;

        }

    } /*End of delete() */

    display()

    {

        int i;

        if (front == - 1)

            printf("Queue is empty \n");

        else

        {

            printf("Queue is : \n");

            for (i = front; i <= rear; i++)

                printf("%d ", queue_array[i]);

            printf("\n");

        }

    } /*End of display() */

Source code of simple bubble sort implementation using array ascending order in c programming language

#include<stdio.h>
int main(){
  int s,temp,i,j,a[20];
  printf("Enter total numbers of elements: ");
  scanf("%d",&s);
  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);
  //Bubble sorting algorithm
  for(i=s-2;i>=0;i--){
      for(j=0;j<=i;j++){
           if(a[j]>a[j+1]){
               temp=a[j];
              a[j]=a[j+1];
              a[j+1]=temp;
           }
      }
  }
  printf("After sorting: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);
  return 0;
}

Source code of simple merge sort implementation using array in ascending order in c programming language

#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main(){
   
    int merge[MAX],i,n;
    printf("Enter the total number of elements: ");
    scanf("%d",&n);
    printf("Enter the elements which to be sort: ");
    for(i=0;i<n;i++){
         scanf("%d",&merge[i]);
    }
    partition(merge,0,n-1);
    printf("After merge sorting elements are: ");
    for(i=0;i<n;i++){
         printf("%d ",merge[i]);
    }
   return 0;
}
void partition(int arr[],int low,int high){
    int mid;
    if(low<high){
         mid=(low+high)/2;
         partition(arr,low,mid);
         partition(arr,mid+1,high);
         mergeSort(arr,low,mid,high);
    }
}
void mergeSort(int arr[],int low,int mid,int high){
    int i,m,k,l,temp[MAX];
    l=low;
    i=low;
    m=mid+1;
    while((l<=mid)&&(m<=high)){
         if(arr[l]<=arr[m]){
             temp[i]=arr[l];
             l++;
         }
         else{
             temp[i]=arr[m];
             m++;
         }
         i++;
    }
    if(l>mid){
         for(k=m;k<=high;k++){
             temp[i]=arr[k];
             i++;
         }
    }
    else{
         for(k=l;k<=mid;k++){
             temp[i]=arr[k];
             i++;
         }
    }
   
    for(k=low;k<=high;k++){
         arr[k]=temp[k];
    }
}

output:
Enter the total number of elements: 5
Enter the elements which to be sort: 25 45 85 45 12
After merge sorting elements are: 12 25 45 45 85