80x86描述符总结及解析描述符的小程序
生活随笔
收集整理的這篇文章主要介紹了
80x86描述符总结及解析描述符的小程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、描述符的分類
二、描述符格式
1、數據段描述符
2、代碼段描述符
3、LDT段描述符
4、TSS段描述符
5、調用門描述符
6、中斷門描述符
7、陷阱門描述符
8、任務門描述符
三、解析描述符的小程序
#include <stdio.h>//定義描述符中的低32位 struct seg_des_low_word {unsigned int limit_0_15:16;unsigned int base_0_15 :16;}; //定義描述符中的高32位(通用格式) struct seg_des_high_word {unsigned int base_16_23 :8;unsigned int type :4;unsigned int s :1;unsigned int dpl :2;unsigned int p :1;unsigned int limit_16_19:4;unsigned int avl :1;unsigned int l :1;//保留給64位處理器使用unsigned int d_b :1;unsigned int g :1;unsigned int base_24_31 :8; };void parse_seg_des(unsigned int low_dw, unsigned int hig_dw) {struct seg_des_low_word *pl =(struct seg_des_low_word*)&low_dw;struct seg_des_high_word *ph=(struct seg_des_high_word*)&hig_dw;unsigned int type = ph->type;int offset = (hig_dw & 0xFFFF0000) | (low_dw & 0x0000FFFF);unsigned int seg_base = (ph->base_24_31<<24)|(ph->base_16_23<<16)|pl->base_0_15;//拼接基地址字段unsigned int seg_limit =seg_limit = (ph->limit_16_19<<16)|pl->limit_0_15;//拼接段限長字段int system = ph->s;//下面的字段輸出是不是很方便?這就是位字段的好處之一printf("P = %d\n",ph->p);printf("DPL = %d\n",ph->dpl); //P和DPL是每個段描述符都有的if(system == 0) //system segment{if(type == 9 || type==11 || type==2) //==的優先級高于||{//print base address,limit,Gprintf("base address = %#X\n",seg_base);printf("limit = %#X ,",seg_limit);if(ph->g == 1)printf("以4KB為單位\n");elseprintf("以B為單位\n");}if(type == 9)printf("TSS段,不忙\n");else if(type == 11)printf("TSS段,忙\n"); else if(type == 5)printf("任務門,TSS段選擇子 = %#X\n",pl->base_0_15);else if(type == 6)printf("16位中斷門,段選擇子 = %#X,偏移 = %#X\n",pl->base_0_15,offset);else if(type == 14)printf("32位中斷門,段選擇子 = %#X,偏移 = %#X\n",pl->base_0_15,offset);else if(type == 7)printf("16位陷阱門,段選擇子 = %#X,偏移 = %#X\n",pl->base_0_15,offset);else if(type == 15)printf("32位陷阱門,段選擇子 = %#X,偏移 = %#X\n",pl->base_0_15,offset);else if(type == 12)printf("調用門,段選擇子 = %#X,偏移 = %#X , 參數個數 = %d\n",pl->base_0_15,offset, hig_dw & 0x1F);else if(type == 2)printf("LDT描述符\n");}else //data or code segment{printf("base address = %#X\n",seg_base);printf("limit = %#X ,",seg_limit);if(ph->g == 1)printf("以4KB為單位\n");elseprintf("以B為單位\n");if(type & 0x08 ) //代碼段,分析C,R,D {printf("代碼段,");if(ph->d_b == 1) //D=1printf("默認操作數大小為32位\n");elseprintf("默認操作數大小為16位\n");if(type & 0x04) //C=1printf("一致性,");elseprintf("非一致性,");if(type & 0x02) //R=1printf("可讀\n");elseprintf("不可讀\n");}else //數據段,分析B,E,W{printf("數據段,");if(ph->d_b == 1) //B=1printf("棧段使用ESP,ESP最大為0xFFFFFFFF\n");elseprintf("棧段使用SP,SP最大為0xFFFF\n");if(type & 0x04) //E=1printf("向下擴展,");elseprintf("向上擴展,");if(type & 0x02) //W=1printf("可寫\n");elseprintf("不可寫\n");}}}int main(void) {printf("please input the segment descriptor,format: [low dword] [high dword]\n");printf("example:\n");printf("000003ff 00c0fa00\n");printf("The Descriptor:\n");unsigned int l_dword = 0;unsigned int h_dword = 0;//請求用戶輸入描述符,先是低32位,再是高32位scanf("%x" "%x",&l_dword,&h_dword);printf("----------------------\n");parse_seg_des(l_dword,h_dword);return 0; }以上代碼編譯后運行:
please input the segment descriptor,format: [low dword] [high dword]
example:
000003ff 00c0fa00
The Descriptor:
提示用戶輸入段描述符,先輸入低32位,再輸入高32位。注意:中間以空格分開,且以16進制輸入,前面不用加“0x”。
比如,用戶輸入:
80000002 c0920b
回車后得到:
——————–
P = 1
DPL = 0
base address = 0XB8000
limit = 0X2 ,以4KB為單位
數據段,棧段使用ESP,ESP最大為0xFFFFFFFF
向上擴展,可寫
總結
以上是生活随笔為你收集整理的80x86描述符总结及解析描述符的小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css元素隐藏不可获取,Css隐藏元素(
- 下一篇: 工作学习