Sei sulla pagina 1di 33

Course Code Course Title Assignment Number Maximum Marks Weightage Last Dates for Submission

: : : : : :

MCSL-017 C and Assembly Language Programming MCA(1)/L017/Assign/2011 100 25% 30th April, 2011 (For January Session) 31st October, 2011 (For July Session)

This assignment has two sections. Answer all questions in each section. Each Section is of 20 marks. Your Lab Records will carry 40 Marks. Rest 20 marks are for viva voce. You may use illustrations and diagrams to enhance the explanations. Please go through the guidelines regarding assignments given in the programme guide for the format of presentation.
Section 1: C Programming Lab Question 1: Write an interactive program in C language to manage the Clinic Information with menu options like Patients details, Doctors details, Doctors and Patients visits, Laboratory details, Bills, Payments etc. using the file handling concepts. The application should be designed userfriendly. (20 Marks) Note: You must execute the program and submit the program logic, sample input and output along with the necessary documentation for this question. Assumptions can be made wherever necessary. Ans: File: clinic.c /* Clinic Information system */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<conio.h> #include"doctor.h" #include"patient.h" #include"visit.h" #include"lab.h" #include"bills.h" #include"payment.h" #include"inventory.h" /* Function Declaration */ int patient();

int doctor(); int visits(); int pathology(); int bills(); int payments(); int stocks(); /* Main Function*/ void main() { int ch,handle; /* Main Menu */ do{ printf("\t\t\tClinic Management System"); printf("\n\t\t\t\tMain Menu\n---------------------------------------------------------\n"); printf("1. Patient Registration\n"); printf("2. Doctor Details\n"); printf("3. Patient & Doctor Visits\n"); printf("4. Laboratory\n"); printf("5. Billing\n"); printf("6. Payments\n"); printf("7. Inventory\n"); printf("0. Exit\n"); printf("Enter your Choice :"); scanf("%d",&ch); switch(ch) { case 1: { /* Patient Registration */ handle=patient(); break; } case 2: { /* Doctor's Details */ handle=doctor(); break; } case 3: { /* Visits */ handle=visits(); break; } case 4: {

/* Laboratory */ handle=pathology(); break; } case 5: { /* Bills */ handle=bills(); break; } case 6: { /* Payments */ handle=payment(); break; } case 7: { /* Inventory */ handle=stocks(); break; } default: { break; } } }while(ch!=0); } Include File: doctor.h /***************** Doctor Information Processing Module *******************/ /***************** Structure Declaration for holding doctor Data *******************/

struct doctor { char name[64],address[64],dept[64],sex[2],mobile[11]; int age; float cr,dr; doctor()/*Constructor*/ { strcpy(name," ");strcpy(address," ");strcpy(dept," ");strcpy(sex," ");strcpy(mobile," "); age=0;cr=0.0;dr=0.0;

} }; /***************** Function to accept doctor data *******************/ int doctor() { int ch; struct doctor p; FILE *out,*in; // char c; /* Main Menu */ do{ printf("\nDoctor Menu\n-----------\n"); printf("1. Add New Doctor\n"); printf("2. Show Doctor\n"); printf("0. Main Menu\n"); scanf("%d",&ch); switch(ch) { case 1: { printf("Adding New Patient ...\n"); printf("Name :"); scanf("%s",&p.name); printf("Address :"); scanf("%s",&p.address); printf("Dept :"); scanf("%s",&p.dept); printf("sex m/f:"); scanf("%s",&p.sex); printf("Mobile :"); scanf("%s",&p.mobile); printf("Account cr :"); scanf("%d",&p.cr); printf("Account dr :"); scanf("%d",&p.dr); printf("Press 0 to exit :"); scanf("%d",&ch); /* Write to disk*/ out=fopen("doctor.txt","a+"); if(out==NULL) { puts("\ncannot open file to write"); exit(1); }

fflush(stdin); /*Saving started*/ fprintf(out,"%s\t%s\t%s\t%s\t%s\t%f\t%f\n",p.name,p.address,p.dept,p.sex,p.mobile,p. age,p.cr,p.dr); fclose(out); /* Data Saved to disk */ break; } case 2: { printf("\n Showing Doctor File\n"); in=fopen("doctor.txt","r"); if(in==NULL) { puts("\ncannot open file"); exit(1); } do{ fscanf(in,"%s%s%s%s%s%f%f",&p.name,&p.address,&p.dept,&p.sex,&p.mobile,&p.cr,& p.dr); fprintf(stdout,"Name\t:%s\nAddress\t:%s\nDept\t:%s\nSex\t:%s\nMobile\t:%s\nAge\t: %d\nCR\t:%f\nDR\t:%f\n\n",p.name,p.address,p.dept,p.sex,p.mobile,p.cr,p.dr); }while(!feof(in)); break; } case 0: { break; } default: { break; } } }while(ch!=0); return 1; } Include File: patient.h

