Search This Blog

C Program to Display and Set System Time and Date Using 8086 Interrupts

This is a C Program to Display and Set System Time and Date Using 8086 Interrupts. Interrupt number 21 (in hexadecimal) is used to:
C program to get system time and date and also to set it using 8086 interrupt INT 21 interrupt 0x21 21h int86() function call interrupts in C program
Output of c Program to get and set system time and date 

  • Get System time
  • Set System time
  • Get System Date
  • Set System Date
To Get system date:
- Call interrupt 0x21 (hexadecimal) 
- AH (higher byte of accumulator) should be made equal to 2A (hex) before calling interrupt
Output:



  • day in DL
  • Month in DH
  • Year in CX

To set System Date:

- Call interrupt 0x21 (hexadecimal) 
- AH (higher byte of accumulator) should be made equal to 2B (hex) before calling interrupt
- Before Calling interrupt, store:
  • Year in CX
  • Month in DH
  • Day in DL
To Get system time:
- Call interrupt 0x21 (hexadecimal) 
- AH (higher byte of accumulator) should be made equal to 2C (hex) before calling interrupt
Output:
  • Hours in CH
  • Minutes in CL
  • Seconds in DH
  • 1/100 th of a second in DL







To set System Time:







- Call interrupt 0x21 (hexadecimal) 


- AH (higher byte of accumulator) should be made equal to 2D (hex) before calling interrupt

- Before Calling interrupt, store:


  • Hour in CH 

  • Minutes in CL

  • Seconds in DH 

  • 1/100 th of second in DL




#include<stdio.h>
#include<dos.h>
#include<conio.h>

void main()
{
int day,month,year,tflag=0,dflag=0,hr,min,sec;
union REGS inreg,outreg;
DISDATE:printf("\nCurrent date:\n");
inreg.h.ah=0x2a;
int86(0x21,&inreg,&outreg);
printf("%d-%d-%d ",outreg.h.dl,outreg.h.dh,outreg.x.cx);
switch(outreg.h.al)
{
case 0:printf("Sunday");break;
case 1:printf("Monday");break;
case 2:printf("Tueday");break;
case 3:printf("Wednesday");break;
case 4:printf("Thursday");break;
case 5:printf("Friday");break;
case 6:printf("Sunday");break;
}
if(dflag==1)
goto SETTIME;
DISTIME:printf("\nCurrent Time:\n");
inreg.h.ah=0x2c;
int86(0x21,&inreg,&outreg);
printf("%d:%d:%d.%d ",outreg.h.ch,outreg.h.cl,outreg.h.dh,outreg.h.dl);
if(tflag==1)
goto END;
SETDATE:printf("\nSet Date.\nDay,Month and Year\n");
scanf("%d%d%d",&day,&month,&year);
inreg.h.ah=0x2b;
inreg.x.cx=year;
inreg.h.dh=month;
inreg.h.dl=day;
int86(0x21,&inreg,&outreg);
if(outreg.h.al==0x00)
{
dflag=1;
goto DISDATE;
}
else
{
printf("\nInvalid entry");
getch();
exit(0);
}
SETTIME:printf("\nSet Time. Enter Time.\nHour,Minutes and Seconds\n");
scanf("%d%d%d",&hr,&min,&sec);
inreg.h.ah=0x2d;
inreg.h.ch=hr;
inreg.h.dh=sec;
inreg.h.cl=min;
inreg.h.dl=0;
int86(0x21,&inreg,&outreg);
if(outreg.h.al==0x00)
{
tflag=1;
goto DISTIME;
}
else
{
printf("\nInvalid entry");
getch();
exit(0);
}
END: getch();
}

No comments: