From ff104a942ce7240b7ee39deb8e58958f7afd3e30 Mon Sep 17 00:00:00 2001 From: yuchen Date: Sat, 7 Mar 2015 00:44:03 +0800 Subject: [PATCH] update lab0-ex3.c lab0-ex4.c --- related_info/lab0/lab0_ex3.c | 44 +++++++++++++ related_info/lab0/lab0_ex4.c | 116 +++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) diff --git a/related_info/lab0/lab0_ex3.c b/related_info/lab0/lab0_ex3.c index 4d2ce7e..d6e6c28 100644 --- a/related_info/lab0/lab0_ex3.c +++ b/related_info/lab0/lab0_ex3.c @@ -114,3 +114,47 @@ int main() return 0; } #endif + +//ex2 +#if 0 +#include "stdlib.h" + +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 +}; + +typedef struct gatedesc gatedesc; +typedef unsigned int uint32_t; + +#define STS_IG32 0xE // 32-bit Interrupt Gate +#define STS_TG32 0xF // 32-bit Trap Gate + +#define SETGATE(gate, istrap, sel, off, dpl) { \ + ((gatedesc*)(&gate))->gd_off_15_0 = (uint32_t)(off) & 0xffff; \ + ((gatedesc*)(&gate))->gd_ss = (sel); \ + ((gatedesc*)(&gate))->gd_args = 0; \ + ((gatedesc*)(&gate))->gd_rsv1 = 0; \ + ((gatedesc*)(&gate))->gd_type = (istrap) ? STS_TG32 : STS_IG32; \ + ((gatedesc*)(&gate))->gd_s = 0; \ + ((gatedesc*)(&gate))->gd_dpl = (dpl); \ + ((gatedesc*)(&gate))->gd_p = 1; \ + ((gatedesc*)(&gate))->gd_off_31_16 = (uint32_t)(off) >> 16; \ +} + +int main() +{ + unsigned intr; + intr=8; + SETGATE(intr, 0,1,2,3); + printf("%d", intr); + return 0; +} +#endif diff --git a/related_info/lab0/lab0_ex4.c b/related_info/lab0/lab0_ex4.c index 6909260..93e2b31 100644 --- a/related_info/lab0/lab0_ex4.c +++ b/related_info/lab0/lab0_ex4.c @@ -168,3 +168,119 @@ int main() return 0; } #endif + + +//ex5 +#if 0 +// compile with -nostdinc and explicitly provide header file directories +#include "list.h" +#include + +struct MyDataType { + list_entry_t list; + int32_t data; +}; + +struct MyDataType x, y, z; + +void display() { + printf("x = %lx prev = %lx next = %lx \n", &x.list, x.list.prev, x.list.next); + printf("y = %lx prev = %lx next = %lx \n", &y.list, y.list.prev, y.list.next); + printf("z = %lx prev = %lx next = %lx \n", &z.list, z.list.prev, z.list.next); + printf("----------------------------------\n"); +} + +int main() { + // initialize + list_init(&x.list); + list_init(&y.list); + list_init(&z.list); + + display(); + + // insert element + list_add(&x.list, &y.list); + + display(); + + list_add_before(&x.list, &z.list); + + display(); + + // delete element + list_del_init(&x.list); + + display(); + + return 0; +} +#endif + +//ex6 +#if 0 +#include +#include + +int main() { + struct list_entry first, second, third; + list_init(&first); + list_init(&second); + list_init(&third); + printf("Is empty:%d\n", list_empty(&first)); + list_add_after(&first, &second); + printf("Is empty:%d\n", list_empty(&first)); + list_add_before(&first, &third); + struct list_entry *temp = &first; + int num = 0; + while ((temp = list_prev(temp)) != &first) + num++; + printf("Total elem:%d\n", num); + list_del_init(&second); + list_del_init(&first); + printf("Is empty:%d\n", list_empty(&third)); + return 0; +} +#endif + +//ex7 +#if 0 +#include +#include + +struct Ints { + int data; + list_entry_t le; +}; + +#define le2struct(ptr) to_struct((ptr), struct Ints, le) +#define to_struct(ptr, type, member) \ + ((type *)((char *)(ptr) - offsetof(type, member))) +#define offsetof(type, member) \ + ((size_t)(&((type *)0)->member)) + +int main() { + struct Ints one, two, three, *now_int; + list_entry_t *now; + one.data = 1; + two.data = 2; + three.data = 3; + list_init(&one.le); + list_add_before(&one.le, &two.le); + list_add_after(&one.le, &three.le); + + now = &two.le; + while (1) { + now_int = le2struct(now); + printf("Current: %d\n", now_int->data); + now = now->next; + if (now == &two.le) + break; + } + + return 0; +} +//输出 +//Current: 2 +//Current: 1 +//Current: 3 +#endif