/***************** Patient Information Processing Module *******************/ /***************** Structure Declaration for holding Patient Data *******************/ struct patient { char name[64],address[64],ref[64],sex[2],mobile[11],reg_dt[16]; int age; float cr,dr; patient()/*Condtructor*/ { strcpy(name," ");strcpy(address," ");strcpy(ref," ");strcpy(sex," ");strcpy(mobile," ");strcpy(reg_dt," "); age=0;cr=0.0;dr=0.0; } }; /***************** Function to accept patient registration data int patient() { int ch; struct patient p; FILE *out,*in; // char c; /* Main Menu */ do{ printf("\nPatient Menu\n-----------\n"); printf("1. Add New Patient\n"); printf("2. Show Patient\n"); printf("0. Main Menu\n"); scanf("%d",&ch); switch(ch) { case 1: { printf("Adding New Patient ...\n"); printf("Name :"); scanf("%s",&p.name); printf("Address :"); scanf("%s",&p.address); printf("Ref. By :"); scanf("%s",&p.ref); printf("sex m/f:"); scanf("%s",&p.sex); printf("Mobile :"); scanf("%s",&p.mobile); *******************/

printf("Reg date :"); scanf("%s",&p.reg_dt); printf("Age :"); scanf("%d",&p.age); printf("Account cr :"); scanf("%d",&p.cr); printf("Account dr :"); scanf("%d",&p.dr); printf("Press 0 to exit :"); scanf("%d",&ch); /* Write to disk*/ out=fopen("patient.txt","a+"); if(out==NULL) { puts("\ncannot open file to write"); exit(1); } fflush(stdin); /*Saving started*/ fprintf(out,"%s\t%s\t%s\t%s\t%s\t%s\t%d\t%f\t%f\n",p.name,p.address,p.ref,p.sex,p.m obile,p.reg_dt,p.age,p.cr,p.dr); fclose(out); /* Data Saved to disk */ break; } case 2: { printf("\n Showing Patient File\n"); in=fopen("patient.txt","r"); if(in==NULL) { puts("\ncannot open file"); exit(1); } do{ fscanf(in,"%s%s%s%s%s%s%d%f%f",&p.name,&p.address,&p.ref,&p.sex,&p.mobile,&p.r eg_dt,&p.age,&p.cr,&p.dr); fprintf(stdout,"Name\t:%s\nAddress\t:%s\nRef\t:%s\nSex\t:%s\nMobile\t:%s\nReg Dt\t:%s\nAge\t:%d\nCR\t:%f\nDR\t:%f\n\n",p.name,p.address,p.ref,p.sex,p.mobile,p.reg_dt,p.a ge,p.cr,p.dr); }while(!feof(in));

break; } case 0: { break; } default: { break; } } }while(ch!=0); return 1; } Include File: visit.h /***************** Visit Information Processing Module *******************/ *******************/

/***************** Structure Declaration for holding Visit Data

struct visit { char name[64],pid[64],dept[64],dt[16],nv[16],symptom[64],med1[64],med2[64],med3[64],med4[64]; int fee; visit() /*Constructor*/ { strcpy(name," ");strcpy(pid," ");strcpy(dept," ");strcpy(dt," ");strcpy(nv," "); strcpy(symptom," ");strcpy(med1," ");strcpy(med2," ");strcpy(med3," ");strcpy(med4," "); fee=0; } }; /***************** Function to accept visit data int visits() { int ch; struct visit p; FILE *out,*in; // char c; /* Main Menu */ *******************/

