if you want an in depth knowledge of assembly language click here
editing a program in assembly language
An assembly language program can be entered using a text editor available.
ex:notepad,edit,ne
The edited file should be saved with the extension .asm
ex:abc.asm,test.asm
assebling a .asm program
An assembler converts assembly language program to its object code.
masm assembler is masm.exe.usually it is located at c:\masm611\bin
Suppose that your program is in a directory c:\abc and the program name is abc.asm.
Before calling the assembler set the path using
c:\abc\path=%path%;c:\masm611\bin
After setting your path assemble your program using
c:\abc\masm abc.asm
or
c:\abc\masm abc;
If there are errors in the program abc.asm the assembler will display the errors with line numbers.The programmer has to correct the errors and save it.Try to assemble the program again.If the program is error free, it will be converted to an object file, abc.obj.
linking the program
The following commands can be used to link the object program to an executable file abc.exe
c:\abc\link abc.obj
or
c:\abc\link abc;
executing a program
If linking is successful, you can execute the program using the following command line.
c:\abc\abc.exe
or
c:\abc\abc
Your first program("hello.asm")
Program to display "Hello World".
data segment
msg db "Hello world$"
data ends
code segment
start:mov ax,data
mov ds,ax
lea dx,msg
mov ah,9
int 21h
mov ah,4ch
int 21h
code ends
end start
code snippet to read a character from the keyboard
mov ah,1
int 21h
After the execution of the interrupt AL register will hold the ASCII value of the keypressed.
Ex:if you have pressed 0, the content of AL will be 30H
code snippet to print a character on the screen if it is printable and execute a control function if it is control character.
Ex: if you want to print 0 on the screen
mov dl,'0' //moving the character 0 to dl register
or
mov dl,30h
mov ah,2
int 21h
Ex: if you want to execute the carriage return function
mov dl,0dh
mov ah,2
int 21h
code snippet to read a number(less than 255)
in the data segment there is a variable var1 to store the number
ie var1 db 0
in the code segment
l1:mov ah,1
int 21h
cmp al,0dh
jz l2
sub al,30h
mov bl,al
mov bh,var1
mov al,10
mul bh
add al,bl
mov var1,al
jmp l1
l2:
Monday, July 28, 2008
Wednesday, July 23, 2008
NETWORK & OS LAB
read() system call
#include fcntl.h
#include stdio.h
main()
{
int fd;
char ch[100];
fd=open("a.txt",O_RDONLY);
read(fd,&ch,7);
printf("%s",ch);
printf("\n");
}
program to create and write to a file using open() and write() system calls
#include
#include
main()
{
int fd;
fd=open("a.txt",O_CREAT|O_WRONLY,0644);
write(fd,"shibu\n",7);
}
fork() system call
#include unistd.h
#include stdio.h
main()
{
int i,j,pid,n;
printf("enter the delay:");
scanf("%d",&n);
pid=fork();
if (pid==0)/*child process*/
{
for(i=0;i<=100;i+=2)
{
printf("executing child %d\n",i);
}
}
else
{
for (i=1;i<=100;i++)
{
printf("executing parent %d\n",i);
}
}
}
exec() system call
#include unistd.h
#include stdio.h
main()
{
printf("before calling exec\n");
execl("/bin/date","date",NULL);
printf("after calling exec\n");
}
program to create a file using open() and write() system calls
#include fcntl.h
#include stdio.h
main()
{
int fd;
fd=open("a.txt",O_CREAT|O_WRONLY,0644);
write(fd,"shibu\n",7);
}
pipe() system call
#include
#include
void do_parent(int []);
void do_child(int []);
main()
{
int data_pipe[2];
int pid;
int rc;
rc = pipe(data_pipe);
printf("%d %d\n",data_pipe[0],data_pipe[1]);
if (rc == -1) { perror("pipe"); exit(1); }
pid = fork();
switch (pid)
{
case -1:
perror("fork"); exit(1);
perror("fork");
case 0:
do_child(data_pipe);
default:
do_parent(data_pipe);
}
}
void do_parent(int data_pipe[])
{
int c; /* data received from the user. */
int rc;
/* first, close the un-needed read-part of the pipe. */
close(data_pipe[0]);
while ((c = getchar()) > 0)
{
rc = write(data_pipe[1], &c, 1);
if (rc ==-1)
{
perror("Parent:write");close(data_pipe[1]);exit(1);
}
}
close(data_pipe[1]);
}
void do_child(int data_pipe[]) {
int c; /* data received from the parent. */
int rc;
/* first, close the un-needed write-part of the pipe. */
close(data_pipe[1]);
while ((rc = read(data_pipe[0], &c, 1)) > 0)
{
putchar(c);
}
}
SENDING "Hello World" through the pipe
#include stdio.h
#include unistd.h
#include sys/types.h
#include string.h
#include stdlib.h
int main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[] = "Hello, world!\n";
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
/* Child process closes up input side of pipe */
close(fd[0]);
/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)+1));
exit(0);
}
else
{
/* Parent process closes up output side of pipe */
close(fd[1]);
/* Read in a string from the pipe */
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
}
return(0);
}
if you want to know more about pipes click HERE
program for counting the number of words in the output when executing the command ls
#include stdio.h
#include stdlib.h
#include unistd.h
int main()
{
int pfds[2];
pipe(pfds);
if (!fork()) {
close(1); /* close normal stdout */
dup(pfds[1]); /* make stdout same as pfds[1] */
close(pfds[0]); /* we don't need this */
execlp("ls", "ls", NULL);
} else {
close(0); /* close normal stdin */
dup(pfds[0]); /* make stdin same as pfds[0] */
close(pfds[1]); /* we don't need this */
execlp("wc", "wc", "-l", NULL);
}
}
program implementing "ls|more" using pipes
#include
#include
main()
{
int fd[2];
pipe(fd);
switch(fork()){
case -1:perror("fork");
break;
case 0:close(1);
dup(fd[1]);
close(0);
execlp("ls","ls","-l",NULL);
break;
default:close(0);
dup(fd[0]);
execlp("more","more",NULL);
break;
}
}
SHARED MEMORY CLICK HERE
#include fcntl.h
#include stdio.h
main()
{
int fd;
char ch[100];
fd=open("a.txt",O_RDONLY);
read(fd,&ch,7);
printf("%s",ch);
printf("\n");
}
program to create and write to a file using open() and write() system calls
#include
#include
main()
{
int fd;
fd=open("a.txt",O_CREAT|O_WRONLY,0644);
write(fd,"shibu\n",7);
}
fork() system call
#include unistd.h
#include stdio.h
main()
{
int i,j,pid,n;
printf("enter the delay:");
scanf("%d",&n);
pid=fork();
if (pid==0)/*child process*/
{
for(i=0;i<=100;i+=2)
{
printf("executing child %d\n",i);
}
}
else
{
for (i=1;i<=100;i++)
{
printf("executing parent %d\n",i);
}
}
}
exec() system call
#include unistd.h
#include stdio.h
main()
{
printf("before calling exec\n");
execl("/bin/date","date",NULL);
printf("after calling exec\n");
}
program to create a file using open() and write() system calls
#include fcntl.h
#include stdio.h
main()
{
int fd;
fd=open("a.txt",O_CREAT|O_WRONLY,0644);
write(fd,"shibu\n",7);
}
pipe() system call
#include
#include
void do_parent(int []);
void do_child(int []);
main()
{
int data_pipe[2];
int pid;
int rc;
rc = pipe(data_pipe);
printf("%d %d\n",data_pipe[0],data_pipe[1]);
if (rc == -1) { perror("pipe"); exit(1); }
pid = fork();
switch (pid)
{
case -1:
perror("fork"); exit(1);
perror("fork");
case 0:
do_child(data_pipe);
default:
do_parent(data_pipe);
}
}
void do_parent(int data_pipe[])
{
int c; /* data received from the user. */
int rc;
/* first, close the un-needed read-part of the pipe. */
close(data_pipe[0]);
while ((c = getchar()) > 0)
{
rc = write(data_pipe[1], &c, 1);
if (rc ==-1)
{
perror("Parent:write");close(data_pipe[1]);exit(1);
}
}
close(data_pipe[1]);
}
void do_child(int data_pipe[]) {
int c; /* data received from the parent. */
int rc;
/* first, close the un-needed write-part of the pipe. */
close(data_pipe[1]);
while ((rc = read(data_pipe[0], &c, 1)) > 0)
{
putchar(c);
}
}
SENDING "Hello World" through the pipe
#include stdio.h
#include unistd.h
#include sys/types.h
#include string.h
#include stdlib.h
int main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[] = "Hello, world!\n";
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
/* Child process closes up input side of pipe */
close(fd[0]);
/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)+1));
exit(0);
}
else
{
/* Parent process closes up output side of pipe */
close(fd[1]);
/* Read in a string from the pipe */
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
}
return(0);
}
if you want to know more about pipes click HERE
program for counting the number of words in the output when executing the command ls
#include stdio.h
#include stdlib.h
#include unistd.h
int main()
{
int pfds[2];
pipe(pfds);
if (!fork()) {
close(1); /* close normal stdout */
dup(pfds[1]); /* make stdout same as pfds[1] */
close(pfds[0]); /* we don't need this */
execlp("ls", "ls", NULL);
} else {
close(0); /* close normal stdin */
dup(pfds[0]); /* make stdin same as pfds[0] */
close(pfds[1]); /* we don't need this */
execlp("wc", "wc", "-l", NULL);
}
}
program implementing "ls|more" using pipes
#include
#include
main()
{
int fd[2];
pipe(fd);
switch(fork()){
case -1:perror("fork");
break;
case 0:close(1);
dup(fd[1]);
close(0);
execlp("ls","ls","-l",NULL);
break;
default:close(0);
dup(fd[0]);
execlp("more","more",NULL);
break;
}
}
SHARED MEMORY CLICK HERE
Subscribe to:
Posts (Atom)