From 87940c9b0b308b42e952c64d6ef580c8e38230c7 Mon Sep 17 00:00:00 2001 From: yuchen Date: Fri, 6 Mar 2015 12:27:41 +0800 Subject: [PATCH] add more examples in lab0 exercise --- related_info/lab0/lab0_ex3.c | 65 ++++++++++++++++ related_info/lab0/lab0_ex4.c | 143 +++++++++++++++++++++++++++++++++++ 2 files changed, 208 insertions(+) diff --git a/related_info/lab0/lab0_ex3.c b/related_info/lab0/lab0_ex3.c index b59c7ee..4d2ce7e 100644 --- a/related_info/lab0/lab0_ex3.c +++ b/related_info/lab0/lab0_ex3.c @@ -49,3 +49,68 @@ main(void) return 0; } + +// other examples +//ex1 +#if 0 +#include +#include +#include +#include + +#define STS_TG32 0xF +#define STS_IG32 0xE +#define SETGATE( gate, istrap, sel, off, dpl) { \ + (gate).gd_off_15_0 = (uint32_t)(off) & 0xffff; \ + (gate).gd_ss = (sel); \ + (gate).gd_args = 0; \ + (gate).gd_rsv1 = 0; \ + (gate).gd_type = (istrap) ? STS_TG32 : STS_IG32; \ + (gate).gd_s = 0; \ + (gate).gd_dpl = (dpl); \ + (gate).gd_p = 1; \ + (gate).gd_off_31_16 = (uint32_t)(off) >> 16; \ +} +using namespace std; + + +struct gatedesc { + unsigned gd_off_15_0 : 16; // low 16 bits of offset in segment + unsigned gd_ss : 16; // segment selector + unsigned gd_args : 5; // # args, 0 for interrupt/trap gates + unsigned gd_rsv1 : 3; // reserved(should be zero I guess) + unsigned gd_type : 4; // type(STS_{TG,IG32,TG32}) + unsigned gd_s : 1; // must be 0 (system) + unsigned gd_dpl : 2; // descriptor(meaning new) privilege level + unsigned gd_p : 1; // Present + unsigned gd_off_31_16 : 16; // high bits of offset in segment +}; + + + + + +int main() +{ + gatedesc intr; + + intr.gd_off_15_0 = 8; + intr.gd_ss = 0; + intr.gd_args = 0; + intr.gd_rsv1 = 0; + intr.gd_type = 0; + intr.gd_s = 0; + intr.gd_dpl = 0; + intr.gd_p = 0; + intr.gd_off_31_16 = 0; + + SETGATE( intr, 0,1,2,3); + + printf( "%u\n", intr); + printf( "%x", intr); + + + + return 0; +} +#endif diff --git a/related_info/lab0/lab0_ex4.c b/related_info/lab0/lab0_ex4.c index 99d8509..6909260 100644 --- a/related_info/lab0/lab0_ex4.c +++ b/related_info/lab0/lab0_ex4.c @@ -25,3 +25,146 @@ int main() { printf("%d\n", ((struct entry *)p)->num); return 0; } + +// other examples + +// ex1 +#if 0 +include "list.h"; + +void main() { + node_t node1; + node1.data = 1; + list_entry_t *n1 = &nodes1.node_link; + node_t node2; + node2.data = 2; + list_init(n1); + list_add_after(n1, &nodes2.node_link); + printf("\n"); +} +#endif + +//ex2 +#if 0 +#include "list.h" +#include "defs.h" +#include + +struct page { + int test; + list_entry_t page_link; +}; + +#define le2page(le, member) to_struct((le), struct page, member) + +#define to_struct(ptr, type, member) \ +((type *)((char *)(ptr) - offsetof(type, member))) + +#define offsetof(type, member) \ + ((size_t)(&((type *)0)->member)) + + +typedef struct { + list_entry_t free_list; + unsigned int nr_free; +}free_area_t; + +int main(){ + + free_area_t free_area; + struct page pg; + free_area.free_list.next = &pg.page_link; + + pg.test = 1; + pg.page_link.next = &free_area.free_list; + list_entry_t* le = &free_area.free_list; + while ( (le = list_next(le)) != &free_area.free_list ) { + struct page* p = le2page(le, page_link); + printf ( "%d\n", p->test ); + } + return 0; +} +#endif + +//ex3 +#if 0 +#include +#include "list.h" + + +int main(int argc, const char * argv[]) +{ + list_entry_t HS0,HS1,HS2,HS3; + + list_init(&HS0); + printf("%d %d %d\n",HS0.prev, HS0.next, &HS0); + + HS0.prev = NULL; + HS0.next = &HS1; + HS1.prev = &HS0; + HS1.next = &HS2; + HS2.prev = &HS1; + HS2.next = NULL; + + list_add(&HS1, &HS3); + + printf("%d %d %d\n",HS0.prev, HS0.next, &HS0); + printf("%d %d %d\n",HS1.prev, HS1.next, &HS1); + printf("%d %d %d\n",HS3.prev, HS3.next, &HS3); + printf("%d %d %d\n",HS2.prev, HS2.next, &HS2); + + + list_del(&HS3); + printf("%d %d %d\n",HS0.prev, HS0.next, &HS0); + printf("%d %d %d\n",HS1.prev, HS1.next, &HS1); + printf("%d %d %d\n",HS2.prev, HS2.next, &HS2); +} +#endif + +//ex4 +#if 0 +//一个简单的随机生成一个长度的链表并计算长度的程序 +#include +#include +#include +#include +#include "list.h" + +using namespace std; + +void randlength( list_entry_t *&elm ) +{ + list_init(elm); + int countt = rand()%100; + + printf("the length of the list that will be created: %d\n", countt ); + + for( int i = 0; i < countt; i++ ) + { + list_entry_t * node = new list_entry_t(); + list_add_after(elm, node); + } +} + +int getlength( list_entry_t *&elm ) +{ + int countt = 0; + list_entry_t * current_node = elm; + while( current_node->next!=elm ) + { + countt ++; + current_node = current_node->next; + } + return countt; +} + +int main() +{ + srand( (unsigned)time(NULL)); + list_entry_t * root = new list_entry_t(); + randlength( root ); + printf(" the length of this list is %d", getlength(root) ); + + return 0; +} +#endif