do{ printf("\nVisit Menu\n-----------\n"); printf("1. Add New Visit\n"); printf("2. Show Visit\n"); printf("0. Main Menu\n"); scanf("%d",&ch); switch(ch) { case 1: { printf("Adding New Visit ...\n"); printf("Name :"); scanf("%s",&p.name); printf("Patient ID :"); scanf("%s",&p.pid); printf("Dept :"); scanf("%s",&p.dept); printf("Date:"); scanf("%s",&p.dt); printf("Symptom :"); scanf("%s",&p.symptom); printf("Medicine 1 :"); scanf("%s",&p.med1); printf("Medicine 1 :"); scanf("%s",&p.med2); printf("Medicine 3 :"); scanf("%s",&p.med3); printf("Medicine 4 :"); scanf("%s",&p.med4); printf("Fees :"); scanf("%d",&p.fee); printf("Next Visit :"); scanf("%s",&p.nv); printf("Press 0 to exit :"); scanf("%d",&ch); /* Write to disk*/ out=fopen("visit.txt","a+"); if(out==NULL) { puts("\ncannot open file to write"); exit(1); } fflush(stdin); /*Saving started*/

fprintf(out,"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\n",p.name,p.pid,p.dept,p.s ymptom,p.med1,p.med2,p.med3,p.med4,p.fee,p.nv,p.dt); fclose(out); /* Data Saved to disk */ break; } case 2: { printf("\n Showing Visits File\n"); in=fopen("visit.txt","r"); if(in==NULL) { puts("\ncannot open file"); exit(1); } do{ fscanf(in,"%s%s%s%s%s%s%s%s%d%s%s",&p.name,&p.pid,&p.dept,&p.symptom,&p.me d1,&p.med2,&p.med3,&p.med4,&p.fee,&p.nv,&p.dt); fprintf(stdout,"Name\t:%s\nPatient ID\t:%s\nDept\t:%s\nSymptoms\t:%s\nMed1\t:%s\nMed2\t:%sMed3\t:%s\nMed4\t:%s\nFees\ t:%d\nNext Visit\t:%s\nDate\t%s\n",p.name,p.pid,p.dept,p.symptom,p.med1,p.med2,p.med3,p.med4,p.fee, p.nv,p.dt); }while(!feof(in)); break; } case 0: { break; } default: { break; } } }while(ch!=0); return 1; } Include File: payment.h

10

/***************** Payment Processing Module

*******************/

/***************** Structure Declaration for holding payment Data *******************/ struct pay { char name[64],address[64],dept[64],mobile[11],date[16],log[64]; int cr,dr; pay()/*Constructor*/ { strcpy(name," ");strcpy(address," ");strcpy(dept," ");strcpy(mobile," ");strcpy(date," "); strcpy(log," "); cr=0;dr=0; } }; /***************** Function to accept patient registration data int payment() { int ch; struct pay p; FILE *out,*in; /* Main Menu */ do{ printf("\nPayment Menu\n-----------\n"); printf("1. Create Transaction\n"); printf("2. Show Transactions\n"); printf("0. Main Menu\n"); scanf("%d",&ch); switch(ch) { case 1: { printf("Adding New Transaction ...\n"); printf("Name :"); scanf("%s",&p.name); printf("Address :"); scanf("%s",&p.address); printf("Dept :"); scanf("%s",&p.dept); printf("Mobile :"); scanf("%s",&p.mobile); printf("Date :"); scanf("%s",&p.date); *******************/

11

printf("Comments :"); scanf("%s",&p.log); printf("Account cr :"); scanf("%d",&p.cr); printf("Account dr :"); scanf("%d",&p.dr); printf("Press 0 to exit :"); scanf("%d",&ch); /* Write to disk*/ out=fopen("payment.txt","a+"); if(out==NULL) { puts("\ncannot open file to write"); exit(1); } fflush(stdin); /*Saving started*/ fprintf(out,"%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\n",p.name,p.address,p.dept,p.mobile,p. date,p.log,p.cr,p.dr); fclose(out); /* Data Saved to disk */ break; } case 2: { printf("\n Showing Doctor File\n"); in=fopen("payment.txt","r"); if(in==NULL) { puts("\ncannot open file"); exit(1); } do{ fscanf(in,"%s%s%s%s%s%s%f%f",&p.name,&p.address,&p.dept,&p.mobile,&p.date,&p.l og,&p.cr,&p.dr); fprintf(stdout,"Name\t:%s\nAddress\t:%s\nDept\t:%s\nMobile\t:%s\nDate\t:%s\nCom ments\t:%s\nCR\t:%f\nDR\t:%f\n\n",p.name,p.address,p.dept,p.mobile,p.date,p.log,p.cr,p.dr); }while(!feof(in)); break; } case 0:

12

{ break; } default: { break; } } }while(ch!=0); return 1; } Include File: lab.h /***************** lab Information Processing Module *******************/ *******************/

/***************** Structure Declaration for holding lab Data

struct lab { char name[64],pid[64],test[64],date[16],ref[64]; int cost; lab() /* Constructor */ { strcpy(name," ");strcpy(pid," ");strcpy(test," ");strcpy(date," "),strcpy(ref," ");cost=0; } }; /***************** Function to accept lab data *******************/

