Commit bba49d9ed929d84140afa5141137a8771a69f660
1 parent
61a62015
--no commit message
Showing
12 changed files
with
251 additions
and
28 deletions
arith/Makefile
1 | -TARGETS = adder substractor multiplier divider negator comparator | |
1 | +TARGETS = adder substractor multiplier divider negator comparator shifter | |
2 | 2 | |
3 | 3 | .PHONY: all clean |
4 | 4 | |
... | ... | @@ -18,4 +18,6 @@ divider: |
18 | 18 | negator: |
19 | 19 | gcc -I../common/ ../common/auxiliar.c negator.c -o negator |
20 | 20 | comparator: |
21 | - gcc -I../common/ ../common/auxiliar.c comparator.c -o comparator | |
22 | 21 | \ No newline at end of file |
22 | + gcc -I../common/ ../common/auxiliar.c comparator.c -o comparator | |
23 | +shifter: | |
24 | + gcc -I../common/ ../common/auxiliar.c shifter.c -o shifter | |
23 | 25 | \ No newline at end of file | ... | ... |
arith/adder.c
... | ... | @@ -14,8 +14,6 @@ int check_length(char **inputs, uint8_t num_inputs) |
14 | 14 | |
15 | 15 | int main(int argc, char **argv) |
16 | 16 | { |
17 | - int i; | |
18 | - int pos; | |
19 | 17 | if(argc != 2) |
20 | 18 | { |
21 | 19 | error(); |
... | ... | @@ -41,9 +39,9 @@ int main(int argc, char **argv) |
41 | 39 | error(); |
42 | 40 | } |
43 | 41 | int a,b,carry_in,c; |
44 | - a = bin2dec(inputs[0]); | |
45 | - b = bin2dec(inputs[1]); | |
46 | - carry_in = bin2dec(inputs[2]); | |
42 | + a = bin2udec(inputs[0]); | |
43 | + b = bin2udec(inputs[1]); | |
44 | + carry_in = bin2udec(inputs[2]); | |
47 | 45 | c = a+b+carry_in; |
48 | 46 | char *result = dec2bin(c,input_length+1); |
49 | 47 | printf("%s,%c",result+1,result[0]); | ... | ... |
arith/bit_adder.c
0 → 100644
1 | +#include <stdio.h> | |
2 | +#include <string.h> | |
3 | +#include "auxiliar.h" | |
4 | + | |
5 | +int main(int argc, char **argv) | |
6 | +{ | |
7 | + if(argc != 2) | |
8 | + { | |
9 | + error(); | |
10 | + } | |
11 | + char *input = argv[1]; | |
12 | + if(!strcmp(input,"error")) | |
13 | + { | |
14 | + error(); | |
15 | + } | |
16 | + char **inputs = comma_separate(input); | |
17 | + if(inputs == NULL) | |
18 | + { | |
19 | + error(); | |
20 | + } | |
21 | + int num_inputs = num_occurrences(input,','); | |
22 | + if(num_inputs != 1) | |
23 | + { | |
24 | + error(); | |
25 | + } | |
26 | + int input_length = strlen(inputs[0]); | |
27 | + int num = bin2dec(inputs[0]); | |
28 | + num *= -1; | |
29 | + char *result = dec2bin(num,input_length); | |
30 | + printf("%s",result); | |
31 | + free(result); | |
32 | + free_mem(inputs,num_inputs); | |
33 | + return 0; | |
34 | +} | ... | ... |
arith/bit_finder.c
0 → 100644
1 | +#include <stdio.h> | |
2 | +#include <string.h> | |
3 | +#include "auxiliar.h" | |
4 | + | |
5 | +int main(int argc, char **argv) | |
6 | +{ | |
7 | + if(argc != 2) | |
8 | + { | |
9 | + error(); | |
10 | + } | |
11 | + char *input = argv[1]; | |
12 | + if(!strcmp(input,"error")) | |
13 | + { | |
14 | + error(); | |
15 | + } | |
16 | + char **inputs = comma_separate(input); | |
17 | + if(inputs == NULL) | |
18 | + { | |
19 | + error(); | |
20 | + } | |
21 | + int num_inputs = num_occurrences(input,','); | |
22 | + if(num_inputs != 1) | |
23 | + { | |
24 | + error(); | |
25 | + } | |
26 | + int input_length = strlen(inputs[0]); | |
27 | + int num = bin2dec(inputs[0]); | |
28 | + num *= -1; | |
29 | + char *result = dec2bin(num,input_length); | |
30 | + printf("%s",result); | |
31 | + free(result); | |
32 | + free_mem(inputs,num_inputs); | |
33 | + return 0; | |
34 | +} | ... | ... |
arith/comparator.c
... | ... | @@ -2,6 +2,9 @@ |
2 | 2 | #include <string.h> |
3 | 3 | #include "auxiliar.h" |
4 | 4 | |
5 | +#define SIGNED 1 | |
6 | +#define UNSIGNED 0 | |
7 | + | |
5 | 8 | int check_length(char **inputs, uint8_t num_inputs) |
6 | 9 | { |
7 | 10 | int input_length = strlen(inputs[0]); |
... | ... | @@ -14,9 +17,8 @@ int check_length(char **inputs, uint8_t num_inputs) |
14 | 17 | |
15 | 18 | int main(int argc, char **argv) |
16 | 19 | { |
17 | - int i; | |
18 | - int pos; | |
19 | - if(argc != 2) | |
20 | + uint8_t signedness; | |
21 | + if(argc != 3) | |
20 | 22 | { |
21 | 23 | error(); |
22 | 24 | } |
... | ... | @@ -25,6 +27,18 @@ int main(int argc, char **argv) |
25 | 27 | { |
26 | 28 | error(); |
27 | 29 | } |
30 | + if(!strcmp("unsigned", argv[2])) | |
31 | + { | |
32 | + signedness = UNSIGNED; | |
33 | + } | |
34 | + else if(!strcmp("signed", argv[2])) | |
35 | + { | |
36 | + signedness = SIGNED; | |
37 | + } | |
38 | + else | |
39 | + { | |
40 | + error(); | |
41 | + } | |
28 | 42 | char **inputs = comma_separate(input); |
29 | 43 | if(inputs == NULL) |
30 | 44 | { |
... | ... | @@ -37,8 +51,16 @@ int main(int argc, char **argv) |
37 | 51 | } |
38 | 52 | int input_length = check_length(inputs,num_inputs); |
39 | 53 | int a,b; |
40 | - a = bin2dec(inputs[0]); | |
41 | - b = bin2dec(inputs[1]); | |
54 | + if(signedness == UNSIGNED) | |
55 | + { | |
56 | + a = bin2udec(inputs[0]); | |
57 | + b = bin2udec(inputs[1]); | |
58 | + } | |
59 | + else if(signedness == SIGNED) | |
60 | + { | |
61 | + a = bin2dec(inputs[0]); | |
62 | + b = bin2dec(inputs[1]); | |
63 | + } | |
42 | 64 | if(a > b) |
43 | 65 | { |
44 | 66 | printf("1,0,0"); | ... | ... |
arith/divider.c
... | ... | @@ -18,8 +18,6 @@ int check_length(char **inputs, uint8_t num_inputs) |
18 | 18 | |
19 | 19 | int main(int argc, char **argv) |
20 | 20 | { |
21 | - int i; | |
22 | - int pos; | |
23 | 21 | if(argc != 2) |
24 | 22 | { |
25 | 23 | error(); |
... | ... | @@ -51,8 +49,8 @@ int main(int argc, char **argv) |
51 | 49 | memset(op,0x00,input_length+1); |
52 | 50 | strcpy(op,upper); |
53 | 51 | strcat(op,lower); |
54 | - a = bin2dec(op); | |
55 | - b = bin2dec(inputs[2]); | |
52 | + a = bin2udec(op); | |
53 | + b = bin2udec(inputs[2]); | |
56 | 54 | c = a/b; |
57 | 55 | rem = a%b; |
58 | 56 | char *result = dec2bin(c,input_length); | ... | ... |
arith/multiplier.c
... | ... | @@ -18,8 +18,6 @@ int check_length(char **inputs, uint8_t num_inputs) |
18 | 18 | |
19 | 19 | int main(int argc, char **argv) |
20 | 20 | { |
21 | - int i; | |
22 | - int pos; | |
23 | 21 | if(argc != 2) |
24 | 22 | { |
25 | 23 | error(); |
... | ... | @@ -45,9 +43,9 @@ int main(int argc, char **argv) |
45 | 43 | error(); |
46 | 44 | } |
47 | 45 | int a,b,carry_in,c; |
48 | - a = bin2dec(inputs[0]); | |
49 | - b = bin2dec(inputs[1]); | |
50 | - carry_in = bin2dec(inputs[2]); | |
46 | + a = bin2udec(inputs[0]); | |
47 | + b = bin2udec(inputs[1]); | |
48 | + carry_in = bin2udec(inputs[2]); | |
51 | 49 | c = (a*b)+carry_in; |
52 | 50 | char *result = dec2bin(c,input_length*2); |
53 | 51 | printf("%s,%.*s",result+input_length,input_length,result); | ... | ... |
arith/negator.c
arith/shifter.c
0 → 100644
1 | +#include <stdio.h> | |
2 | +#include <string.h> | |
3 | +#include "auxiliar.h" | |
4 | + | |
5 | +#define LEFT 0 | |
6 | +#define RIGHT 1 | |
7 | +#define LOGIC 0 | |
8 | +#define ARITH 1 | |
9 | +#define ROTATE 2 | |
10 | + | |
11 | +int main(int argc, char **argv) | |
12 | +{ | |
13 | + uint8_t direction, type; | |
14 | + int shift; | |
15 | + if(argc != 3) | |
16 | + { | |
17 | + error(); | |
18 | + } | |
19 | + char *input = argv[1]; | |
20 | + if(!strcmp(input,"error")) | |
21 | + { | |
22 | + error(); | |
23 | + } | |
24 | + char **inputs = comma_separate(input); | |
25 | + if(inputs == NULL) | |
26 | + { | |
27 | + error(); | |
28 | + } | |
29 | + int num_inputs = num_occurrences(input,','); | |
30 | + if(num_inputs != 2) | |
31 | + { | |
32 | + error(); | |
33 | + } | |
34 | + int input_length = strlen(inputs[0]); | |
35 | + if(!strcmp("logic-left",argv[2])) | |
36 | + { | |
37 | + type = LOGIC; | |
38 | + direction = LEFT; | |
39 | + } | |
40 | + else if(!strcmp("logic-right",argv[2])) | |
41 | + { | |
42 | + type = LOGIC; | |
43 | + direction = RIGHT; | |
44 | + } | |
45 | + else if(!strcmp("arith-left",argv[2])) | |
46 | + { | |
47 | + type = ARITH; | |
48 | + direction = LEFT; | |
49 | + } | |
50 | + else if(!strcmp("arith-right",argv[2])) | |
51 | + { | |
52 | + type = ARITH; | |
53 | + direction = RIGHT; | |
54 | + } | |
55 | + else if(!strcmp("rotate-left",argv[2])) | |
56 | + { | |
57 | + type = ROTATE; | |
58 | + direction = LEFT; | |
59 | + } | |
60 | + else if(!strcmp("rotate-right",argv[2])) | |
61 | + { | |
62 | + type = ROTATE; | |
63 | + direction = RIGHT; | |
64 | + } | |
65 | + else | |
66 | + { | |
67 | + error(); | |
68 | + } | |
69 | + shift = bin2udec(inputs[1]); | |
70 | + while(shift >= input_length) | |
71 | + { | |
72 | + shift = shift % input_length; | |
73 | + } | |
74 | + char *result; | |
75 | + if(!shift) | |
76 | + { | |
77 | + result = inputs[0]; | |
78 | + } | |
79 | + else | |
80 | + { | |
81 | + if(type == LOGIC) | |
82 | + { | |
83 | + if(direction == LEFT) | |
84 | + { | |
85 | + result = dec2bin(bin2udec(inputs[0]) << shift,input_length); | |
86 | + } | |
87 | + else | |
88 | + { | |
89 | + result = dec2bin(bin2udec(inputs[0]) >> shift,input_length); | |
90 | + } | |
91 | + } | |
92 | + else if(type == ARITH) | |
93 | + { | |
94 | + if(direction == LEFT) | |
95 | + { | |
96 | + result = dec2bin(bin2dec(inputs[0]) << shift,input_length); | |
97 | + } | |
98 | + else | |
99 | + { | |
100 | + result = dec2bin(bin2dec(inputs[0]) >> shift,input_length); | |
101 | + } | |
102 | + } | |
103 | + else if(type == ROTATE) | |
104 | + { | |
105 | + if(direction == RIGHT) | |
106 | + { | |
107 | + shift = input_length - shift; | |
108 | + } | |
109 | + result = (char*) malloc(sizeof(char) * (input_length+1)); | |
110 | + memset(result,0x00,input_length); | |
111 | + strncpy(result,inputs[0]+shift,input_length-shift); | |
112 | + strncpy(result + (input_length-shift),inputs[0],shift); | |
113 | + } | |
114 | + } | |
115 | + printf("%s",result); | |
116 | + free(result); | |
117 | + free_mem(inputs,num_inputs); | |
118 | + return 0; | |
119 | +} | ... | ... |
arith/substractor.c
... | ... | @@ -14,8 +14,6 @@ int check_length(char **inputs, uint8_t num_inputs) |
14 | 14 | |
15 | 15 | int main(int argc, char **argv) |
16 | 16 | { |
17 | - int i; | |
18 | - int pos; | |
19 | 17 | if(argc != 2) |
20 | 18 | { |
21 | 19 | error(); |
... | ... | @@ -42,9 +40,9 @@ int main(int argc, char **argv) |
42 | 40 | } |
43 | 41 | int a,b,borrow_in,c; |
44 | 42 | char borrow_out = '0'; |
45 | - a = bin2dec(inputs[0]); | |
46 | - b = bin2dec(inputs[1]); | |
47 | - borrow_in = bin2dec(inputs[2]); | |
43 | + a = bin2udec(inputs[0]); | |
44 | + b = bin2udec(inputs[1]); | |
45 | + borrow_in = bin2udec(inputs[2]); | |
48 | 46 | c = a-(b+borrow_in); |
49 | 47 | if(c < 0) |
50 | 48 | { | ... | ... |
common/auxiliar.c
... | ... | @@ -150,6 +150,27 @@ int bin2dec(char *bin) |
150 | 150 | return num; |
151 | 151 | } |
152 | 152 | |
153 | +int bin2udec(char *bin) | |
154 | +{ | |
155 | + int i; | |
156 | + int num = 0; | |
157 | + int length = strlen(bin); | |
158 | + int pos = length - 1; | |
159 | + char *aux = (char*) malloc(sizeof(char) * (length+1)); | |
160 | + memset(aux,0x00,length+1); | |
161 | + strcpy(aux,bin); | |
162 | + normalize(&aux,1); | |
163 | + for(i = 0; i < length; i++) | |
164 | + { | |
165 | + if(aux[pos--]) | |
166 | + { | |
167 | + num += 0x1 << i; | |
168 | + } | |
169 | + } | |
170 | + free(aux); | |
171 | + return num; | |
172 | +} | |
173 | + | |
153 | 174 | char* dec2bin(int dec, int length) |
154 | 175 | { |
155 | 176 | char *bin = (char*) malloc(sizeof(char) * (length + 1)); | ... | ... |
common/auxiliar.h
... | ... | @@ -15,6 +15,7 @@ void free_mem(char **inputs, int num_inputs); |
15 | 15 | |
16 | 16 | void normalize(char **inputs, int num_inputs); |
17 | 17 | int bin2dec(char *bin); |
18 | +int bin2udec(char *bin); | |
18 | 19 | char* dec2bin(int dec, int length); |
19 | 20 | void expand_input(char **input, int required_length); |
20 | 21 | void reverse_two_complement(char *bin); | ... | ... |