Assembly, C Programs: Calculator, Palindrome, Search, FCFS, SJF, File Systems, Loader

Calculator

.model small printmsg macro msg push ax push bx push cx

push dx
LEA dx,msg
mov ah,09h
int 21h pop dx pop cx pop bx pop ax

endm .data msg1 db 0ah,0dh,”enter first number:$”

msg2 db 0ah,0dh,”enter second number:”

msg3 db 0ah,0dh,”1-addition,2-sub,3-mul,4-div: $”

msg4 db 0ah,0dh,”enter your option :$”

msg5 db 0ah,0dh,”your result is: $” msg6 db 0ah,0dh,”no such option $” num dw 0
cnt dw 0

choice dw 0 a dw 0
b dw 0

.code
mov ax,@data mov ds,ax printmsg msg1 call readnum mov ax,num mov a,ax printmsg msg2 call readnum mov bx,num mov b,bx printmsg msg3 call readnum mov ax,num mov choice,ax cmp choice,01 je addi
cmp choice,02 je subt
cmp choice,03 je multi
cmp choice,04 je divi printmsg msg6 call printnum jmp ex

addi:
printmsg msg5 mov ax,a

add ax,b mov num,ax call printnum jmp ex

subt:
printmsg msg5 mov ax,a
mov bx,b
sub ax,bx
mov num,ax call printnum jmp ex

multi:
printmsg msg5 mov ax,a
mov bx,b
mul bx


mov num,ax call printnum jmp ex

divi:
printmsg msg5 mov ax,a
mov bx,b
XOR dx,dx mov num,ax call printnum jmp ex

ex:
mov ah,4ch
int 21h
readnum proc near

push ax push bx push cx push dx mov num,00

r1:
mov ah,01h int 21h
cmp al,0dh je r2
mov cx,ax and cx,00ffh sub cx,30h mov bx,10 mov ax,num mul bx
add ax,cx mov num,ax jmp r1

r2:
pop dx pop cx pop bx pop ax

Ret readnum endp

printnum proc near push ax

push bx push cx push dx mov ax,num mov bx,10

p1:
mov dx,00 div bx push dx inc cnt cmp ax,00 jne p1

p2:
cmp cnt,00 je p3
pop dx
add dl,30h mov ah,02h int 21h
dec cnt
jmp p2

p3: pop dx pop cx pop bx pop ax ret

printnum endp end


Palindrome

.MODEL SMALL printmsg macro msg

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

endm
.STACK 100H
.DATA
STRING DB ‘noon’, ‘$’
STRING1 DB ‘String is palindrome’, ‘$’ STRING2 DB ‘String is not palindrome’, ‘$’ .CODE

MOV AX, @DATA MOV DS, AX LEA SI,STRING LOOP1 :

MOV AX, [SI] CMP AL, ‘$’ JE LABEL1 INC SI

JMP LOOP1
LABEL1 :
MOV DI,OFFSET STRING DEC SI
LOOP2 :
CMP SI, DI
JL OUTPUT1
MOV AX,[SI]
MOV BX, [DI]

CMP AL, BL
JNE OUTPUT2 DEC SI
INC DI
JMP LOOP2 OUTPUT1: printmsg STRING1 JMP EXIT OUTPUT2: printmsg STRING2 JMP EXIT
EXIT:
MOV AH, 4CH
INT 21H

end


Searching


.model small
printmsg macro msg
push ax
push bx
push cx
push dx
LEA dx,msg
mov ah,09h
int 21h
pop dx
pop cx
pop bx
pop ax
endm
.data
msg1 db 0ah,0dh,” Enter Number of
elements in the Array:$”
msg2 db 0ah,0dh,” Element FOUND
at position: $”
msg3 db 0ah,0dh,” Element NOT
FOUND!!$”
msg4 db 0ah,0dh,” Enter
Elements:$”
msg5 db 0ah,0dh,”$”
msg6 db 0ah,0dh,” Numbers are:$”
msg7 db 0ah,0dh,” Enter Number to
search:$”
num dw 0
cnt dw 0
count dw 0
input_
t_
array dw 15 dup(‘$’)
.code
mov ax,@data ; These two lines are
used to indicate the location of data
segment of this programĀ 
mov ds,ax
printmsg msg1
call readnum
mov cx,num
mov count,cx
push cx ; Getting count of numbers in
the array
mov si,00 ; Initializing index of array
as zero
printmsg msg4 ; Printing message in
msg1