int pathology() { int ch; struct lab p; FILE *out,*in; // char c; /* Main Menu */ do{ printf("\nPathology Menu\n-----------\n"); printf("1. Add New Patient\n"); printf("2. Show Bill\n"); printf("0. Main Menu\n"); scanf("%d",&ch); switch(ch) {

13

case 1: { printf("Adding New Patient ...\n"); printf("Name :"); scanf("%s",&p.name); printf("Patient ID :"); scanf("%s",&p.pid); printf("Test :"); scanf("%s",&p.test); printf("Date:"); scanf("%s",&p.date); printf("Ref. By :"); scanf("%s",&p.ref); printf("Cost :"); scanf("%d",&p.cost); printf("Press 0 to exit :"); scanf("%d",&ch); /* Write to disk*/ out=fopen("lab.txt","a+"); if(out==NULL) { puts("\ncannot open file to write"); exit(1); } fflush(stdin); /*Saving started*/ fprintf(out,"%s\t%s\t%s\t%s\t%s\t%d\n",p.name,p.pid,p.test,p.date,p.ref,p.cost); fclose(out); /* Data Saved to disk */ break; } case 2: { printf("\n Showing Pathology Bills \n"); in=fopen("visit.txt","r"); if(in==NULL) { puts("\ncannot open file"); exit(1); } do{ fscanf(in,"%s%s%s%s%s%d",&p.name,&p.pid,&p.test,&p.date,&p.ref,&p.cost);

14

fprintf(stdout,"Name\t:%s\nPatient ID\t:%s\nTest\t:%s\nDate\t:%s\nRef By\t:%s\nCost\t:%s\n",p.name,p.pid,p.test,p.date,p.ref,p.cost); }while(!feof(in)); break; } case 0: { break; } default: { break; } } }while(ch!=0); return 1; } Include File: inventory.h /***************** Inventory Information Processing Module *******************/

/***************** Structure Declaration for holding Inventory Data *******************/ struct inventory { char name[64],mfg[16],exp[16],supplier[64],mobile[11]; int qty,mrp; inventory()/*Constructor*/ { strcpy(name," ");strcpy(mfg," ");strcpy(exp," ");strcpy(supplier," ");strcpy(mobile," "); qty=0,mrp=0; } }; /***************** Function to accept Inventory data int stocks() { int ch; struct inventory s; *******************/

15

//

FILE *out,*in; char c; /* Main Menu */ do{ printf("\nInventory Menu\n-----------\n"); printf("1. Add New Stock\n"); printf("2. Show Stocks\n"); printf("0. Main Menu\n"); scanf("%d",&ch); switch(ch) { case 1: { printf("Adding New Stock ...\n"); printf("Name :"); scanf("%s",&s.name); printf("Mfg :"); scanf("%s",&s.mfg); printf("Exp :"); scanf("%s",&s.exp); printf("Supplier :"); scanf("%s",&s.supplier); printf("Mobile :"); scanf("%s",&s.mobile); printf("Qty :"); scanf("%d",&s.qty); printf("Mrp :"); scanf("%d",&s.mrp); printf("Press 0 to exit :"); scanf("%d",&ch); /* Write to disk*/ out=fopen("stocks.txt","a+"); if(out==NULL) { puts("\ncannot open file to write"); exit(1); } fflush(stdin); /*Saving started*/ fprintf(out,"%s\t%s\t%s\t%s\t%s\t%d\t%d\n",s.name,s.mfg,s.exp,s.supplier,s.mobile,s.q ty,s.mrp); fclose(out); /* Data Saved to disk */ break;

16

} case 2: { printf("\n Showing Stocks\n"); in=fopen("stocks.txt","r"); if(in==NULL) { puts("\ncannot open file"); exit(1); } do{ fscanf(in,"%s%s%s%s%s%d%d",&s.name,&s.mfg,&s.exp,&s.supplier,&s.mobile,&s.qty,& s.mrp); fprintf(stdout,"Name\t:%s\nMFG\t:%s\nEXP\t:%s\nSupplier\t:%s\nMobile\t:%s\nQty\t: %d\nMRP\t:%d\n",s.name,s.mfg,s.exp,s.supplier,s.mobile,s.qty,s.mrp); }while(!feof(in)); break; } default: { break; } } }while(ch!=0); return 1; } Include File: bill.h /* Bill Generation Function */ /***************** Bill Processing Module *******************/ void line() { int i; for(i=0;i<80;i++) { printf("-"); } printf("\n"); }

17

