Переглянути джерело

add more examples in lab0 exercise

main
yuchen 9 роки тому
джерело
коміт
87940c9b0b
2 змінених файлів з 208 додано та 0 видалено
  1. +65
    -0
      related_info/lab0/lab0_ex3.c
  2. +143
    -0
      related_info/lab0/lab0_ex4.c

+ 65
- 0
related_info/lab0/lab0_ex3.c Переглянути файл

@ -49,3 +49,68 @@ main(void)
return 0;
}
// other examples
//ex1
#if 0
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#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

+ 143
- 0
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 <stdio.h>
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 <stdio.h>
#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 <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#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

Завантаження…
Відмінити
Зберегти