x1: printmsg msg5 ; Printing newline
call readnum ; Reading number
using procedure and gets number in
variable num
mov ax,num ; Moving number to
register ax
mov input_
t_
array[si],ax ; Storing
number in array
add si,02 ; Points to next location
dec cx ; Reducing count
cmp cx,00 ; If not finished
jnz x1 ; If count not reached to zero
loop will repeat
printmsg msg6 ; Printing message in
msg2
mov si,00 ; Point to the beginning of
array
pop cx ; Take count value from top of
stack to the register cx again

x2:
printmsg msg5
mov ax,input_
t_
array[si] ; Set ax to 0
mov num,ax
call printnum
add si,02 ; Use procedure to find
sum
dec cx
cmp cx,00
jnz x2
printmsg msg7
call readnum
mov bx,num
call linear_search
ex: mov ah,4ch ; Exit to DOS prompt
using interrupt
int 21h
linear_search proc near
push ax
push bx
push cx
push dx
mov si,00
mov dx,01
mov cx,count


x3:
mov ax,input_array[si]
cmp ax,bx
je x4
inc dx
add si,02
dec cx
cmp cx,00
jne x3
printmsg msg3
jmp x5
x4:
printmsg msg2
mov num,dx
call printnum
x5:
pop dx
pop cx
pop bx
pop ax
ret
linear_search endp
; Procedure to read number
readnum proc near
push ax
push bx
push cx
push dx
mov num,00
r1: mov ah,01h
int 21h
cmp al,0dh
je r2
mov cx,ax
and cx,00ffh
sub cx,30h

mov bx,10
mov ax,num
mul bx
add ax,cx
mov num,ax
jmp r1
r2: pop dx
pop cx
pop bx
pop ax
ret


readnum endp
; Procedure to print number
printnum proc near
push ax
push bx
push cx
push dx
mov ax,num
mov bx,10
p1: mov dx,00
div bx
push dx
inc cnt
cmp ax,00
jne p1
p2: cmp cnt,00
je p3
pop dx
add dl,30h
mov ah,02h
int 21h
dec cnt
jmp p2
p3: pop dx
pop cx
pop bx
pop ax
ret
printnum endp
end


FCFS