int bills() { int ch,i=0,j=0,items=0,count=0,cost[16],total=0; char pid[16],*test[10]; struct lab p[10]; struct visit v[10]; struct patient bp1[10]; FILE *clf,*labf; /* Main Menu */ do{ printf("\nBill Menu\n-----------\n"); printf("1. Get Clinic Bill\n"); printf("2. Get Pathology Bill\n"); printf("0. Main Menu\n"); scanf("%d",&ch); switch(ch) { case 1: { total=0; printf("Enter Patient ID:"); fscanf(stdin,"%s",&pid); line();/*Print Line*/ printf("\t\t\t\tBill\n"); line(); /* Extract patient data */ clf=fopen("visit.txt","r"); if(labf==NULL) { puts("\ncannot open file"); exit(1); } i=0; do{ fscanf(clf,"%s%s%s%s%s%s%s%s%d%s%s",&v[i].name,&v[i].pid,&v[i].dept,&v[i].sympto m,&v[i].med1,&v[i].med2,&v[i].med3,&v[i].med4,&v[i].fee,&v[i].nv,&v[i].dt); i++; count++; }while(!feof(clf)); items=0; for(i=0;i<count;i++) { if(!(strcmp(v[i].pid,pid))) {

18

fprintf(stdout,"Name\t: %s\t\t\t\t\tPat.Id : %s\nDept\t: %s\t\t\t\t\t\tDate : %s\n",v[i].name,v[i].pid,v[i].dept,v[i].dt); cost[i]=v[i].fee; items++; } } line(); printf("Sn\t|\t\tParticulars\t\t\t\t|\tAmount\n"); line(); for(i=0;i<items;i++) { j=i+1; printf("%d\t|Fees Charged \t\t\t\t\t\t|\t%d\n",j,cost[i]); total+=cost[i]; } line(); printf("\t\t\t\t\t\t\t\tTotal=%d\n",total); line(); break; } case 2: { total=0; printf("Enter Patient ID:"); fscanf(stdin,"%s",&pid); line();/*Print Line*/ printf("\t\t\t\tBill\n"); line(); /* Extract patient data */ labf=fopen("lab.txt","r"); if(labf==NULL) { puts("\ncannot open file"); exit(1); } i=0; do{ fscanf(labf,"%s%s%s%s%s%d",&p[i].name,&p[i].pid,&p[i].test,&p[i].date,&p[i].ref,&p[i].c ost); i++; count++; }while(!feof(labf)); items=0; for(i=0;i<count;i++)

19

{ if(!(strcmp(p[i].pid,pid))) { fprintf(stdout,"Name\t: %s\t\t\t\t\t\tPat.Id : %s\nRef By\t: %s\t\t\t\t\tDate : %s\n",p[i].name,p[i].pid,p[i].ref,p[i].date); test[i]=p[i].test; cost[i]=p[i].cost; items++; } } line(); printf("Sn\t|\t\tParticulars\t\t\t|\tAmount\n"); line(); for(i=0;i<items;i++) {j=i+1; printf("%d\t|%s\t\t\t\t\t\t|\t%d\n",j,test[i],cost[i]); total+=cost[i]; } line(); printf("\t\t\t\t\t\t\t\tTotal=%d\n",total); line(); break; } default: { break; } } }while(ch!=0); return 1; } /*********************** End of program *************************/

20

Section 2: Assembly Language Programming Lab Question 1: (a) Ans: .MODEL SMALL .STACK 64 DATA SEGMENT KEY DB 04H NUM DB 05H ARRAY DB 05H DB 04H DB 03H DB 02H DB 01H MID MSG1 MSG2 MSG3 MSG4 DATA DB 00H DB 'PRESENT$' DB 'ABSENT$' DB 'SORTED LIST > $' DB 'KEY > $' ENDS Write a program in assembly language to sort an array of signed integers and search for the presence of an item in the sorted array using linear search (5 Marks)

CODE SEGMENT ASSUME CS:CODE,DS:DATA START: CALL CLS MOV DX,0000H CALL CURSOR MOV AX,DATA MOV DS,AX ;Bubble Sort MOV DL,NUM ;counter1 LIN1: DEC DL JZ LOUT1 MOV CL,DL ;counter2 LEA BX,ARRAY ;pointer to first number LIN2: MOV AL,[BX] CMP AL,[BX+1] JS LESS ;ARRAY(I)<ARRAY(I+1) XCHG AL,[BX+1]; MOV [BX],AL LESS: INC BX DEC CL JNZ LIN2 JMP LIN1 ;Display

21

