经典编程900例(c语言)(第二十二篇)

这篇的所有代码都是需在tc下编译,太老了,就不亲测了,纯搬运了,算是看看前辈都是怎么写代码的吧
有感兴趣的可以自己编译运行
tc下载

例258:

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

void main(void)
{
    int drive;

    drive = bdos(0x19, 0, 0);

    printf("Current drive is %c\n", 'A' + drive);
    
    return 0;
}

例259:

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

void main(void)
{
    struct COUNTRY info;

    country(0, &info);

    if (info.co_date == 0)
        printf("Date format: mm/dd/yy\n");
    else if (info.co_date == 1)
        printf("Date format: dd/mm/yy\n");
    else if (info.co_date == 2)
        printf("Date format: yy/mm/dd\n");

    printf("Currency symbol %s\n", info.co_curr);
    printf("Decimal separator %s\n", info.co_thsep);
    printf("Date separator %s Time separator %s\n",
           info.co_dtsep, info.co_tmsep);

    if (info.co_currstyle == 0)
        printf("Currency symbol precedes with no leading spaces\n");
    else if (info.co_currstyle == 1)
        printf("Currency symbol follows with no spaces\n");
    else if (info.co_currstyle == 2)
        printf("Currency symbol precedes with leading space\n");
    if (info.co_currstyle == 4)
        printf("Currency symbol follows with space\n");

    printf("Currency significant digits %d\n", info.co_digits);

    if (info.co_time)
        printf("24 hour time\n");
    else
        printf("12 hour time\n");

    printf("Data separator %s\n", info.co_dasep);
}

例260:

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

void main(void)
{
    printf("Previous extended Ctrl-Break status %s\n",
           (getcbrk()) ? "On": "Off");

    setcbrk(0);  // Turn if off
}

例261:

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

void main(void)
{
    char far *dta;

    dta = getdta();

    printf("Current DTA is %lX\n", dta);

    if (MK_FP(_psp, 0x80) == dta)
        printf("DTA is at same location as command line\n");

    dta = _fmalloc(128);
    setdta(dta);

    printf("New DTA is %lX\n", getdta());
}

例262:

#include <stdio.h>
#include <bios.h>

void main(void)
{
    unsigned int state, old_state = 0;

    do
    {
        state = _bios_keybrd(_KEYBRD_SHIFTSTATUS);

        if (state != old_state)
        {
            old_state = state;

            if (state & 0x80)
                printf("Ins On ");
            if (state & 0x40)
                printf("Caps On ");
            if (state & 0x20)
                printf("Num Lock On ");
            if (state & 0x10)
                printf("Scroll Lock On ");
            if (state & 0x08)
                printf("Alt pressed ");
            if (state & 0x04)
                printf("Ctrl pressed ");
            if (state & 0x02)
                printf("Left shift pressed ");
            if (state & 0x01)
                printf("Right shift pressed ");
            printf("\n");
        }
    }
    while (! _bios_keybrd(_KEYBRD_READY));
}

例263:

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

void main (void)
{
    int status = 0;
    int old_status = 0;

    do
    {
        status = biosprint(2, 0, 0);  // Read LPT1

        if (status != old_status)
        {
            if (status & 1)
                printf ("Time-out\t");

            if (status & 8)
                printf ("Output Error\t");

            if (status & 16)
                printf ("Printer Selected\t");

            if (status & 32)
                printf ("Out of Paper\t");

            if (status & 64)
                printf ("Acknowledge\t");

            if (status & 128)
                printf ("Printer Not Busy");

            printf ("\n");

            old_status = status;
        }
    }
    while (! kbhit());
}

例264:

#include <stdio.h>
#include <bios.h>

void main(void)
{
    char i = 0, title[] = "Jamsa's 1001 C/C++ Tips";

    unsigned status;

    status = _bios_serialcom(_COM_INIT, 0, _COM_9600 | _COM_CHR8 | _COM_STOP1 |
                             _COM_NOPARITY);

    if (status & 0x100) // Data ready
        while (title[i])
        {
            _bios_serialcom(_COM_SEND, 0, title[i]);
            putchar(title[i]);
            i++;
        }
}

