Wednesday, April 16, 2014

multithreading in JAVA

public class ProducerConsumerTest {
   public static void main(String[] args) {
      CubbyHole c = new CubbyHole();
      Producer p1 = new Producer(c, 1);
      Consumer c1 = new Consumer(c, 1);
      p1.start();
      c1.start();
   }
}
class CubbyHole {
   private int contents;
   private boolean available = false;
   public synchronized int get() {
      while (available == false) {
         try {
            wait();
         }
         catch (InterruptedException e) {
         }
      }
      available = false;
      notifyAll();
      return contents;
   }
   public synchronized void put(int value) {
      while (available == true) {
         try {
            wait();
         }
         catch (InterruptedException e) {
         }
      }
      contents = value;
      available = true;
      notifyAll();
   }
}

class Consumer extends Thread {
   private CubbyHole cubbyhole;
   private int number;
   public Consumer(CubbyHole c, int number) {
      cubbyhole = c;
      this.number = number;
   }
   public void run() {
      int value = 0;
         for (int i = 0; i < 10; i++) {
            value = cubbyhole.get();
            System.out.println("Consumer #"
+ this.number
+ " got: " + value);
}
}
}

class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;

public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}

public void run() {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
System.out.println("Producer #" + this.number
+ " put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}

Tuesday, April 15, 2014

File operation in Assembly language 8086

;random access from file
.model small
.stack 64
.data
name1 label byte
maxlen db 5
actlen db ?
record db 5 dup(' '),'$'
nl db 13,10,'$'
filename db 'c:\rec.asm',00h
filehandle1 dw ?
recno dw ?    
error db 10,13,"error $"
msg db 10,13,"recno:- $"
.code
main proc near
mov ax,@data
mov ds,ax

mov ah,3ch
mov cx,00
lea dx,filename
int 21h

jc end1
mov filehandle1,ax
a10:
mov ah,0ah
lea dx,name1
int 21h
cmp actlen,0
jbe a20
mov ah,40h
mov bx,filehandle1
mov cx,5
lea dx,record
int 21h

mov ah,09h
lea dx,nl
int 21h

jmp a10

a20:
mov ah,3eh
mov bx,filehandle1
int 21h

mov ah,3dh
mov al,00
lea dx,filename
int 21h
jc end1

mov filehandle1,ax

mov ah,09h
lea dx,msg
int 21h

mov ah,01h
int 21h

sub al,31h
mov ah,00
mul maxlen
mov recno,ax

mov ah,42h
mov al,00
mov bx,filehandle1
mov cx,00
mov dx,recno
int 21h

mov ah,3fh
mov bx,filehandle1
mov cx,5
lea dx,record
int 21h

mov ah,09h
lea dx,record
int 21h
 
 
mov ah,3eh
mov bx,filehandle1
int 21h
jmp end2
 
 
end1:  
mov ah,09h
lea dx,error
int 21h
end2:
mov ax,4c00h
int 21h
main endp
end main

Monday, April 14, 2014

string copying in assembly language 8086

.model small
.stack 64
.data
str1 db 'pawan samarwal$'
str2 db 20 dup(' '),'$'

.code
main proc far
mov ax,@data
mov ds,ax
mov es,ax
cld
mov cx,20
lea di,str1
lea si,str2
rep movsb
mov ah,09h
lea dx,str1
int 21h
mov ah,4ch
int 21h
main endp
end main

doubly linked list in C++

 a doubly-linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders.


#include<iostream>
#include<iomanip>
template<class s>                                ///templats classclass stack
{
      private:
          int top;
          s item;
          s array[5];
      public:
          stack()
          {
              top=-1;
          }
          void getdata()
          {
                  cout<<"enter item to push in stack"<<endl;
                cin>>item;
              
          }
          void push()
          {
              if(top==5)
                  cout<<"overflow"<<endl;
              else
              {
                  top++;
                  array[top]=item;
              }
          }
          void pop()
          {
              if(top==-1)
                  cout<<"underflow"<<endl;
              else
              {
                  array[top]=NULL;
                  
                  top--;
              }
          }
          void disp()
          {
               for(int i=0;i<=top;i++)
               {
                     cout<<array[i]<<'\t';
               }
                cout<<endl;
          }
};
int main()
{
    stack<int>stacki;
    stack<char>stackc;
    int r;
    char ch;
    do
    {
        cout<<"integer array"<<endl;
        cout<<"press 1 to push element in stack & 2 to pop it"<<endl;  
        cout<<"    3 to display stack"<<endl;
        cin>>r;
        switch (r)
        {
          case 1:
               stacki.getdata();
               stacki.push(); 
               break;
          case 2:
                stacki.pop();
                break;
          case 3:
                stacki.disp();
                break;
          default:
            cout<<"bad input"<<endl;
            break;
        }
        cout<<"do you want to process more y/n"<<endl;
        cin>>ch;
    }
    while(ch!='n');
do
{    
    cout<<"character array"<<endl;
        cout<<"press 1 to push element in stack & 2 to pop it"<<endl;  
        cout<<"    3 to display stack"<<endl;
        cin>>r;
        switch (r)
        {
          case 1:
               stackc.getdata();
               stackc.push(); 
               break;
          case 2:
                stackc.pop();
                break;
          case 3:
                stackc.disp();
                break;
          default:
            cout<<"bad input"<<endl;
            break;
        }
        cout<<"do you want to process more y/n"<<endl;
        cin>>ch;
    }
    while(ch!='n');

    return 0;
}

String comparison in assembly language 8086

DATA SEGMENT
        STR1 DB "ENTER FIRST STRING HERE ->$"
        STR2 DB "ENTER SECOND STRING HERE ->$"
        STR11 DB "FIRST STRING : ->$"
        STR22 DB "SECOND STRING: ->$"

        INSTR1 DB 20 DUP("$")
        INSTR2 DB 20 DUP("$")
        NEWLINE DB 10,13,"$"
        N DB ?
        S DB ?
        MSG1 DB "BOTH STRING ARE SAME$"
        MSG2 DB "BOTH STRING ARE DIFFERENT$"

DATA ENDS

CODE SEGMENT

        ASSUME DS:DATA,CS:CODE
START:

        MOV AX,DATA
        MOV DS,AX

        LEA SI,INSTR1
        LEA DI,INSTR2

;GET STRING
        MOV AH,09H
        LEA DX,STR1
        INT 21H

        MOV AH,0AH
        MOV DX,SI
        INT 21H


        MOV AH,09H
        LEA DX,NEWLINE
        INT 21H

        MOV AH,09H
        LEA DX,STR2
        INT 21H

        MOV AH,0AH
        MOV DX,DI
        INT 21H


        MOV AH,09H
        LEA DX,NEWLINE
        INT 21H


;PRINT THE STRING

        MOV AH,09H
        LEA DX,STR11
        INT 21H

        MOV AH,09H
        LEA DX,INSTR1+2
        INT 21H

        MOV AH,09H
        LEA DX,NEWLINE
        INT 21H

        MOV AH,09H
        LEA DX,STR22
        INT 21H

        MOV AH,09H
        LEA DX,INSTR2+2
        INT 21H

        MOV AH,09H
        LEA DX,NEWLINE
        INT 21H

;STRING COMPARISION
        MOV BX,00

        MOV BL,INSTR1+1
        MOV BH,INSTR2+1

        CMP BL,BH
        JNE L1

        ADD SI,2
        ADD DI,2

      L2:MOV BL,BYTE PTR[SI]
        CMP BYTE PTR[DI],BL
        JNE L1
        INC SI
        INC DI
        CMP BYTE PTR[DI],"$"
        JNE L2

        MOV AH,09H
        LEA DX,MSG1
        INT 21H

        JMP L5

      L1:MOV AH,09H
        LEA DX,MSG2
        INT 21H



     L5:
        MOV AH,09H
        LEA DX,NEWLINE
        INT 21H

        MOV AH,4CH
        INT 21H


CODE ENDS
END START

TSR DIGITAL CLOCK in ASSEMBLY LANGUAGE 8086

I've been trying to write TSR (Terminate-Stay-Resident) programs (in general) in Assembly (16-bit) for MS-DOS. I've read through a Wikipedia page on TSR and also a page on using it specifically in DOS (but it seems to be teaching it in C and not Assembly directly). I've looked at a site with tons of DOS interrupt documentation and find this onethis one, and another most relevant to TSR programs. I can't post all of the links because as a new user I can have up to 2 hyperlinks on a post.
So, I've tried writing a (seemingly) very simple TSR program in real mode flat model (.COM file format) in NASM. Here's the code:

creating digital clock

.model small
.stack 64h
code segment
assume cs:code
begin :
jmp tsr
address dd ?
str db ':','$'
hrs db ?
min db ?
sec db ?
num db 10
h db 2 dup(0)
m db 2 dup(0)
s db 2 dup(0)

clrscrn proc near
mov ax,0600h
mov bh,34h
mov cx,0000h
mov dx,2060h
int 10h
ret
clrscrn endp
set proc near
mov ah,02h
mov bh,00h
mov dh,0ah
mov dl,0ah
int 10h
ret
set endp

getv proc near
mov ah,2ch
int 21h

mov al,ch
mov ah,00
div num
mov bl,ah
mov dl,al
add dl,30h
mov ah,02h
int 21h
mov dl,bl
add dl,30h
mov ah,02h
int 21h
mov ah,09h
lea dx,str
int 21h
mov ah,2ch
int 21h
mov al,cl
mov ah,00h
div num
mov bl,ah
mov dl,al
add dl,30h
mov ah,02h
int 21h
mov dl,bl
add dl,30h
mov ah,02h
int 21h
mov ah,09h
lea dx,str
int 21h
mov ah,2ch
int 21h
mov al,dh
mov ah,00h
div num
mov bl,ah
mov dl,al
add dl,30h
mov ah,02h
int 21h
mov dl,bl
add dl,30h
mov ah,02h
int 21h
ret
getv endp


clock proc near
mov cl,100
b11:
call clrscrn
call set
call getv
call clrscrn
loop b11
ret
clock endp

l1:
mov ax,cs
mov ds,ax
mov es,ax
mov ah,00h
mov al,03h
int 10h
in al,60h
cmp al,01
jne e1
call clock
e1:
jmp dword ptr cs:address

tsr:
mov ax,cs
mov ds,ax
mov es,ax
mov ah,35h
mov al,09h
int 21h
mov word ptr address,bx
mov word ptr address+2,es
mov ah,25h
mov al,09h
mov dx,offset l1
int 21h
mov ah,31h
mov dx,offset tsr
int 21h
code ends
end begin

large number adition using linked list C++

#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
int a2[10],m=0,n=0,a1[10],y=0,r1[10],r2[10],i=0,j=0;
char c1[10],c2[10],b;
node *l1,*l2,*s1,*s2;
cout<<"enter 1st number";
cin.getline(c1,10);
while(c1[i]!='\0')
{
a1[i]=c1[i]-48;
i++;m++;
}
cout<<"\n enter 2nd number";
cin.getline(c2,10);
while(c2[j]!='\0')
{
a2[j]=c2[j]-48;
j++;n++; }
for(i=0;i<m;i++)
cout<<a1[i];
s1=l1;
for(j=m-1;j>0;j--)
{
cout<<"hi";
l1=new node;
l1->data=a1[m-j-1];
l1=l1->next;
}
l1->next=NULL;
l1=s1;
s2=l2;
for(i=n;i>0;i--)
{
l2=new node;
l2->data=a2[n-i];
l2=l2->next;
}
l2->next=NULL;
l2=s2;
cout<<"________+++++++";
while(l1!=NULL)
{
cout<<l1->data;
l1=l1->next;
}}

key identification in assembly language 8086

.model small
.stack 64h
.data
st1 db 'home $'
st2 db 'other $'
CTR DB 00H
.code
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
A20:
MOV AH,10H
INT 16H
CMP Ah,47h
JE B10
MOV AH,09H
LEA DX,ST2
INT 21H

CMP CTR,09H
JNE A50
jmp a20
 B10:
 MOV AH,09H
 LEA DX,ST1
 INT 21H
 JMP A20
a50:
 MOV AH,4CH
 INT 21H
 MAIN ENDP
 END MAIN

screensaver in SP using keyboard input 8086

pawan macro
mov ax,0600h
mov cx,0000h
mov dx,184fh
mov bh,a
int 10h
endm
.model small
.stack 64

.data
ctr db 10h
tik db 'pawan$'
a db 00h
.code
main proc far
mov ax,@data
mov ds,ax

a20:
inc ctr
mov cx,0fh
pawa:
inc a
loop pawa
mov ah,10h
int 16h
cmp al,0e0h
je a10
jmp a20
a10:
pawan
jmp a20

mov ah,4ch
int 21h
main endp
end main

Saturday, April 5, 2014

BINARY TREE Level Printing in c++




The binary tree is a fundamental data structure used in computer science. The binary tree is a useful data structure for rapidly storing sorted data and rapidly retrieving stored data. A binary tree is composed of parent nodes, or leaves, each of which stores data and also links to up to two other child nodes (leaves) which can be visualized spatially as below the first node with one placed to the left and with one placed to the right. It is the relationship between the leaves linked to and the linking leaf, also known as the parent node, which makes the binary tree such an efficient data structure. It is the leaf on the left which has a lesser key value (i.e., the value used to search for a leaf in the tree), and it is the leaf on the right which has an equal or greater key value. As a result, the leaves on the farthest left of the tree have the lowest values, whereas the leaves on the right of the tree have the greatest values. More importantly, as each leaf connects to two other leaves, it is the beginning of a new, smaller, binary tree. Due to this nature, it is possible to easily access and insert data in a binary tree using search and insert functions recursively called on successive leaves. 





#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
struct btree
{
char data;
btree* lchild;
btree* rchild;
};
btree* bcreate(btree* t,char k);
void show(btree *t,int n);
int hgt(btree* t);
int main()
{
btree *p=NULL;
char c,d; int l,k=0;
cout<<"enter data\n";
cin>>c;
p=bcreate(p,c);
cout<<endl;
l=hgt(p);
while(k<=l)
{
show(p,k++);
cout<<endl;
}


return 0;
}
btree* bcreate(btree* t,char k)
{
btree *s=NULL,*r=NULL;char c1,c2;
if(t==NULL && k!='.')
{
t=new(btree);
t->data=k;
t->rchild=NULL; t->lchild=NULL;
cout<<"enter left and right child of "<<k<<endl;
cin>>c1>>c2;
t->lchild=bcreate(s,c1);
t->rchild=bcreate(r,c2);
return t;
}
return t;
}
int hgt(btree* t)
{
if(t==NULL)
return -1;
else
{
int a=hgt(t->lchild);
int b=hgt(t->rchild);
if(a>b)
return a+1;
else
return b+1;
}
}
void show(btree *t,int n)
{
if(n==0 && t!=NULL)
cout<<t->data<<" ";
else
{
show(t->lchild,n-1);
show(t->rchild,n-1);
}

}

Moving CAR in ASSEMBLY LANGUAGE

int 10h is a video BIOS interrupt that is older than dirt and cannot be accessed from user mode in current operating systems

.model small
.stack 64
.data
tik dw ?
a db 05h
b db 05h
c db 08h
d db 0fh
e db 06h
f db 12h
str db '   0     0 ','$'
ctr db 00
.code
main proc far
mov ax,@data
mov ds,ax
pawan:
mov ah,00h
mov al,03h
int 10h
inc ctr
mov ah,02h
mov bh,00
mov dh,09h
mov dl,b
int 10h
mov ah,09h
lea dx,str
int 21h
mov ax,600h
mov bh,61h
mov cx,0a00h
mov dx,0aaah
int 10h
mov ax,600h
mov bh,30h
mov ch,a
mov cl,b
mov dh,c
mov dl,d
int 10h
mov ch,07h
mov cl,e
mov dl,f
int 10h
inc b
inc d
inc e
inc f
call delay
cmp ctr,60
jne pawan
mov ah,4ch
int 21h
main endp


delay proc near
mov ah,00h
int 1ah
mov tik,dx
add tik,1h
b10:
mov ah,00h
int 1ah
cmp tik,dx
jge b10

ret
delay endp

end main

Contributors

Translate