LOUT1: LEA DX,MSG3 MOV AH,09H INT 21H LEA BX,ARRAY MOV CH,00H MOV CL,NUM DISP1: MOV AL,[BX] MOV AH,AL PUSH BX PUSH CX CALL SHOW POP CX POP BX INC BX LOOP DISP1 MOV DX,0200H CALL CURSOR LEA DX,MSG4 MOV AH,09H INT 21H MOV AL,KEY MOV AH,AL CALL SHOW ;Binary Search MOV CX,01H ;lower MOV DH,00H MOV DL,NUM ;upper LOOP1: CMP DX,CX JC NFOUND MOV AX,CX ADD AX,DX SHR AX,1 MOV MID,AL LEA BX,ARRAY ADD BX,AX DEC BX MOV AL,[BX] CMP AL,KEY JE FOUND JC LHALF UHALF: MOV DH,00H MOV DL,MID DEC DX JMP LOOP1 LHALF: MOV CH,00H

22

MOV CL,MID INC CX JMP LOOP1 FOUND: LEA DX,MSG1 MOV AH,09H INT 21H JMP HALT NFOUND: LEA DX,MSG2 MOV AH,09H INT 21H HALT: MOV AX,4C00H INT 21H ;CLEARS SCREEN CLS: MOV AX,0600H MOV BH,07H MOV CX,0000H MOV DX,184FH INT 10H RET ;SET CURSOR CURSOR: MOV AH,02H MOV BH,00H INT 10H RET SHOW: MOV BX,AX AND BX,0F00FH MOV CL,04H SHR BH,CL ADD BX,3030H MOV AH,02H MOV DL,BH INT 21H MOV AH,02H MOV DL,BL INT 21H MOV AH,02H MOV DL,' ' INT 21H RET CODE ENDS END START

23

(b)

Develop and execute an assembly language program to find the HCF of two unsigned 16-bit numbers. (5 Marks)

Ans: .MODEL SMALL .STACK 100H .DATA PROMPT_1 DB 'Enter the value of M = $' PROMPT_2 DB 0DH,0AH,'Enter the value of N = $' PROMPT_3 DB 0DH,0AH,'The GCD of M and N is = $' .CODE MAIN PROC MOV AX, @DATA MOV DS, AX LEA DX, PROMPT_1 MOV AH, 9 INT 21H CALL INDEC PUSH AX LEA DX, PROMPT_2 MOV AH, 9 INT 21H CALL INDEC MOV BX, AX POP AX @REPEAT: XOR DX, DX DIV BX CMP DX, 0 JE @END_LOOP MOV AX, BX MOV BX, DX JMP @LOOP @END_LOOP:

; initialize DS ; load and display the string PROMPT_1

; call the procedure INDEC ; push AX onto the STACK ; load and display the string PROMPT_2

; call the procedure INDEC ; set BX=AX ; pop a value from STACK into AX ; jump label ; clear DX ; set AX=DX:AX\BX , AX=DX:AX%BX ; compare DX with 0 ; jump to label @END_LOOP if CX=0 ; set AX=BX ; set BX=DX ; jump to label @REPEAT ; jump label

24

LEA DX, PROMPT_3 MOV AH, 9 INT 21H MOV AX, BX CALL OUTDEC MOV AH, 4CH INT 21H MAIN ENDP

; load and display the string PROMPT_3

; set AX=BX ; call the procedure OUTDEC ; return control to DOS

;**************************************************************************; ;**************************************************************************; ;------------------------- Procedure Definitions ------------------------; ;**************************************************************************; ;**************************************************************************; ;**************************************************************************; ;------------------------------- INDEC ----------------------------------; ;**************************************************************************; INDEC PROC ; this procedure will read a number indecimal form ; input : none ; output : store binary number in AX ; uses : MAIN PUSH BX PUSH CX PUSH DX JMP @READ ; push BX onto the STACK ; push CX onto the STACK ; push DX onto the STACK ; jump to label @READ

@SKIP_BACKSPACE: ; jump label MOV AH, 2 ; set output function MOV DL, 20H ; set DL=' ' INT 21H ; print a character @READ: XOR BX, BX XOR CX, CX XOR DX, DX MOV AH, 1 INT 21H CMP AL, "-" ; jump label ; clear BX ; clear CX ; clear DX ; set input function ; read a character ; compare AL with "-"

25

JE @MINUS CMP AL, "+" JE @PLUS JMP @SKIP_INPUT @MINUS: MOV CH, 1 INC CL JMP @INPUT @PLUS: MOV CH, 2 INC CL @INPUT: MOV AH, 1 INT 21H @SKIP_INPUT: CMP AL, 0DH JE @END_INPUT