例265:

#include <stdio.h>
#include <bios.h>

void main(void)
{
    struct Equip
    {
        unsigned floppy_available:1;
        unsigned coprocessor_available:1;
        unsigned system_memory:2;
        unsigned video_memory:2;
        unsigned floppy_disk_count:2;
        unsigned unused_1:1;
        unsigned serial_port_count:3;
        unsigned game_adapter_available:1;
        unsigned unused_2:1;
        unsigned printer_count:2;
    } ;

    union Equipment
    {
        unsigned list;
        struct Equip list_bits;
    } equip;

    equip.list = _bios_equiplist();

    if (equip.list_bits.coprocessor_available)
        printf("Math coprocessor available\n");
    else
        printf("No math coprocessor\n");

    printf("System board memory %d\n",
           (equip.list_bits.system_memory + 1) * 16);

    printf("Number of floppies %d\n",
           equip.list_bits.floppy_disk_count + 1);

    printf("Number of printers %d\n",
           equip.list_bits.printer_count);

    printf("Number of serial ports %d\n",
           equip.list_bits.serial_port_count);
}

例266:

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

void main(void)
{
    unsigned frequency;

    do
    {
        for (frequency = 500; frequency <= 1000; frequency += 50)
        {
            sound(frequency);
            delay(50);
        }
        for (frequency = 1000; frequency >= 500; frequency -= 50)
        {
            sound(frequency);
            delay(50);
        }
    }
    while (! kbhit());

    nosound();
}

例267:

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

void main(void)
{
    printf("About to sleep for 5 seconds\n");
    sleep(5);
    printf("Awake\n");
}

例268:

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

extern unsigned _stklen = 8096;

void main(void)
{
    printf("The current stack size %d bytes\n", _stklen);
}

例269:

#include <stdio.h>
#include <bios.h>

void main (void)
{
    printf("BIOS Memory report %dKb\n", biosmemory());
    printf("BIOS Memory report %dKb\n", _bios_memsize());
}

例270:

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

#define VIDEO 0xB800   // CGA base

void main(void)
{
    FILE *fp;
    int offset;

    if ((fp = fopen("SAVE_SCR.DAT", "wb")) == NULL)
        printf("Error opening file\n");
    else
    {
        for (offset = 0; offset < 8000; offset++)
            fprintf(fp, "%c", peekb(VIDEO, offset));
        fclose(fp);
    }
}

例271:

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

#define VIDEO 0xB800   // CGA base

void main(void)
{
    FILE *fp;
    int offset;
    char value;

    if ((fp = fopen("SAVE_SCR.DAT", "rb")) == NULL)
        printf("Error opening file\n");
    else
    {
        for (offset = 0; offset < 8000; offset++)
        {
            fscanf(fp, "%c", &value);
            pokeb(VIDEO, offset, value);
        }
        fclose(fp);
    }
}

例272:

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

void main (void)
{
    struct CMOS
    {
        unsigned char current_second;
        unsigned char alarm_second;
        unsigned char current_minute;
        unsigned char alarm_minute;
        unsigned char current_hour;
        unsigned char alarm_hour;
        unsigned char current_day_of_week;
        unsigned char current_day;
        unsigned char current_month;
        unsigned char current_year;
        unsigned char status_registers[4];
        unsigned char diagnostic_status;
        unsigned char shutdown_code;
        unsigned char drive_types;
        unsigned char reserved_x;
        unsigned char disk_1_type;
        unsigned char reserved;
        unsigned char equipment;
        unsigned char lo_mem_base;
        unsigned char hi_mem_base;
        unsigned char hi_exp_base;
        unsigned char lo_exp_base;
        unsigned char fdisk_0_type;
        unsigned char fdisk_1_type;
        unsigned char reserved_2[19];
        unsigned char hi_check_sum;
        unsigned char lo_check_sum;
        unsigned char lo_actual_exp;
        unsigned char hi_actual_exp;
        unsigned char century;
        unsigned char information;
        unsigned char reserved3[12];
    } cmos;

    char i;

    char *pointer;
    char byte;

    pointer = (char *) &cmos;

    for (i = 0; i < 0x34; i++)
    {
        outportb(0x70, i);
        byte = inportb(0x71);
        *pointer++ = byte;
    }

    printf("Current date %d/%d/%d\n", cmos.current_month,
           cmos.current_day, cmos.current_year);
    printf("Current time %d:%d:%d\n", cmos.current_hour,
           cmos.current_minute, cmos.current_second);
    printf("Hard disk type %d\n", cmos.fdisk_0_type);
}

