PDA

View Full Version : آموزش: تمرین های اموزشی



irangavedany
شنبه 05 تیر 1389, 12:16 عصر
چند تا تمرین ساده برای درس زبان ماشین بر پایه cنوشتم امیدوارم به درد دوستان بخوره

irangavedany
شنبه 05 تیر 1389, 12:25 عصر
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void main()
{
char s[20];
char s1[40];

int i=0;
char c=0;
clrscr();
scanf("%s",&s);
randomize();

asm{
mov ax,0B800h
mov dx,4141h
mov es,ax
mov si,0
}

l1:

c=random(9)+65;
asm{
mov dh,41
mov dl,c
mov es:[si],dx
add si,2
inc i
cmp i,2000
jnz l1

}

int l1;
l1=strlen(s);
asm{
mov i,0
lea SI,s
lea DI,s1
mov cx,l1
}
l2:
asm{
mov ah,[si]
mov al,41h
mov [di],ah
mov [di+1],al
add di,2
inc si
inc i
cmp i,cx
jnz l2
}
/*
gotoxy(0,0);
textcolor(1);
textbackground(4);
cprintf("hamid");

gotoxy(1,1);
textcolor(1);
textbackground(4);
cprintf("hamid");
gotoxy(2,2);
textcolor(1);
textbackground(4);
cprintf("hamid");
*/
///************************

char x,y;

int adr;
///////
int j=l1;
asm {
mov i,0
}
l3:

x=random(80)+1;
y=random(25)+1;
asm{
mov ax,0B800h
mov es,ax
mov al,x
mov bl,80
mul bl
mov bl,y
mov bh,0
add ax,bx
mov bx,2
mul bx
lea si,s1
mov di,ax
mov adr,di
}
//printf("%d\n",adr);
asm{
mov ax,l1
mov j,ax
}
l4:
asm{
lodsw
stosw
dec j
cmp j,0
jnz l4

inc i
cmp i,30
jnz l3
}
int asi;
///***************************find
int count=0;
asm{
mov cx,2000
mov ax,0B800h
mov es,ax
mov di,0
lea si,s1
mov ax,[si]
mov asi,si
}
l6:
asm{

scasw
loopnz l6
mov dx,cx
mov bx,di
mov cx,l1
sub di,2
}
loo:
asm{
cmpsw
loopz loo
sub si,2
cmp cx,0

jnz loo2
inc count
mov si,asi
}
loo2:
asm{

mov si,asi
mov cx,dx
mov di,bx
add di,2
//dec cx
//add di,2
cmp cx,0
jz end

jmp l6

}
end:
getch();
//clrscr();
textcolor(15);
textbackground(0);
gotoxy(1,1);
cprintf(" there is(are) %d item(s). ",count);
getch();
}

irangavedany
شنبه 05 تیر 1389, 12:33 عصر
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//search
main()
{
clrscr();
int num,max;
int arr[20];
//...................................
cout<<"list aray arr :"<<endl;
for(int i=0;i<20;i++)
{
arr[i]=random(90)+10;
cout<<"i="<<i<<"a[]="<<arr[i]<<endl;
}
// max=arr[0];
asm{
lea si,arr
mov ax,[si]
mov max,ax
add si,2
}
//..........................................
// for(i=1;i<20;i++)
// {
asm{
mov i,1
}
ll:
asm{

cmp i,20
jnl end

}
//......................................
// if(arr[i]>max)
// max=arr[i];
//....................................
asm{
mov cx,[si]
cmp cx,max
jng exitif
mov max,cx
}
exitif:
asm{
add si,2
add i,1
jmp ll
}
// }
//.................................................. ......
end:
cout<<".............max:"<<max<<"..............";
getch();
}