; jump to label @MINUS if AL="-" ; compare AL with "+" ; jump to label @PLUS if AL="+" ; jump to label @SKIP_INPUT ; jump label ; set CH=1 ; set CL=CL+1 ; jump to label @INPUT ; jump label ; set CH=2 ; set CL=CL+1 ; jump label ; set input function ; read a character ; jump label ; compare AL with CR ; jump to label @END_INPUT

CMP AL, 8H ; compare AL with 8H JNE @NOT_BACKSPACE ; jump to label @NOT_BACKSPACE if AL!=8 CMP CH, 0 ; compare CH with 0 JNE @CHECK_REMOVE_MINUS ; jump to label @CHECK_REMOVE_MINUS if CH!=0 CMP CL, 0 ; compare CL with 0 JE @SKIP_BACKSPACE ; jump to label @SKIP_BACKSPACE if CL=0 JMP @MOVE_BACK ; jump to label @MOVE_BACK @CHECK_REMOVE_MINUS: ; jump label

CMP CH, 1 ; compare CH with 1 JNE @CHECK_REMOVE_PLUS ; jump to label @CHECK_REMOVE_PLUS if CH!=1 CMP CL, 1 ; compare CL with 1 JE @REMOVE_PLUS_MINUS ; jump to label @REMOVE_PLUS_MINUS if CL=1 @CHECK_REMOVE_PLUS: ; jump label

CMP CL, 1 ; compare CL with 1 JE @REMOVE_PLUS_MINUS ; jump to label @REMOVE_PLUS_MINUS if CL=1 JMP @MOVE_BACK ; jump to label @MOVE_BACK

26

@REMOVE_PLUS_MINUS: ; jump label MOV AH, 2 ; set output function MOV DL, 20H ; set DL=' ' INT 21H ; print a character MOV DL, 8H INT 21H JMP @READ @MOVE_BACK: MOV AX, BX MOV BX, 10 DIV BX MOV BX, AX MOV AH, 2 MOV DL, 20H INT 21H MOV DL, 8H INT 21H XOR DX, DX DEC CL JMP @INPUT @NOT_BACKSPACE: INC CL CMP AL, 30H JL @ERROR CMP AL, 39H JG @ERROR AND AX, 000FH PUSH AX MOV AX, 10 MUL BX MOV BX, AX ; set DL=8H ; print a character ; jump to label @READ ; jump label ; set AX=BX ; set BX=10 ; set AX=AX/BX ; set BX=AX ; set output function ; set DL=' ' ; print a character ; set DL=8H ; print a character ; clear DX ; set CL=CL-1 ; jump to label @INPUT ; jump label

; set CL=CL+1 ; compare AL with 0 ; jump to label @ERROR if AL<0 ; compare AL with 9 ; jump to label @ERROR if AL>9 ; convert ascii to decimal code ; push AX onto the STACK ; set AX=10 ; set AX=AX*BX ; set BX=AX

27

POP AX ADD BX, AX JS @ERROR JMP @INPUT @ERROR: MOV AH, 2 MOV DL, 7H INT 21H XOR CH, CH @CLEAR: MOV DL, 8H INT 21H MOV DL, 20H INT 21H MOV DL, 8H INT 21H LOOP @CLEAR JMP @READ @END_INPUT: CMP CH, 1 JNE @EXIT NEG BX @EXIT: MOV AX, BX POP DX POP CX POP BX RET INDEC ENDP

; pop a value from STACK into AX ; set BX=AX+BX ; jump to label @ERROR if SF=1 ; jump to label @INPUT ; jump label ; set output function ; set DL=7H ; print a character ; clear CH ; jump label ; set DL=8H ; print a character ; set DL=' ' ; print a character ; set DL=8H ; print a character ; jump to label @CLEAR if CX!=0 ; jump to label @READ ; jump label ; compare CH with 1 ; jump to label @EXIT if CH!=1 ; negate BX ; jump label ; set AX=BX ; pop a value from STACK into DX ; pop a value from STACK into CX ; pop a value from STACK into BX ; return control to the calling procedure

;**************************************************************************; ;-------------------------------- OUTDEC --------------------------------; ;**************************************************************************;

28