例273:

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <string.h>

int check_for_ems(void)
{
    union REGS inregs, outregs;
    struct SREGS segs;

    int major, minor;   // DOS version

    struct DeviceHeader
    {
        struct DeviceHeader far *link;
        unsigned attributes;
        unsigned strategy_offset;
        unsigned interrupt_offset;
        char name_or_number_of_units[8];
    } far *dev;

    int i;

    char driver_name[9];

    // Get the DOS version
    inregs.x.ax = 0x3001;
    intdos (&inregs, &outregs);
    major = outregs.h.al;
    minor = outregs.h.ah;

    if (major < 2)
        return(0);     // Requires DOS 2.0
    else
    {
        // Get the list of lists
        inregs.h.ah = 0x52;
        intdosx (&inregs, &outregs, &segs);

        if (major == 2)
            dev = (struct DeviceHeader far *)
                  MK_FP(segs.es + 1, outregs.x.bx + 7);
        else if ((major == 3) && (minor == 0))
            dev = (struct DeviceHeader far *)
                  MK_FP(segs.es + 2, outregs.x.bx + 8);
        else
            dev = (struct DeviceHeader far *)
                  MK_FP(segs.es + 2, outregs.x.bx + 2);

        while (FP_OFF(dev) != 0xFFFF)
        {
            if (dev->attributes & 0x8000)
            {
                // Character device
                for (i = 0; i < 8; i++)
                    driver_name[i] = dev->name_or_number_of_units[i];
                driver_name[8] = NULL;
            }

            if (! strcmp(driver_name, "EMMXXXX0"))
                return(1);   // Found driver

            dev = dev->link;
        }
    }

    return(0);
}

