■ARM 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
int main(void)
{
int a;
int b;
int c;
int i;
int result;
int start,end,sum,max;
 
    HOW_TO_RETURN(); 
 
 
 
#if 0    
    a=9;b=11/* a < b condition */
    result = CONDITIONAL_EXECUTE(a,b);
 
    a=11;b=10/* a < b condition */
    result = CONDITIONAL_EXECUTE(a,b);
 
    a=10;b=10/* a < b condition */
    result = CONDITIONAL_EXECUTE(a,b);
#endif
 
 
#if 0
    a=11;b=22;c=30;
    result = DATA_PROCESS1(a,b,c);
#endif
 
#if 0
    a=0x10; b=0x33;
    result = DATA_PROCESS2(a,b);
#endif
 
#if 0
    start =1;end =5;
    sum = SUM_OF_DEC(start,end);
#endif
 
 
#if 1
    start =0;end =1;
    sum = Fibonacci(start,end);
#endif
 
 
return i;
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 
                        /* 프로그램 시작 */
    .text              /* 프로그램 정의 */
    .arm               /* ARM 프로세서 */
    .global HOW_TO_RETURN
    .global CONDITIONAL_EXECUTE
    .global DATA_PROCESS1
    .global DATA_PROCESS2
    .global SUM_OF_DEC
    .global Fibonacci
 
 
RESET:  B      MAIN        /* MAIN으로 점프 */
        /* 메인 프로그램 */
        .org    0x100
MAIN:   MOV     R0,#2          /* R0 <- 2 */
        MOV     R1,R0          /* R1 <- R0 */
    b main
 
 
HOW_TO_RETURN:
   mov r0,#0x00000010
   mov pc,lr
 
CONDITIONAL_EXECUTE:
    cmp r0,r1
    movgt r0,#1    
    movlt r0,#2
    moveq r0,#3    
    mov pc,lr
DATA_PROCESS1:
    add r0,r0,r1
    sub r0,r0,r2
    mov pc,lr 
DATA_PROCESS2:
    and r3,r1,#15
    orr r3,r3,r0,LSL#2
    mov r3,r0
    mov pc,lr 
 
SUM_OF_DEC:
    mov r3,#0
FOR:
    add r3,r3,r0
    add r0,r0,#1
    cmp r0,r1
    ble FOR
    mov r0,r3
    mov pc,lr
 
Fibonacci:
 
Fibonacci1:
    add r2,r0,r1
    mov r0,r1
    mov r1,r2
b Fibonacci1
    
    mov pc,lr
 
.end
cs




#피보나치 결과




#BITREE : TREE의 NODE CHILDE가 2개로 이루어져 2진트리라고한다.


이때 한쪽으로 치우쳐진것을 사향트리라고 하는데 밸런스에 맞게 조정한것을 BSTREE라고 한다.



BiTree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include<stdio.h>
 
#include"bitree.h"
 
 
int tree_menu(void) {
 
    int i;
 
    do {
 
        printf("\n<<<Tree Menu>>>>\n");
 
        printf("1. Insert node\n");
 
        printf("2. Print_Preorder\n");
 
        printf("3. Print_Inorder\n");
        
        printf("4. Print_Postorder\n");
        
        printf("5. example - 20, 9, 53, 5, 15, 11, 79\n");
        
        printf("0. Quit\n");
 
        printf("Input Operation : ");
 
        scanf("%d"&i);
 
    } while(i<0 || i>10);
 
    return i;
 
}
 
BiTreeNode * insert_tree(BiTree *tree, BiTreeNode *node, int data)
{
 
    if (bitree_is_eob(node))
    {
        node = (BiTreeNode *)malloc(sizeof(BiTreeNode));
         bitree_left(node)=NULL;
        bitree_right(node)=NULL;
           bitree_data(node) = (void *)data;
            return node;
    }
    
    else
    {
        
    if(bitree_data(node)>data)
        bitree_left(node) = insert_tree(tree,bitree_left(node),data);
 
    else
        bitree_right(node) = insert_tree(tree,bitree_right(node),data);    
    }
     return node;
}
 
void print_preorder(BiTreeNode *node)
{
    if(bitree_is_eob(node))
        return;
 
    printf("%d ", bitree_data(node));
    print_preorder(bitree_left(node));
    print_preorder(bitree_right(node));
}
 
void print_inorder(BiTreeNode *node)
{
    if(bitree_is_eob(node))
        return;
        
    print_inorder(bitree_left(node));
    printf("%d ", bitree_data(node));
    print_inorder(bitree_right(node));
}
void print_postorder(BiTreeNode *node)
{
    if(bitree_is_eob(node))
        return;
 
    print_postorder(bitree_left(node));
    print_postorder(bitree_right(node));
    printf("%d ", bitree_data(node));
}
 
 
void bitree_init(BiTree *tree, void (*destroy)(void *data))
{
    
tree->size = 0;
 
tree->destroy = destroy;
 
tree->root =NULL;
 
return;
    
}
 
int main()
{
    int i;
    BiTree tree;
    bitree_init(&tree,free);
    
    BiTreeNode *root = tree.root;
    
    
    int data;
    
    
        while((i=tree_menu())!=0) {
        switch(i) {
 
            case 1 :
                    printf(">Input data : ");
                    scanf("%d",&data);
                    if(root = insert_tree(&tree,root,data))
                    tree.size++;
            break;
            case 2:
                printf("\nPreeorder>>");
                    print_preorder(root);
                    printf("\n");
                break;
            case 3:
                printf("\ninorder>>");
                    print_inorder(root);
                printf("\n");
                break;
            case 4:
                printf("\npostorder>>");
                    print_postorder(root);
                printf("\n");
                break;
            case 5
                root = insert_tree(&tree,root,20);
                root = insert_tree(&tree,root,9);
                root = insert_tree(&tree,root,53);
                root = insert_tree(&tree,root,5);
                root = insert_tree(&tree,root,15);
                root = insert_tree(&tree,root,11);
                root = insert_tree(&tree,root,79);
                printf("\nPreeorder>>");
                    print_preorder(root);
                    printf("\n");
                    printf("\ninorder>>");
                    print_inorder(root);
                printf("\n");
                    printf("\npostorder>>");
                    print_postorder(root);
                printf("\n");
            break;
        }
    }
    
    system("pause");
    return 0;
}
cs



BsTree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include<stdio.h>
 
#include"bitree.h"
 
int in_srt_array[100];
int bal_idx=0;
 
int tree_menu(void) {
 
    int i;
 
    do {
 
        printf("\n<<<Tree Menu>>>>\n");
 
        printf("1. Insert node\n");
 
        printf("2. Print_Preorder\n");
 
        printf("3. Print_Inorder\n");
        
        printf("4. Print_Postorder\n");
        
        printf("5. Print_Balance tree\n");
        
        printf("0. Quit\n");
 
        printf("Input Operation : ");
 
        scanf("%d"&i);
 
    } while(i<0 || i>10);
 
    return i;
 
}
 
BiTreeNode * insert_tree(BiTree *tree, BiTreeNode *node, int data)
{
    in_srt_array[tree->size]=data;
    
    if (bitree_is_eob(node))
    {
        node = (BiTreeNode *)malloc(sizeof(BiTreeNode));
         bitree_left(node)=NULL;
        bitree_right(node)=NULL;
           bitree_data(node) = (void *)data;
            return node;
    }
    
    else
    {    
    if(bitree_data(node)>data)
        bitree_left(node) = insert_tree(tree,bitree_left(node),data);
 
    else
        bitree_right(node) = insert_tree(tree,bitree_right(node),data);    
    }
 
     return node;
}
 
 
 
 
 
void print_preorder(BiTreeNode *node)
{
 
    if(bitree_is_eob(node))
        return;
    printf("%d ", bitree_data(node));
    print_preorder(bitree_left(node));
    print_preorder(bitree_right(node));
    
}
 
void print_inorder(BiTreeNode *node)
{
    if(bitree_is_eob(node))
        return;
        
    print_inorder(bitree_left(node));
    printf("%d ", bitree_data(node));
    print_inorder(bitree_right(node));
}
void print_postorder(BiTreeNode *node)
{
    if(bitree_is_eob(node))
        return;
 
    print_postorder(bitree_left(node));
    print_postorder(bitree_right(node));
    printf("%d ", bitree_data(node));
}
 
 
 
void bitree_init(BiTree *tree, void (*destroy)(void *data))
{
    
tree->size = 0;
 
tree->destroy = destroy;
 
tree->root =NULL;
 
return;
    
}
 
BiTreeNode * balance(BiTree *tree,int n)
{
    int ln, rn;
    BiTreeNode *r;
    
    if(n>0)
    {
        ln=(n-1)/2;
        rn=n-ln-1;
        r = (BiTreeNode *)malloc(sizeof(BiTreeNode));
        r->left = balance(&tree,ln);
        r->data=in_srt_array[(bal_idx++)];
        r->right = balance(&tree,rn);
        return r;
    }
 
    return NULL;
}
int main()
{
    int i;
    BiTree tree;
    BiTree tree2;
    bitree_init(&tree,free);
    bitree_init(&tree2,free);
    
    BiTreeNode *root = tree.root;    
    
    int data;
    
    
        while((i=tree_menu())!=0) {
        switch(i) {
 
            case 1 :
                    printf(">Input data : ");
                    scanf("%d",&data);
                    if(root = insert_tree(&tree,root,data))
                    tree.size++;
            break;
            case 2:
                printf("\nPreeorder>>");
                    print_preorder(root);
                    printf("\n");
                break;
            case 3:
                printf("\ninorder>>");
                    print_inorder(root);
                printf("\n");
                break;
            case 4:
                printf("\npostorder>>");
                    print_postorder(root);
                printf("\n");
                break;
 
            
            case 5:
 
            tree2.root=balance(&tree2,tree.size);
            print_preorder(tree2.root);
            bal_idx=0;
            
            break;
 
        }
    }
    
    system("pause");
    return 0;
}
cs


'자료구조' 카테고리의 다른 글

[자료구조_Day4]Chained Hash Tables  (0) 2018.11.06
[자료구조_Day3]Stack  (0) 2018.11.06
[자료구조_Day3]Set  (0) 2018.11.06
[자료구조_Day2]Double Circular Linked List  (0) 2018.10.28
[자료구조_Day1]Double Linked List  (0) 2018.10.28

Chained Hash Tables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <stdio.h>
#include <stdlib.h>
#include"list.h"
#include<string.h>
#include<stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
List * make_bucket(int *size);
void find_node(List *list,int size);
void auto_data(List* list,int size);
int list_menu(void) {
 
    int i;
 
    do {
 
        printf("\n\nLinked List Menu\n");
 
        printf("1. Insert node\n");
 
        printf("2. Display all node\n");
        
        printf("3. find node\n");
 
        printf("4. auto data\n");
    
        printf("0. Quit\n");
 
        printf("Input Operation : ");
 
        scanf("%d"&i);
 
    } while(i<0 || i>10);
 
    return i;
 
}
 
void start(List* hash,int size) {
 
    int i;
 
    void *data;
 
 
    while((i=list_menu())!=0) {
 
        switch(i) {
 
 
            case 1 :
                isert_node(hash,size);
            break;
 
            case 2 :
                for(i=0;i<size;i++)
                {
                    printf("\n>>hash[%d]>> ",i);
                    if((hash+i)->head==NULL)
                    {
                        printf("- empty",i);
                        continue;
                    }
                
                    print_list(hash+i);    
                }
            break;
            case 3 :
                find_node(hash,size);
            break;
            case 4:
                auto_data(hash,size);
                break;
        }
 
    }
    exit(0);
 
}
 
 
void print_list(List *list) {
    
    ListElmt *element;
    element=list_head(list);
 
    while(element){
        
        printf("%s  ",list_data(element));
 
        element=element->next;
    }
}
 
void main(void) {
 
 
    List *hash;
    int size=0;
    
    hash=make_bucket(&size);
    start(hash,size);
 
}
List * make_bucket(int *size)
{
    int i;
    printf("#버킷 크기 입력 :");
    fflush(stdin);
    scanf("%d",size);
    
    List * n_hash=(List *)malloc(sizeof(List)*(*size));
    
    for(i=0;i<*size;i++)
    {
        list_init(n_hash+i,free);
    }
    printf("\n#Bucket[%d]\n",*size);
    return n_hash;
    
}
 
void isert_node(List* list,int size)
{
    char temp[100];
    char * str;
 
        printf(">Input data : ");
        fflush(stdin);
        gets(temp); 
        str = (char *)malloc(sizeof(temp)+1);
        strcpy(str,temp);
    
    
    int num = rand()%size;
    
    if(list[num].tail==NULL)
    {
        if(list_ins_next(list+num, NULL, str)==-1)
        {
            printf("no data\n");
            return;
        }
    }
    else
    {
            if(list_ins_next(list+num, list[num].tail, str)==-1)
        {
            printf("no data\n");
            return;
        }
    }
    return;    
}
 
void auto_data(List* list,int size)
{
    char temp[100];
    char * str;
    int input,i;
    int num;
        printf(">Input data 개수 : ");
        fflush(stdin);
        scanf("%d",&input);
 
 
        for(i=0;i<input;i++)
        { 
            str = (char *)malloc(sizeof(temp)+1);
            strcpy(str,itoa(rand()%1000,temp,10));
        
        num = atoi(str)%size;
 
    
    if(list[num].tail==NULL)
    {
        if(list_ins_next(list+num, NULL, str)==-1)
        {
            printf("no data\n");
            return;
        }
    }
    else
    {
            if(list_ins_next(list+num, list[num].tail, str)==-1)
        {
            printf("no data\n");
            return;
        }
    }
}
    return;    
}
 
void find_node(List *list,int size){
    int key,count=1;
    
    char temp[100];
    char * str;
 
        printf(">Input data : ");
        fflush(stdin);
        gets(temp); 
        str = (char *)malloc(sizeof(temp)+1);
        strcpy(str,temp);
        
    key=str[0]%size;
    ListElmt *element=list[key].head;
    
    while(1){
        if(!strcmp((char*)element->data,str)){
            printf("# data는 hash[%d]의 %d번째 노드에 있습니다.\n\n",key,count);
            return ;    
        }
        if(element->next==NULL){
            printf("# not find...\n");
            return ;
        }
        element=list_next(element);
        count++;
    }
}
 
cs


'자료구조' 카테고리의 다른 글

[자료구조_Day5]biTree,bsTree  (0) 2018.11.06
[자료구조_Day3]Stack  (0) 2018.11.06
[자료구조_Day3]Set  (0) 2018.11.06
[자료구조_Day2]Double Circular Linked List  (0) 2018.10.28
[자료구조_Day1]Double Linked List  (0) 2018.10.28

■Stack■

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*****************************************************************************
*                                                                            *
*  ------------------------------- stack.c --------------------------------  *
*                                                                            *
*****************************************************************************/
 
#include <stdlib.h>
#include<stdio.h>
#include<string.h>
 
#include "list.h"
#include "stack.h"
 
/*****************************************************************************
*                                                                            *
*  ------------------------------ stack_push ------------------------------  *
*                                                                            *
*****************************************************************************/
 
int stack_push(Stack *stackconst void *data) {
 
/*****************************************************************************
*                                                                            *
*  Push the data onto the stack.                                             *
*                                                                            *
*****************************************************************************/
 
 
return list_ins_next(stackNULL, data);
 
}
 
/*****************************************************************************
*                                                                            *
*  ------------------------------ stack_pop -------------------------------  *
*                                                                            *
*****************************************************************************/
 
int stack_pop(Stack *stackvoid **data) {
 
/*****************************************************************************
*                                                                            *
*  Pop the data off the stack.                                               *
*                                                                            *
*****************************************************************************/
 
return list_rem_next(stackNULL, data);
 
}
 
 
 
void main(void) {
 
 
    Stack stack;
    stack_init(&stackfree);
    
    
    start(&stack);
 
}
 
int list_menu(void) {
 
    int i;
 
    do {
 
        printf("\nStackt Menu\n");
 
        printf("1. stack push\n");
 
        printf("2. stack pop\n");
        
        printf("3. Display stack\n");
 
        printf("0. Quit\n");
 
        printf("Input Operation : ");
 
        scanf("%d"&i);
 
    } while(i<0 || i>10);
 
    return i;
 
}
 
 
void start(Stack* stack) {
 
    int i;
 
    void *data;
    char temp[100];
    char * str,*str2;
 
 
    while((i=list_menu())!=0) {
 
        switch(i) {
 
 
            case 1 :
 
            printf(">Input data : ");
            fflush(stdin);
            gets(temp); 
            str = (char *)malloc(sizeof(temp)+1);
            strcpy(str,temp);
            stack_push(&stack, str);
            break;
            
            case 2 :
 
            if(list_rem_next(&stackNULL, (void **)&data)!=0)
 
            printf("The data is not exist\n");
 
            break;
            
            case 3 :
 
                print_stack(&stack);  
 
                break;
 
        }
 
    }
 
 
    exit(0);
 
}
 
void print_stack(Stack *stack)
{
    void *data;
    char temp[100];
    
    ListElmt *ST;
 
    ST=stack->head;
    
    
    if(ST==NULL) {
    printf("There is not data\n");
    return;
    }
        
    while(1) {
        
            strcpy(temp,(char *)(ST->data));
            
            if(is_check(temp))
            printf("%s    ", (char *)(ST->data) );
            else
            printf("%d    ",atoi((char *)(ST->data) ));
            
        if(ST->next==NULL)
            break;
 
        else
            ST=(ST->next);
    }
 
}
int is_check(char* temp)
{
    int i;
    for(i=0;i<strlen(temp);i++)
            if(temp[i]<'0' ||temp[i]>'9')
            {
                return 1;
            }
        return 0;
}
cs


'자료구조' 카테고리의 다른 글

[자료구조_Day5]biTree,bsTree  (0) 2018.11.06
[자료구조_Day4]Chained Hash Tables  (0) 2018.11.06
[자료구조_Day3]Set  (0) 2018.11.06
[자료구조_Day2]Double Circular Linked List  (0) 2018.10.28
[자료구조_Day1]Double Linked List  (0) 2018.10.28
■SET■

# 합집합, 차집합, 교집합을 링크리스트를 이용하여 구현

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <stdio.h>
#include <stdlib.h>
#include"list.h"
#include<string.h>
 
 
void print_list(List *list);
void doUnion(List* listA,List* listB);
void deDuplicate(List *list);
void doIntersection(List* listA,List* listB);
void doDiffrence(List* listA,List* listB);
int find_list(List *list,void *data);
 
int list_menu(void) {
 
    int i;
 
    do {
 
        printf("\n=======Menu=======\n");
 
        printf("1. Insert nodeA\n");
 
        printf("2. Insert nodeB\n");
        
        printf("3. print nodeA\n");
        
        printf("4. print nodeB\n");
        
        printf("5. doUnion\n");
            
        printf("6. doIntersection\n");
                
        printf("7. doDiffrence\n");
            
        printf("0. Quit\n");
 
        printf("Input Operation : ");
 
        scanf("%d"&i);
 
    } while(i<0 || i>8);
 
    return i;
 
}
 
void start(List* listA,List* listB) {
 
    int i;
 
    void *data;
    char temp[100];
    char * str;
 
 
    while((i=list_menu())!=0) {
 
        switch(i) {
 
 
            case 1 :
 
            printf(">Input data : ");
            fflush(stdin);
            gets(temp); 
            str = (char *)malloc(sizeof(temp)+1);
            strcpy(str,temp);
            list_ins_next(listA, NULL, str);
 
            break;
            
            case 2 :
 
            printf(">Input data : ");
            fflush(stdin);
            gets(temp); 
            str = (char *)malloc(sizeof(temp)+1);
            strcpy(str,temp);
            list_ins_next(listB, NULL, str);
 
            break;
 
            case 3 :
 
                print_list(listA);  
 
            break;
            
            case 4 :
 
                print_list(listB);  
                
            break;
            
            case 5:
                doUnion(listA,listB);
 
            break;
            
            case 6:
                
                doIntersection(listA,listB);
 
            break;
            
            case 7:
                
                doDiffrence(listA,listB);
 
            break;
 
        }
 
    }
 
    exit(0);
 
}
 
 
int is_check(char* temp)
{
    int i;
    for(i=0;i<strlen(temp);i++)
            if(temp[i]<'0' ||temp[i]>'9')
            {
                return 1;
            }
        return 0;
}
 
 
void print_list(List *list)
{
    void *data;
    char temp[100];
    
    ListElmt *element;
 
    element=list->head;
    
    
    if(element==NULL) {
    printf("There is not data\n");
    return;
    }
    
 
    while(1) {
        
            strcpy(temp,(char *)(element->data));
            
            if(is_check(temp))
            printf("%s    ", (char *)(element->data) );
            else
            printf("%d    ",atoi((char *)(element->data) ));
            
        if(list_is_tail(element))
            break;
 
        else
            element=list_next(element);
    }
 
}
 
 
void doUnion(List* listA,List* listB)
{
    List listC;
    list_init(&listC, free);
    
    char tempA[100],tempB[100];
    
    ListElmt *elementA,*elementB;
 
    elementA=listA->head;
    elementB=listB->head;
 
        
    if(elementA==NULL) {
    printf("A is not data\n");
    return;
    }
    if(elementB==NULL) {
    printf("B is not data\n");
    return;
    }
    
    while(1) {
        
        strcpy(tempA,(char *)(elementA->data));
            
        if(!find_list(&listC,tempA)||(find_list(&listC,tempA)==-1))
            list_ins_next(&listC, NULL, elementA->data);
            
        while(1) {
        
        strcpy(tempB,(char *)(elementB->data));
                
        if(strcmp(tempA,tempB))
        if(!find_list(&listC,tempB))
        list_ins_next(&listC, NULL, elementB->data);
 
 
        if(list_is_tail(elementB))
            break;
 
        else
            elementB=list_next(elementB);
    }
            
            elementB=listB->head;
            
        if(list_is_tail(elementA))
            break;
 
        else
            elementA=list_next(elementA);
    }
        printf("\nUnion >> ");
        print_list(&listC);
        printf("\n");
}
 
void doIntersection(List* listA,List* listB)
{
    List listC;
    list_init(&listC, free);
    
    char tempA[100],tempB[100];
 
    ListElmt *elementA,*elementB;
 
    elementA=listA->head;
    elementB=listB->head;
 
        
    if(elementA==NULL) {
    printf("A is not data\n");
    return;
    }
    if(elementB==NULL) {
    printf("B is not data\n");
    return;
    }
 
    
    while(1) {
        
            strcpy(tempA,(char *)(elementA->data));
            
            
 
        while(1) {
        
            strcpy(tempB,(char *)(elementB->data));
                
            if(!strcmp(tempA,tempB))
            if(!find_list(&listC,tempB)||(find_list(&listC,tempB)==-1))
            list_ins_next(&listC, NULL, elementB->data);
 
        if(list_is_tail(elementB))
            break;
 
        else
            elementB=list_next(elementB);
    }
            
            elementB=listB->head;
            
        if(list_is_tail(elementA))
            break;
 
        else
            elementA=list_next(elementA);
    }
        
        printf("\nIntersection >> ");
        print_list(&listC);
        printf("\n");
}
 
void doDiffrence(List* listA,List* listB)
{
    List listC;
    list_init(&listC, free);
    
    int flag=0;
    char tempA[100],tempB[100];
    
    ListElmt *elementA,*elementB;
 
    elementA=listA->head;
    elementB=listB->head;
 
        
    if(elementA==NULL) {
    printf("A is not data\n");
    return;
    }
    if(elementB==NULL) {
    printf("B is not data\n");
    return;
    }
 
    
    while(1) {
        
        strcpy(tempA,(char *)(elementA->data));
                    
        while(1) {
        
            strcpy(tempB,(char *)(elementB->data));
                
        if(!strcmp(tempA,tempB))
            {
            flag=1;
            break;
            }
 
        if(list_is_tail(elementB))
            break;
 
        else
            elementB=list_next(elementB);
    }
    
    if(flag==0)
        if(!find_list(&listC,tempA)||(find_list(&listC,tempA)==-1))
            list_ins_next(&listC, NULL, elementA->data);            
    flag=0;    
    
        elementB=listB->head;
            
        if(list_is_tail(elementA))
            break;
 
        else
            elementA=list_next(elementA);
    }
        
        printf("\nDiffrence >> ");
        print_list(&listC);
        printf("\n");
}
 
 
int find_list(List *list,void *data){//있을시 return 1, 없을시 return 0 
 
    char temp[100];
 
    ListElmt *element;
 
    element=list->head;
    
    
    if(element==NULL)
    return -1;
    
    while(1) {
 
    strcpy(temp,(char *)(data));
    
    if(is_check(temp))
    {
 
    if(strcmp((char *)(data),(char *)(element->data))==0 )
    {
    return 1;
    }
    
    else
    {
        if(list_is_tail(element))
        {
            return 0;
        }
    element=list_next(element);
    }
}
    else
    
    {
 
    if(atoi((char *)(data))==atoi((char *)(element->data)) )
    {
 
    return 1;
    }
    
    else
    {
        if(list_is_tail(element))
        {
 
            return 0;
        }
    element=list_next(element);
    }
}
    
    }
 
 }
int main(void) {
 
 
    List listA,listB;
    list_init(&listA, free);
    list_init(&listB, free);
 
    start(&listA,&listB);
    
    system("pause");
 
}
 
cs


'자료구조' 카테고리의 다른 글

[자료구조_Day5]biTree,bsTree  (0) 2018.11.06
[자료구조_Day4]Chained Hash Tables  (0) 2018.11.06
[자료구조_Day3]Stack  (0) 2018.11.06
[자료구조_Day2]Double Circular Linked List  (0) 2018.10.28
[자료구조_Day1]Double Linked List  (0) 2018.10.28



■ARM Operaing Mode



#Mode bits

-ARM의 7가지의 동작모드


#Exception Vector Table 




.text "지금부터 명령어를 실행할것이다" 라는 지시어 (지시어는 용량을 차지 하지 않는다)
* RESET,MAIN ..등등 을 label이라고 한다.

* swi가면 무조건 Supervisor 모드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
         /* 프로그램 시작 */
        .text              /* 프로그램 정의 */    
        .arm               /* ARM 프로세서 */    
 
RESET:  B    MAIN        /* MAIN으로 점프 */
 
        B    Undefined_Instruction 
        B    Software_Interrupt
        B    Prefetch_Abort
        B    Data_Abort
        B    Reserved
        B    IRQ
        B    FIQ
 
    /* 메인 프로그램 */
        .org    0x100
MAIN:   MOV     R0,#2          /* R0 &lt;- 2 */
        MOV     R1,R0          /* R1 &lt;- R0 */
                    
        mov     r0, #3  /* fiq */
        swi     0x08    /*cpsr_c = r0*/
Software_Interrupt :
    mov     r0, #0x00000013  
    msr     cpsr_c, r0
Undefined_Instruction : 
    mov     r0, #0x0000001b  
    msr     cpsr_c, r0
Prefetch_Abort : 
    mov     r0, #0x00000017  
    msr     cpsr_c, r0
Data_Abort : 
    mov     r0, #0x00000017  
    msr     cpsr_c, r0
Reserved :
IRQ : 
    mov     r0, #0x00000012  
    msr     cpsr_c, r0
FIQ : 
    mov     r0, #0x00000011  
    msr     cpsr_c, r0
    .end
cs

#test#
swi 명령어를 통해서 디버깅을 하도록 코드를 만들었습니다.
각각의 mode bits를 PSR의 control bits에 넣었을 때 mode가 변경되는 것을 확인할 수 있습니다.


■임베디드 시스템의 정의■


특정한 기능을 수행하도록 설계된 내장형 컴퓨팅 시스템.


======H/W===========S/W===========

=                                                                =

=    프로세서                    응용 소프트웨어        =

=                                                                =

=    메모리 장치        +      시스템 소프트웨어     =   시스템 소프트웨어 : 함수를 쓸때 함수원형이 OS커널안에 있다.

=                                                                =

=    입출력 장치                OS 커널                  =

=                                                                =

==================================

#MCU(=MPU)


CPU와 각종 Peripheral Controller들의 결합(=>프로세서를 내장하고 있는 SoC를 말한다.)


SoC(System on Chip) : 여러 개의 반도체 부품이 하나로 집적되느 기술 및 제품, 단일 칩으로 구현. 


<구성>

1. CPU Core : ALU, CU, Register로만 구성된 CPU의 핵심

2. ALU(Arithmetic and Logical Unit) : 산술/논리 연산을 위한 장치

3. CU(Control Unit) : 기계어를 분석하고 실행하기 위해 제어 신호를 순차적으로 발생시킴

4. Register : 연산이나 기타 목적으로 사용될 저장 공간, 메모리 계층에서 가장 빠르다(Flip-Flop으로 구성)

(=>1.제어 2.임시 기억 장소)<<범용 레지스터 :GFR, 제어용 레지스터 : CPSR, 상태 레지스터 : Status>>

5. Cache : 프로세서가 최근에 액세스한 메모리의 내용을 보관, 재요철시 메모리 액세스 없이 전달

6. MMU(Memory Management Unit) : 운영체제를 위해 가상메모리 지원

(=>Virtual address와 Physical address를 연결시켜주기 위해서 매핑을 해주는 역할을 한다.)

7.Bus : 1. 공유 2. 공통된 선들의 집합


ARM Processor에서 개발환경을 구성하기 힘들기 때문에 교차 개발을 한다.

교차 개발이란 Host System(PC)에서 개발환경을 구성하고 타켓(ARM Processor) 시스템 전용의 실행 파일을 생성하여 타켓 시스템이

다운로드한다. PC에서 크로스 툴체인을 이용한다.


#컴파일

<컴파일 단계>

전처리 -> 컴파일 -> 어셈블 -> 링크 과정을 거쳐 최종적으로 실행 이미지 생성.


1.전처리

컴파일 전에 전처리기(Preprocessor)에 의해 수행된다.

헤더 내용(.h)과 define 매크로/상수를 소스파일(.c)에 복사하는 작업.

.i 형태로 임시 저장되었다가 삭제된(-E option 컴파일 전에 멈춤)


2.컴파일

C코드를 해당 Machine의 어셈블리로 번역, 코드의 에러 경고 발생, .i -> .S


3.어셈블

어셈블리 코드를 Binary형태의 기계어로 변환하는 작업

어셈블러를 통과시킨 결과물을 Object File이라고 한다.

.S ->.o

<어셈블러가 어셈블리어를 어셈블한다.>


4.링크

오브젝트 파일 단독으로는 실행될 수 없어서 여러 개의 오브젝트 파일들과 라이브러리 파일들의 모든 코드롸 데이터를 포함하여 새로운 오브젝트 생성하는데 Section 단위로 묶여져 1개의 실행 파일(.exe)이 생성된다. 




#메모리 맵

-메모리에 적재되어 실행 가능한 오브젝트 파일


오브젝트 파일은 링커에 의해 해석될 수 있도로 여러가지 섹션들로 구성


대표적 섹션들 : text(code), data(RO),data(RW),bss(ZI),HEAP,STACK .....



1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
int g_a = 20;                //DATA(RW)
int g_b;                    //BSS
const double PI = 3.14;        //DATA(RO)
 
void Func(int x)            //STACK
{
    static int cnt;            //BSS
    static int life = 5;    //DATA(RW)
    int result = -1;        //STACK
}
cs


1. DATA(RW) : read-write 로서 초기값이 있는 전역변수

2. ZI(Zero-initialized) BSS : 초기값이 0인 전역변수

3. DATA(RO) : read-only 로서 수정이 불가능한 const 전역변수 text인 code 의미,(단, const붙은 전역변수도 포함) 








■Uart_Print■

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "2450addr.h"
#include <stdarg.h>
#include "libc.h"
#include "option.h"
 
#define rUTXH1 (*(volatile unsigned *)0x50004020)
#define rURXH1 (*(volatile unsigned *)0x50004024)
#define rUTRSTAT1 (*(volatile unsigned *)0x50004010)
#define rUDIVSLOT1    (*(volatile unsigned *)0x5000402C)
 
// Function Declaration
void Uart_Init(int baud);
void Uart_Printf(char *fmt,...);
void Uart_Send_String(char *pt);
void Uart_Send_Byte(int data);
char Uart_Get_Char();
void Uart_get_String(char* str);
 
extern int vsprintf(char *const char *, va_list);
 
 
void Main()
{
 
    char str[]="";
    //Uart_Init(NULL);
    while(1)
    {
    Uart_get_String(str);
    Uart_Send_String(str);
    }
}
 
 
 
void Uart_Init(int baud)
{
    int pclk;
    pclk = PCLK;
    
    // PORT GPIO initial
    rGPHCON &= ~(0xf<<4);
    rGPHCON |= (0xa<<4);    
 
    
    rUFCON1 = 0x0;
    rUMCON1 = 0x0;
    
    /* TODO : Line Control(Normal mode, No parity, One stop bit, 8bit Word length */
    rULCON1 = 0x03;
 
    /* TODO : Transmit & Receive Mode is polling mode  */
    rUCON1 = 0x05;
 
    /* TODO : Baud rate 설정  */        
    rUBRDIV1=0x22;
    rUDIVSLOT1=0xDFDD;
}
#if 0
void Uart_Printf(char *fmt,...)
{
    va_list ap;
    char string[256];
 
    va_start(ap,fmt);
    vsprintf(string,fmt,ap);
    Uart_Send_String(string);
    va_end(ap);        
}
#endif
void Uart_Send_String(char *pt)
{
    while(*pt)
    {
        /* TODO : 문자 하나씩 증가시키면서 문자열 출력  */
        /*YOUR CODE HERE*/
        Uart_Send_Byte(*pt++);
    }
}
 
void Uart_Send_Byte(int data)
{
    while(!(rUTRSTAT1 & 0x2));//tx버퍼가 비어있으면 출력(while문 끝나지 x)
                        //비어있다 1 -> while(0)
                        //비어있지 않다 0 ->while(1)
    rUTXH1 = (char)data;
    if(data=='\r')
    {
        //while(!(rUTRSTAT1 & 0x2));
        rUTXH1 = '\r';
        //while(!(rUTRSTAT1 & 0x2));
        rUTXH1 = '\n';
    }
    /* TODO : UTRSTAT1의 값을 확인하여 TX 버퍼가 비어있으면 문자 출력   */    
    /*YOUR CODE HERE*/
}
 
char Uart_Get_Char()
{
    /* TODO : UTRSTAT1의 값을 확인하여 문자열 입력   */        
    /*YOUR CODE HERE*/
    
    while(!(rUTRSTAT1 & 0x1));
    return rURXH1;
 
}
 
void Uart_get_String(char* str)
{    
    unsigned char data;
    while(1)
    {
        data = Uart_Get_Char();
        *str++ = data;
        Uart_Send_Byte(data);    
        if(data == '\r')
        {
        break;
        }
    }
    *str = '\0';
}
 
cs

#Uart_Get_Char에서 폴링 방식으로 TX버퍼가 0인지 1인지 검사를 계속한다.


#결과(Tera Term)




+ Recent posts