irangavedany
شنبه 05 تیر 1389, 12:38 عصر
#include <stdio.h>
#include <conio.h>
#include <string.h>
char x[20];
char y[20];
char result[20];
main()
{
char str1[20];
char str2[20];
int i,j;
clrscr();
scanf("%s",&str1);
scanf("%s",&str2);
i=strlen(str1);
j=19;
loop1:

asm{
lea SI,x
lea DI,str1
mov bx,i
dec bx
mov cl,[DI+BX]
mov BX,j
sub cl,48
mov [SI+bx],cl
dec i
dec j
cmp i,0
jne loop1

}

i=strlen(str2);
j=19;
loop2:

asm{
lea SI,y
lea DI,str2
mov bx,i
dec bx
mov cl,[DI+BX]
mov BX,j
sub cl,48
mov [SI+bx],cl
dec i
dec j
cmp i,0
jne loop2

}
//add calc\
char temp,carry;
asm{
mov j,20
mov carry,0
mov temp,0
}
loop3:
asm{
lea SI,x
lea DI,y
mov bx,j
dec bx
mov al,[SI+BX]
mov ah,[DI+BX]
mov cl,0
add cl,carry
add al,ah
add cl,al
mov ah,0
mov al,cl
mov cl,10
div cl
mov carry,al
mov temp,ah
lea SI,result
mov al,temp
mov [SI+BX],al
dec j
cmp j,0
jne loop3
}


for(i=0;i<=19;i++)
printf("%d",x[i]);

printf("\n");
for(i=0;i<=19;i++)
printf("%d",y[i]);
printf("add result\n");
for(i=0;i<=19;i++)
printf("%d",result[i]);
printf("\n");
///sub
asm{
mov temp,0
mov carry,0
mov j,20

}

loop4:
asm{
lea SI,x
lea DI,y
mov bx,j
dec bx
mov al,[SI+BX]
mov ah,[DI+BX]
sub al,carry
cmp al,ah
jl if1
sub al,ah
lea SI,result
mov [SI+BX],al
mov carry,0
jmp l1
}
if1:
asm{
add al,10
sub al,ah
mov carry,1
}
l1:
asm{
dec j
cmp j,0
jne loop4
}
printf("sub result\n");

for(i=0;i<=19;i++)
printf("%d",x[i]);

printf("\n");
for(i=0;i<=19;i++)
printf("%d",y[i]);
printf("\n");
for(i=0;i<=19;i++)
printf("%d",result[i]);



getch();
}

irangavedany
شنبه 05 تیر 1389, 12:43 عصر
#include<iostream.h>
#include<conio.h>
#include <stdlib.h>
main()
{
clrscr();
int arr[20];
int j,i,item;
//..........................
for(int z=0;z<20;z++)
{
arr[z]=random(90)+10;
cout<<arr[z];
}
//..........................

// for(i=19;i>0;i--)
// {
asm{
mov i,19
}
l:
asm{
cmp i,0
jng exitfor1
}
asm{
lea si,arr
}
// for(j=0;j<i;j++)
// {
asm{
mov j,0
}
l2:
asm{
mov dx,j
cmp dx,i
jnl exitfor2
}
//soap..............................
asm{
mov ax,[si]
cmp ax,[si+2]
jng l3
mov item,ax
mov ax,[si+2]
mov ds:[si],ax
mov cx,item
mov [si+2],cx
}
l3:
asm{
add si,2
add j,1
jmp l2
}
//.......................................
exitfor2:
asm{
sub i,1
jmp l
}
// }
// }
exitfor1:
//.................................
cout<<"number sort:"<<endl;
//.................................
for(z=0;z<20;z++)
cout<<arr[z];

getch();
}

irangavedany
شنبه 05 تیر 1389, 12:44 عصر
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//search
main()
{
clrscr();
int num;
int arr[20];
//...................................
for(int i=0;i<20;i++)
{
arr[i]=random(90)+10;
}
cout<<"list aray arr :"<<endl;

for(i=0;i<20;i++)
{
cout<<"i="<<i<<"a[]="<<arr[i]<<endl;

}
//...................................
cout<<"number";
cin>>num;
//..................................

asm{

mov ax,num

mov cx,0

lea si,arr

}

l:

asm{

cmp ds:[si],ax

je found

add si,2

add cx,1

cmp cx,20

jne l

jmp not
}

found:

cout<< "found";

asm{

jmp end

}

not:

cout<<" not found";


end:
getch();

}

irangavedany
شنبه 05 تیر 1389, 12:49 عصر
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
main()
{ clrscr();
int i,num,newnum;
int arr[5];
//..............................................
for(i=0;i<5;i++)
{
arr[i]=random(50)+10;
cout<<"arr[]="<<arr[i]<<endl;
}
//.............................................
cout<<"please insert number"<<endl;
cin>>num;
cout<<"please insert new number"<<endl;
cin>>newnum;
//.............................................
asm{
lea si ,arr

mov ax ,num

mov cx,0

mov dx,newnum
}
//.......................................
l:
asm{
cmp ax,[si]

je found
}
//.......................................
ll:

asm{
add cx,1
add si,2
cmp cx,5
je end
jmp l
}
//.......................................

found:
asm{
mov [si],dx
jmp ll
}
//.......................................
end:
for(i=0;i<5;i++)
{
cout<<"arr[]="<<arr[i]<<endl;
}


getch();
}

irangavedany
شنبه 05 تیر 1389, 12:56 عصر
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
main()
{
clrscr();
int num;
int arr[5];
int result;
//...................................
cout<<"list aray arr :"<<endl;
for(int i=0;i<5;i++)
{
arr[i]=random(90)+10;
cout<<"i="<<i<<"a[]="<<arr[i]<<endl;
}

//for(i=0;i<20;i++)
//{
// result=arr[i]+result;
asm{
lea si,arr
mov i,0
mov dx,0
}
l:

asm{
cmp i,5
jnl end
mov ax,[si]
add dx,ax
add si,2
add i,1
jmp l
}

end:
asm{
mov cx,5
mov ax,dx
mov dx,0

div cx
mov result,ax
}

cout<<"result="<<result;
getch();

}