免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4540 | 回复: 11
打印 上一主题 下一主题

[高级]gcc 中 怎么得到 一个变量的 段地址和偏移地址?? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-08-26 13:23 |只看该作者 |倒序浏览
gcc 中 怎么得到 一个变量的 段地址和偏移地址??
tc中 FP_SEG 类似的?? 有么 请高手指点
djgpp 中怎么搞 主要目的是使用int86x()
----------------------------------------------------------
下面的程序在xp下 编译成功 但是运行程序出错 为啥?
--------------------------------------------------------
#include<stdio.h>;
#include<dos.h>;

#define FP_OFF(fp)        ((unsigned)(fp))
#define FP_SEG(fp)        ((unsigned)((unsigned long)(fp) >;>; 16))

typedef unsigned int WORD;
typedef unsigned long DWORD;
typedef unsigned long long QWORD;
unsigned long CYLINDERS,HEADS,SECTOR_PER_TRACK;
unsigned long long SECTORS;
unsigned int BYTE_PER_SECTOR;

int get_parameter(int drive)
{
        union REGS regs;
        struct SREGS sregs;
        struct{
                WORD packetsize;
                WORD infoflags;
                DWORD cylns;
                DWORD heads;
                DWORD sects_per_track;
                QWORD sectors;
                WORD bps;
        }package;
        regs.h.ah=0x48;
        regs.h.dl=0x80;
        sregs.ds=FP_SEG(&package);
        regs.x.si=FP_OFF(&package);
        int86x(0x13,&regs,&regs,&sregs);
        CYLINDERS=package.cylns;
        HEADS=package.heads;
        SECTOR_PER_TRACK=package.sects_per_track;
        SECTORS=package.sectors;
        BYTE_PER_SECTOR=package.bps;
        if (regs.h.ah)
                return regs.h.ah;
        else
                return 0;
}

int main()
{
        if (get_parameter(0x80)==0)
        {
                printf("%ldCYLINDERS:\n",CYLINDERS);
                printf("%ldHEADS:\n",HEADS);
                printf("%ldSECTOR_PER_TRACK:\n",SECTOR_PER_TRACK);
                printf("%lldSECTORS:\n",SECTORS);
                printf("%dBYTE_PER_SECTOR:\n",BYTE_PER_SECTOR);
        }
       
        return 0;
}

论坛徽章:
0
2 [报告]
发表于 2005-08-26 13:44 |只看该作者

[高级]gcc 中 怎么得到 一个变量的 段地址和偏移地址??

嘿嘿,你的概念早过时了

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
3 [报告]
发表于 2005-08-26 13:45 |只看该作者

[高级]gcc 中 怎么得到 一个变量的 段地址和偏移地址??

这个代码我没有看,但是你要得到 段地址和偏移地址 是和cpu有关的,有的cpu没有分段机制,有的cpu可以选择是否分段,所以,没有任何检测代码的代码不是健壮的代码。

另外,XP对物理设备的访问有保护,除非你写驱动,否则肯定出错。

论坛徽章:
0
4 [报告]
发表于 2005-08-26 13:46 |只看该作者

[高级]gcc 中 怎么得到 一个变量的 段地址和偏移地址??

段地址和偏移可以有很多种答案的
这个你翻翻那些老书就知道了

论坛徽章:
0
5 [报告]
发表于 2005-08-26 13:52 |只看该作者

[高级]gcc 中 怎么得到 一个变量的 段地址和偏移地址??

嵌入汇编取段地址寄存器ds,偏移应该就是&name,但我不知道保护模式下可不可以这样做.

论坛徽章:
0
6 [报告]
发表于 2005-08-26 19:25 |只看该作者

[高级]gcc 中 怎么得到 一个变量的 段地址和偏移地址??

我 当然是在x86上写的,各位没看到 下面两个宏是tc2  dos。h中定义的
用来取得段地址和偏移地址
#define FP_OFF(fp) ((unsigned)(fp))
#define FP_SEG(fp) ((unsigned)((unsigned long)(fp) >;>; 16))
可在djgpp中使用 好像有些问题,到底是什么问题呢〉?