void main (void)
{
    union REGS inregs, outregs;
    struct SREGS segs;

    unsigned handle;
    unsigned page;
    unsigned index, page_number;

    unsigned page_frame_address;

    char far *data;

    if (check_for_ems())
    {
        inregs.h.ah = 0x40;
        int86 (0x67, &inregs, &outregs);

        // Make sure EMS is functional
        if (outregs.h.ah == 0)
        {
            // Allocate a handle for 5 pages
            inregs.h.ah = 0x43;
            inregs.x.bx = 5;
            int86 (0x67, &inregs, &outregs);

            if (outregs.h.ah == 0)
            {
                handle = outregs.x.dx;

                // Get the page frame address
                inregs.h.ah = 0x41;
                int86 (0x67, &inregs, &outregs);

                if (outregs.h.ah == 0)
                {
                    page_frame_address = outregs.x.bx;

                    // map the first 4 pages
                    for (page_number = 0; page_number < 4; page_number++)
                    {
                        inregs.h.ah = 0x44;
                        inregs.h.al = page_number;  // Physical page
                        inregs.x.bx = page_number;  // Logical page
                        inregs.x.dx = handle;

                        int86 (0x67, &inregs, &outregs);

                        if (outregs.h.ah != 0)
                        {
                            printf ("Error mapping pages %xH\n",
                                    outregs.h.ah);

                            // Release the handle
                            inregs.h.ah = 0x45;
                            inregs.x.dx = handle;
                            int86 (0x67, &inregs, &outregs);
                            exit(0);
                        }
                    }
                    // Fill the first four pages
                    data = (char far *) MK_FP(page_frame_address, 0);

                    for (index = 0; index < 16384; index++)
                        data[index] = 'A';

                    for (index = 16384; index < 32768; index++)
                        data[index] = 'B';

                    for (index = 32768; index < 49152; index++)
                        data[index] = 'C';

                    for (index = 49152; index != 0; index++)
                        data[index] = 'D';

                    // Map logical page 4 into physical page 1
                    inregs.h.ah = 0x44;
                    inregs.h.al = 1;  // Physical page
                    inregs.x.bx = 4;  // Logical page
                    inregs.x.dx = handle;

                    int86 (0x67, &inregs, &outregs);

                    if (outregs.h.ah != 0)
                    {
                        printf ("Error mapping page %xH\n",
                                outregs.h.ah);

                        // Release the handle
                        inregs.h.ah = 0x45;
                        inregs.x.dx = handle;
                        int86 (0x67, &inregs, &outregs);
                        exit(0);
                    }

                    // Fill page 4 which resides in page 1
                    for (index = 16384; index < 32768; index++)
                        data[index] = 'E';

                    // Display the first 20 bytes of each page
                    printf ("Physical Page Zero\n");
                    for (index = 0; index < 20; index++)
                        printf ("%c ", data[index]);

                    printf ("\nPhysical Page One\n");
                    for (index = 16384; index < 16404; index++)
                        printf ("%c ", data[index]);

                    printf ("\nPhysical Page Two\n");
                    for (index = 32768; index < 32788; index++)
                        printf ("%c ", data[index]);

                    printf ("\nPhysical Page Three\n");
                    for (index = 49152; index < 49172; index++)
                        printf ("%c ", data[index]);

                    // Map logical page 1 into physical page 3
                    inregs.h.ah = 0x44;
                    inregs.h.al = 3;  // Physical page
                    inregs.x.bx = 1;  // Logical page
                    inregs.x.dx = handle;

                    int86 (0x67, &inregs, &outregs);

                    if (outregs.h.ah != 0)
                    {
                        printf ("Error mapping page %xH\n",
                                outregs.h.ah);

                        // Release the handle
                        inregs.h.ah = 0x45;
                        inregs.x.dx = handle;
                        int86 (0x67, &inregs, &outregs);
                        exit(0);
                    }

                    printf ("\nMapping logical page 1 to physical page 3");
                    printf ("\nPhysical Page Three\n");
                    for (index = 49152; index < 49162; index++)
                        printf ("%c ", data[index]);

                }
                else
                    printf ("Error getting base address %xH\n",
                            outregs.h.ah);

                // Release the handle
                inregs.h.ah = 0x45;
                inregs.x.dx = handle;
                int86 (0x67, &inregs, &outregs);
            }
            else
                printf ("Error allocating 5 pages %xH\n",
                        outregs.h.ah);
        }
        else
            printf ("EMM not functional\n");
    }
    else
        printf ("EMS driver not present\n");

}

例274:

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

void main (void)
{
    struct SREGS segs;

    segread(&segs);

    printf("CS %X DS %X SS %X ES %X\n", segs.cs,
           segs.ds, segs.ss, segs.es);
}

例275:

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

void main(void)
{
    printf("The current stack size %d bytes\n", _stklen);
}

例276:

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

// Note: If you want to use XMS function 0xB, this simple
// xms_access routine only works for tiny, small, or medium
// models where the value of the data segment does not change.

void xms_access (union REGS *inregs, union REGS *outregs)
{
    union REGS in, out;
    struct SREGS segs;

    unsigned segs_ds, save_bx;
    unsigned flags;

    void far (*xms)(void);  // pointer to the XMS services

    // Get the entry point to the XMS services
    in.x.ax = 0x4310;
    int86x (0x2F, &in, &out, &segs);

    xms = (void far *) (((long) (segs.es) << 16) + out.x.bx);

    // Assign the input registers to the actual registers

    _AX = inregs->x.ax;
    _CX = inregs->x.cx;
    _DX = inregs->x.dx;
    _SI = inregs->x.si;
    _DI = inregs->x.di;
    _BX = inregs->x.bx;

    xms();   // Call the XMS entry point.

    // Assign the registers to the output register structure
    asm
    {
        pushf
        push bx

        pop save_bx
        pop flags
    }

    outregs->x.ax = _AX;
    outregs->x.bx = save_bx;
    outregs->x.cx = _CX;
    outregs->x.dx = _DX;
    outregs->x.si = _SI;
    outregs->x.di = _DI;

    outregs->x.cflag = flags & 1;
    outregs->x.flags = flags;
}


