Browse Source

update lab0-ex3.c lab0-ex4.c

main
yuchen 9 years ago
parent
commit
ff104a942c
2 changed files with 160 additions and 0 deletions
  1. +44
    -0
      related_info/lab0/lab0_ex3.c
  2. +116
    -0
      related_info/lab0/lab0_ex4.c

+ 44
- 0
related_info/lab0/lab0_ex3.c View File

@ -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

+ 116
- 0
related_info/lab0/lab0_ex4.c View File

@ -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 <stdio.h>
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 <stdio.h>
#include <list.h>
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 <stdio.h>
#include <list.h>
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

Loading…
Cancel
Save