OUTDEC PROC ; this procedure will display a decimal number ; input : AX ; output : none ; uses : MAIN PUSH BX PUSH CX PUSH DX CMP AX, 0 JGE @START PUSH AX MOV AH, 2 MOV DL, "-" INT 21H POP AX NEG AX @START: XOR CX, CX MOV BX, 10 @OUTPUT: XOR DX, DX DIV BX PUSH DX INC CX OR AX, AX JNE @OUTPUT MOV AH, 2 @DISPLAY: POP DX OR DL, 30H INT 21H LOOP @DISPLAY POP DX POP CX POP BX ; push BX onto the STACK ; push CX onto the STACK ; push DX onto the STACK ; compare AX with 0 ; jump to label @START if AX>=0 ; push AX onto the STACK ; set output function ; set DL='-' ; print the character ; pop a value from STACK into AX ; take 2's complement of AX ; jump label ; clear CX ; set BX=10 ; loop label ; clear DX ; divide AX by BX ; push DX onto the STACK ; increment CX ; take OR of Ax with AX ; jump to label @OUTPUT if ZF=0 ; set output function ; loop label ; pop a value from STACK to DX ; convert decimal to ascii code ; print a character ; jump to label @DISPLAY if CX!=0 ; pop a value from STACK into DX ; pop a value from STACK into CX ; pop a value from STACK into BX

29

RET OUTDEC ENDP

; return control to the calling procedure

;**************************************************************************; ;--------------------------------------------------------------------------; ;**************************************************************************; END MAIN ;**************************************************************************; ;**************************************************************************; ;------------------------------ THE END ---------------------------------; ;**************************************************************************; ;**************************************************************************;

30

(c ) Ans:

Write a program in assembly language for finding the largest number in an array of 10 elements. (5 Marks) CODE SEGMENT MOV AX,DATA MOV DS,AX ; initialize DS MOV DI, OFFSET ARRAY ; DI points to the array MOV AX, [DI] ; AX contains the first element MOV LARGEST, AX ; initialize largest MOV SMALLEST, AX ; initialize smallest MOV CX,10 ; loop counter A1: MOV AX, [DI] ; get array value CMP AX, SMALLEST ; [DI] = smallest? JGE A2 ; yes : skip MOV SMALLEST, AX ; no : move [DI] to smallest JMP A3 ; as it is smallest, thus no need ; to compare it with the largest . i A2: CMP AX,LARGEST ; [DI] = largest JLE A3 ; yes : skip MOV LARGEST, AX ; no : mov [Dl] to largest

A3: ADD DI,2 ; point to next number LOOP A1 ; repeat the loop until CX = 0 MOV AX, 4C00h INT 21h ; halt, return to DOS CODE ENDS DATA SEGMENT ARRAY DW -1, 2000, -4000, 32767, 500, 0 LARGEST DW ? SMALLEST DW ? DATA ENDS END

31

(d)

Given a string of characters terminated by 00H, write a assembly language program to determine if it is a palindrome. If 'Yes' output the message " The given string is a palindrome. If 'No' output the message "No, it is not a palindrome" (5 Marks) .MODEL SMALL .STACK 64 DATA SEGMENT MSG DB 'ENTER STRING TO BE CHECKED FOR PALINDROME > $' MSG1 DB 'YES ,A PALINDROME $' MSG2 DB 'NOT A PALINDROME $' STRING LABEL BYTE MAXLEN DB 20 STRLEN DB ? STRFLD DB 20 DUP(' ') DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA START: MOV AX,DATA MOV DS,AX CALL CLS ;clear screen CALL CURSOR ;set cursor at (0,0) CALL READ CALL CLS CALL CURSOR LEA BX,STRFLD MOV CH,00H MOV CL,STRLEN CMP CX,02H JC PAL MOV DX,BX ADD DX,CX DEC DX SHR CX,1 AGAIN: MOV AL,[BX] XCHG BX,DX CMP AL,[BX] JNE NOTPAL XCHG BX,DX INC BX DEC DX

Ans:

32

LOOP AGAIN PAL: MOV AH,09H LEA DX,MSG1 INT 21H JMP HALT NOTPAL: MOV AH,09H LEA DX,MSG2 INT 21H HALT: MOV AX,4C00H ;terminate program INT 21H ;CLEARS SCREEN CLS: MOV AX,0600H MOV BH,07H MOV CX,0000H MOV DX,184FH INT 10H RET ;SET CURSOR CURSOR: MOV DX,0000H MOV AH,02H MOV BH,00H INT 10H RET READ: MOV AH,09H LEA DX,MSG INT 21H MOV AH,0AH LEA DX,STRING INT 21H RET WRITE: MOV AH,09H LEA DX,STRFLD INT 21H RET CODE ENDS END START ---------------------------;display message

33

Potrebbero piacerti anche