Добавил:
Mymnan
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Сборка Часть1 / avmis_labs / неразобрано / 501 / Лаб_Память / Гамзюк С.А / lab
.C#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <stdarg.h>
#pragma inline
#define REALMODE 0
#define VIRTUALMODE 1
#define PROTECTMODE 2
#define MAXSTR 200
union REGS inregs, outregs;
struct SREGS segregs;
unsigned hostdata_seg, hostdata_size, dpmi_flags;
unsigned int sRegs[12];
char * fname = "output.txt";
void (far *realEPoint)();
void (far *protectEPoint)();
int initialDPMI(void);
int test_mode();
void try_mode();
void switch_mode();
void to_real();
void to_protected();
void save_regs();
void dos_exit(unsigned);
void main()
{
int mode;
char c;
clrscr();
if (initialDPMI() == -1)
{
printf("Error dpmi init\n");
getch();
return 0;
}
save_regs();
asm begn label word
do
{
clrscr();
mode = test_mode();
//printf("6");
//getch();
if (mode == PROTECTMODE)
{
printf("Protected mode!\n\n");
}
if (mode == VIRTUALMODE)
{
printf("Virtual mode! \n\n");
}
if (mode == REALMODE)
{
printf("Real mode! \n\n");
}
printf("Laba 4\n");
printf("-------------------\n");
printf("[1] Try mode\n");
printf("[2] Switch mode\n");
printf("[0] Exit\n");
c = getch();
switch (c)
{
case '1': try_mode();
break;
case '2': switch_mode();
//printf("4");
//getch();
break;
case '0': break;
default: break;
}
//printf("5");
//getch();
} while (c != '0');
dos_exit(0);
}
void dos_exit(unsigned err)
{
asm mov ax, err
asm mov ah, 04ch
asm int 21h
}
int initialDPMI(void)
{
inregs.x.ax = 0x1687;
int86x(0x2F, &inregs, &outregs, &segregs);
if(outregs.x.ax != 0)
{
printf("Error DPMI");
return (-1);
}
dpmi_flags = outregs.x.bx;
hostdata_size = outregs.x.si;
FP_SEG(protectEPoint) = segregs.es;
FP_OFF(protectEPoint) = outregs.x.di;
if(hostdata_size)
{
if(_dos_allocmem(hostdata_size, &hostdata_seg) != 0)
{
printf("Error allocmem");
return (-1);
}
}
return 0;
}
int test_mode()
{
int res = PROTECTMODE;
int pe;
long flags, vm;
asm mov ax,1686h
asm int 2fh
asm mov res,ax
if (res == 0)
res = PROTECTMODE;
else
{
asm pushfd
asm pop ebx
asm mov flags, ebx
vm = flags & 131072;
if (!!vm)
res = VIRTUALMODE;
else
res = REALMODE;
}
return res;
}
void switch_mode()
{
if (test_mode() == REALMODE)
{
to_protected();
}
else
if (test_mode() == VIRTUALMODE)
{
to_protected();
}
else
if (test_mode() == PROTECTMODE)
{
to_real();
}
//printf("3");
//getch();
}
void to_real()
{
unsigned int sel, off;
asm {
mov ax,0306h
int 31h
mov sel, si
mov off, di
}
realEPoint = MK_FP(sel, off);
asm {
mov ax,word ptr sRegs[0]
mov cx,word ptr sRegs[2]
mov dx,word ptr sRegs[4]
mov bx,word ptr sRegs[6]
mov si,word ptr sRegs[8]
mov di,word ptr sRegs[10]
}
(*realEPoint)();
}
void to_protected()
{
asm {
mov ax, hostdata_seg
mov es, ax
mov ax, dpmi_flags
}
(*protectEPoint)();
}
void save_regs()
{
asm {
push ax
mov ax, ds
mov word ptr sRegs[0], ax
mov ax, es
mov word ptr sRegs[2], ax
mov ax, ss
mov word ptr sRegs[4], ax
mov ax, sp
mov word ptr sRegs[6], ax
mov ax, cs
mov word ptr sRegs[8], ax
push dx
lea dx, begn
mov word ptr sRegs[10], dx
pop dx
pop ax
}
}
void try_mode()
{
char c;
int mode;
int bts, res_bts;
char string[MAXSTR];
char res_string[MAXSTR];
FILE * file;
string[0] = res_string[0] = '\0';
printf("-------------------\n");
mode = test_mode();
if (mode == PROTECTMODE)
printf("Protected mode\n");
if (mode == REALMODE)
printf("Real mode\n");
if (mode == VIRTUALMODE)
printf("Virtual mode\n");
do
{
clrscr();
printf("[1] RAM -> HDD\n");
printf("[2] HDD -> RAM\n");
printf("[0] Exit to menu\n");
c = getch();
switch (c)
{
case '1':
{
if ((file = fopen(fname, "w")) == NULL)
{
printf("Error open file\n");
return;
}
printf("Enter a string:\n");
scanf("%s", string);
printf("RAM -> HDD... \n");
bts = fwrite(string, sizeof(char), strlen(string), file);
fclose(file);
printf("success %d bytes!\n", bts);
getch();
break;
}
case '2':
{
if ((file = fopen(fname, "r")) == NULL)
{
printf("Error open file\n");
return;
}
printf("HDD -> RAM... \n");
res_bts = fread(res_string, sizeof(char), MAXSTR, file);
res_string[res_bts] = '\0';
if (res_bts > 0)
{
printf("success %d bytes! (%s)\n", res_bts, res_string);
}
else
printf("empty string\n");
getch();
break;
}
case '0': break;
default : break;
}
} while (c != '0');
return;
}