#include
void main() { int
i=0,j=0,b[i],g[20],p[20],w[20],t[20],a[20],n
=0,m;
float avgw=0,avgt=0;
printf(“Enter the number of process : “);
scanf(“%d”,&n);
for(i=0;i
printf(“Process ID : “);
scanf(“%d”,&p[i]);
printf(“Burst Time : “);
scanf(“%d”,&b[i]);
printf(“Arrival Time: “);
scanf(“%d”,&a[i]); }
int temp=0;
for(i=0;i
for(j=0;j
if(a[j]>a[j+1]) {
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp; } } }
g[0]=0;
for(i=0;i
g[i+1]=g[i]+b[i];
for(i=0;i
{
t[i]=g[i+1]-a[i];
w[i]=t[i]-b[i];
avgw+=w[i];
avgt+=t[i]; }
avgw=avgw/n;
avgt=avgt/n;
printf(“pid\tarrivalT\tBrustT\tCompletionT
\tWaitingtime\tTurnaroundTi\n”);
for(i=0;i
printf(“%d\t%d\t%d\t%d\t\t%d\t\t\t%d\n”,
p[i],a[i],b[i],g[i+1],w[i],t[i]); }
printf(“\nAverage waiting time
%f”,avgw);


SJF


#include
void main()
{
int
i=0,j=0,p[i],b[i],g[20],w[20],t[20],a[20],n=
0,m;
int k=1,min=0,btime=0;
float avgw=0,avgt=0;
printf(“Enter the number of
process : “);
scanf(“%d”,&n);
for(i=0;i
{
printf(“\nProcess id : “);
scanf(“%d”,&p[i]);
printf(“Burst Time : “);
scanf(“%d”,&b[i]);
printf(“Arrival Time: “);
scanf(“%d”,&a[i]);
}
int temp=0;
for(i=0;i
for(j=0;j
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp; } } }
for(i=0;i
btime=btime+b[i];
min=b[k];
for(j=k;j
if(btime >= a[j] &&
b[j]


{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
temp=b[j];
b[j]=b[j-1];
b[j-1]=temp;
temp=p[j];
p[j]=p[j-1];
p[j-1]=temp; } }
k++; }
g[0]=a[0];
for(i=0;i
g[i+1]=g[i]+b[i];
if(g[i]
g[i]=a[i]; }

for(i=0;it[i]=g[i+1]-a[i];
w[i]=t[i]-b[i];
avgw+=w[i];
avgt+=t[i]; }
avgw=avgw/n;
avgt=avgt/n;
printf(“pid\tBrustTime\tGantChart\tWaitin
g time\t\tTurnarround Time\n”);
for(i=0;i{
printf(” %d\t
%d\t\t%d-%d\t\t%d\t\t\t%d\n”,p[i],b[i],g[i],
g[i+1],w[i],t[i]);
}
printf(“\nAverage waiting time
%f”,avgw);
printf(“\nAverage turnarround
time %f\n”,avgt);

}


Single Level File System


#include
#include
#define MAX 10
struct file{
char name[MAX];
}fileList[MAX];
int fileCount = 0;
int main(void){
int choice,i;
char name[MAX];
int isFound = 0;
while(1){
printf(“1. Create a file 2.Search File 3.
List all file 4. Exit \n”);
scanf(“%d”, &choice);
switch(choice){
case 1:
printf(“Enter file name: “);
scanf(“%s”, fileList[fileCount].name);
fileCount++;
break;
case 2:
printf(“Enter file name: “);
scanf(“%s”, name);
for(i = 0; i if(strcmp(name, fileList[i].name) == 0){
printf(“File found \n”);
isFound =1;
}
}
if(isFound==0)
printf(“File not found \n”);
break;
case 3:
for(i = 0; i printf(“%s \n”, fileList[i].name);
}
break;
case 4:
return 0;
default:
printf(“Invalid choice \n”);
}
}
}

Two Level File System


#include
#include
struct dirct{
char dir[20],file[20][10];
int findex; };
void main(){
int i,j,ch=1,dindex=0,found=0;
struct dirct d[10];
char ser[20];
for(i=0;id[i].findex=0;
do{
printf(“\n1directory\t2file\t3file\t\n5printf(“enter your choice “);
scanf(“%d”,&ch);
switch(ch){
case 1: printf(“\nenter the directory
name “);
scanf(“%s”,d[dindex].dir);
dindex++;
printf(“directory Created\n”);
break;
case 2: printf(“\nenter the directory
name “);
scanf(“%s”,ser);
found=0;
for(i=0;iif(!strcmp(ser,d[i].dir))
{
printf(“\nenter the fiename”);
scanf(“%s”,d[i].file[d[i].findex++]);
printf(“File created\n”);
break; }}
if(i==dindex){
printf(“search completed \n”);
printf(“no such file or directory\n”);}
break;
case 3:printf(“enter a file name :”);
scanf(“%s”,ser);
found=0;
for(i=0;ifor(j=0;jif(!strcmp(ser,d[i].file[j]))
{


printf(“%s is removed\n”,d[i].file[j]);
strcpy(d[i].file[j],d[i].file[d[i].findex-1]);
d[i].findex–;
found=1;
break;}}}
if(!found){
printf(“search completed \n”);
printf(“no such file or directory\n”);
}
break;
case 4:
printf(“enter a file name :”);
scanf(“%s”,ser);
found=0;
for(i=0;ifor(j=0;jif(!strcmp(ser,d[i].file[j])){
printf(“%s is removed\n”,d[i].file[j]);
found=1;
Break;}}}
if(!found){
printf(“search completed \n”);
printf(“no such file or directory\n”);
}
break;
case 5:
for(i=0;iprintf(“\nThe files in the directory %s are
:\n”,d[i].dir);
for(j=0;j

printf(“%s\n”,d[i].file[j]);
}
break;
}}
while(ch);
printf(“\n”);
}


Absolute Loader


#include
#include
int start, length, address;
char input[10];
void printLine (){
int i=0;
printf (“%d %c%c \n”,
address++, input[i++],
input[i++]);
printf (“%d %c%c \n”,
address++, input[i++],
input[i++]);
printf (“%d %c%c \n”,
address++, input[i++],
input[i++]);
}
void main (){
FILE *fp1 = fopen (“input.txt”,
“r”);
fscanf (fp1, “%s”, input);
while (strcmp (input, “E”) != 0){
if (strcmp (input, “H”) == 0){
fscanf (fp1, “%d”, &start);
fscanf (fp1, “%d”, &length);
fscanf (fp1, “%s”, input);
}
else if (strcmp (input, “T”) ==
0){
fscanf (fp1, “%d”, &address);
fscanf (fp1, “%s”, input);
printLine();
fscanf (fp1, “%s”, input);
}
else{
printLine();
fscanf (fp1, “%s”, input);
}
}
fclose (fp1);
printf (“FINISHED”);
}
Input.txt
H 1000 200
T 1000 112233 445566
T 2000 778899
E