void main (void)
{
    union REGS inregs, outregs;
    struct SREGS segs;

    FILE *data;

    char data_buffer[256];

    struct xms_move
    {
        long byte_count;         // Number of bytes to move
        unsigned source_handle;  // Data to move
        long source_offset;
        unsigned destination_handle;
        long destination_offset;
    } block;

    unsigned handle;

    int string_length;
    long character_count = 0L;
    int transfer_error = 0;   // 1 if a transfer error occurs
    int i, extra_byte;

    void xms_access (union REGS *, union REGS *);

    inregs.x.ax = 0x4300;
    int86 (0x2F, &inregs, &outregs);

    if (outregs.h.al != 0x80)
        printf ("XMS driver not installed\n");
    else
    {
        // Allocate the extended memory
        inregs.h.ah = 9;
        inregs.x.dx = 64;  // Size 64Kb

        xms_access (&inregs, &outregs);

        if (outregs.x.ax == 0)
            printf ("Error allocating extended memory %2xH\n",
                    outregs.h.bl);
        else
        {
            handle = outregs.x.dx;

            // Read the file into a conventional memory
            // buffer and then move the data to extended memory

            if ((data = fopen ("\\AUTOEXEC.BAT", "r")) == NULL)
                printf ("Error opening AUTOEXEC.BAT\n");
            else
            {
                segread (&segs);

                while (fgets (data_buffer, sizeof(data_buffer), data))
                {
                    // Copy data_buffer to extended memory
                    string_length = strlen(data_buffer);

                    block.byte_count = string_length + 1;

                    // transfer amount must be even
                    if (block.byte_count % 2)
                        block.byte_count++;

                    block.source_handle = 0;
                    block.source_offset = (void far *)
                                          MK_FP(segs.ds, data_buffer);
                    block.destination_handle = handle;
                    block.destination_offset = character_count;
                    character_count += string_length + 1;

                    inregs.h.ah = 0xB;
                    inregs.x.si = (unsigned) &block;

                    xms_access (&inregs, &outregs);

                    if (outregs.x.ax == 0)
                    {
                        transfer_error = 1;
                        break;
                    }
                }
            }

            if (transfer_error)
                printf ("Error in data transfer\n");
            else
            {
                block.destination_handle = 0;
                block.source_handle = handle;
                block.destination_offset = (void far *)
                                           MK_FP(segs.ds, data_buffer);

                block.source_offset = 0L;
                block.byte_count = sizeof(data_buffer);

                while (block.source_offset <
                        character_count)
                {
                    if ((block.byte_count + block.source_offset)
                            > character_count)
                        block.byte_count = character_count -
                                           block.source_offset;

                    // Transfer amount must be even
                    if (block.byte_count % 2)
                    {
                        block.byte_count++;
                        extra_byte = 1;
                    }
                    else
                        extra_byte = 0;

                    xms_access (&inregs, &outregs);

                    if (outregs.x.ax == 0)
                    {
                        transfer_error = 1;
                        break;
                    }

                    for (i = 0; i < (block.byte_count -
                                     extra_byte); i++)
                        if (data_buffer[i])
                            putchar(data_buffer[i]);

                    block.source_offset += block.byte_count;
                }
            }

            if (transfer_error)
                printf ("Error in data transfer\n");

            // Release extended memory
            inregs.h.ah = 0x0A;
            inregs.x.dx = handle;

            xms_access (&inregs, &outregs);

            if (outregs.x.ax == 0)
                printf ("Error releasing extending memory %2xH\n",
                        outregs.h.bl);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值