论坛徽章:
0
7 [报告]
发表于 2005-08-26 20:24 |只看该作者

[高级]gcc 中 怎么得到 一个变量的 段地址和偏移地址??

求各位高手帮忙调试一下程序 谢谢阿

论坛徽章:
0
8 [报告]
发表于 2005-08-26 20:53 |只看该作者

[高级]gcc 中 怎么得到 一个变量的 段地址和偏移地址??

既然用的是djgpp,那就按照他的规则来.不要把TC的那一套套在DJGPP的头上.


  1. 看一下下面两个函数.
  2. #include <bios.h>;

  3. int biosdisk(int cmd, int drive, int head, int track,
  4.      int sector, int nsects, void *buffer)

  5. char buffer[512];
  6. if (biosdisk(2, 0x80, 0, 0, 0, 1, buffer))
  7.   error("disk");

  8. #include <bios.h>;

  9. unsigned _bios_disk(unsigned cmd, struct diskinfo_t *di)

  10. char record_buffer[512];
  11. struct diskinfo_t di;

  12. di.drive    = 0x80;
  13. di.head     = 0;
  14. di.track    = 0;
  15. di.sector   = 1;
  16. di.nsectors = 1;
  17. di.buffer   = &record_buffer;
  18. if ( _bios_disk(_DISK_READ, &di) )
  19.   puts("Disk error.");

复制代码

只需要上面两个函数应当就可以解决你的问题了.

另外,要注意的是,在2000-XP上面,你应当使用2.03版以上的.否则会出很多问题.

论坛徽章:
0
9 [报告]
发表于 2005-08-27 11:41 |只看该作者

[高级]gcc 中 怎么得到 一个变量的 段地址和偏移地址??

楼上的兄弟误会了
我是要调用bios int 13 的 48h功能取得磁盘驱动器的相关参数
biosdisk()怎么能达到我的目的呢?

论坛徽章:
0
10 [报告]
发表于 2005-08-27 11:51 |只看该作者

[高级]gcc 中 怎么得到 一个变量的 段地址和偏移地址??


  1. #include <bios.h>;

  2. int biosdisk(int cmd, int drive, int head, int track,
  3.      int sector, int nsects, void *buffer);
  4. Description

  5. This function interfaces with the BIOS disk sevice (interrupt 0x13). Please refer to a BIOS reference manual for detailed information about the parameters of this call. All known calls are supported. A sector size of 512 bytes is assumed.
  6. 0 - reset disk subsystem
  7. 1 - get disk subsystem status
  8. 2 - read one or more sectors
  9. 3 - write one or more sectors
  10. 5 - format a track
  11. 6 - format back track
  12. 7 - format drive
  13. 8 - get drive parameters
  14. 9 - initialize drive parameters
  15. 10 - read long sectors
  16. 11 - write long sectors
  17. 12 - seek to cylinder
  18. 13 - alternate fixed disk reset
  19. 14 - read test buffer
  20. 15 - write test buffer
  21. 16 - test for drive ready
  22. 17 - recalibrate drive
  23. 18 - controller RAM diagnostic
  24. 19 - controller drive diagnostic
  25. 20 - controller internal diagnostic
  26. 15 - read fixed disk type
  27. 22 - read disk change line status
  28. 23 - set DASD type (pass dasd in nsects)
  29. 24 - set media type for format

  30. The first request with more sectors than will fit in the transfer buffer will cause a DOS buffer to be allocated. This buffer is automatically freed when your application exits. Since this buffer is big enough to hold 18 sectors, requests for more sectors than that will fail.

  31. Request eight returns values in buffer as follows:
  32. byte 0 = sectors per track (bits 0..5) and top two bits of cylinder (in bits 6..7)
  33. byte 1 = cyliders (bits 0..7)
  34. byte 2 = number of drives
  35. byte 3 = number of heads
  36. Return Value

  37. The value of AH returned by the BIOS
复制代码


The 8th got the driver parameter.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP