Commit 35b99e062624c2b6183dc78c967ba6017f3b3129
1 parent
7aeec1aa
--no commit message
Showing
4 changed files
with
91 additions
and
2 deletions
arith/Makefile
1 | -TARGETS = adder | 1 | +TARGETS = adder substractor |
2 | 2 | ||
3 | .PHONY: all clean | 3 | .PHONY: all clean |
4 | 4 | ||
@@ -8,4 +8,6 @@ clean: | @@ -8,4 +8,6 @@ clean: | ||
8 | rm -rf $(TARGETS) | 8 | rm -rf $(TARGETS) |
9 | 9 | ||
10 | adder: | 10 | adder: |
11 | - gcc -I../common/ ../common/auxiliar.c adder.c -o adder | ||
12 | \ No newline at end of file | 11 | \ No newline at end of file |
12 | + gcc -I../common/ ../common/auxiliar.c adder.c -o adder | ||
13 | +substractor: | ||
14 | + gcc -I../common/ ../common/auxiliar.c substractor.c -o substractor | ||
13 | \ No newline at end of file | 15 | \ No newline at end of file |
arith/substractor.c
0 → 100644
1 | +#include <stdio.h> | ||
2 | +#include <string.h> | ||
3 | +#include "auxiliar.h" | ||
4 | + | ||
5 | +int check_length(char **inputs, uint8_t num_inputs) | ||
6 | +{ | ||
7 | + int input_length = strlen(inputs[0]); | ||
8 | + if((input_length != strlen(inputs[1])) || (strlen(inputs[2]) != 1)) | ||
9 | + { | ||
10 | + return -1; | ||
11 | + } | ||
12 | + return input_length; | ||
13 | +} | ||
14 | + | ||
15 | +int main(int argc, char **argv) | ||
16 | +{ | ||
17 | + /*int i; | ||
18 | + int pos; | ||
19 | + if(argc != 2) | ||
20 | + { | ||
21 | + error(); | ||
22 | + } | ||
23 | + char *input = argv[1]; | ||
24 | + if(!strcmp(input,"error")) | ||
25 | + { | ||
26 | + error(); | ||
27 | + } | ||
28 | + char **inputs = comma_separate(input); | ||
29 | + if(inputs == NULL) | ||
30 | + { | ||
31 | + error(); | ||
32 | + } | ||
33 | + int num_inputs = num_occurrences(input,','); | ||
34 | + if(num_inputs != 3) | ||
35 | + { | ||
36 | + error(); | ||
37 | + } | ||
38 | + int input_length = check_length(inputs,num_inputs); | ||
39 | + if(input_length == -1) | ||
40 | + { | ||
41 | + error(); | ||
42 | + } | ||
43 | + int a,b,borrow_in,c; | ||
44 | + char borrow_out = '0'; | ||
45 | + a = bin2dec(inputs[0]); | ||
46 | + b = bin2dec(inputs[1]); | ||
47 | + borrow_in = bin2dec(inputs[2]); | ||
48 | + c = a-(b+borrow_in); | ||
49 | + if(c < 0) | ||
50 | + { | ||
51 | + borrow_out = '1'; | ||
52 | + } | ||
53 | + char *result = dec2bin(c,input_length); | ||
54 | + printf("%s,%c",result,borrow_out); | ||
55 | + free(result); | ||
56 | + free_mem(inputs,num_inputs); | ||
57 | + return 0;*/ | ||
58 | + printf("%d",bin2dec("11000000")); | ||
59 | + return 0; | ||
60 | +} |
common/auxiliar.c
@@ -126,10 +126,16 @@ int bin2dec(char *bin) | @@ -126,10 +126,16 @@ int bin2dec(char *bin) | ||
126 | { | 126 | { |
127 | int i; | 127 | int i; |
128 | int num = 0; | 128 | int num = 0; |
129 | + int factor = 1; | ||
129 | int length = strlen(bin); | 130 | int length = strlen(bin); |
130 | int pos = length - 1; | 131 | int pos = length - 1; |
131 | char *aux = (char*) malloc(sizeof(char) * (length+1)); | 132 | char *aux = (char*) malloc(sizeof(char) * (length+1)); |
132 | strcpy(aux,bin); | 133 | strcpy(aux,bin); |
134 | + if(aux[0] == '1') | ||
135 | + { | ||
136 | + reverse_two_complement(aux); | ||
137 | + factor = -1; | ||
138 | + } | ||
133 | normalize(&aux,1); | 139 | normalize(&aux,1); |
134 | for(i = 0; i < length; i++) | 140 | for(i = 0; i < length; i++) |
135 | { | 141 | { |
@@ -138,6 +144,7 @@ int bin2dec(char *bin) | @@ -138,6 +144,7 @@ int bin2dec(char *bin) | ||
138 | num += 0x1 << i; | 144 | num += 0x1 << i; |
139 | } | 145 | } |
140 | } | 146 | } |
147 | + num *= factor; | ||
141 | free(aux); | 148 | free(aux); |
142 | return num; | 149 | return num; |
143 | } | 150 | } |
@@ -185,3 +192,22 @@ int str2int(char *string) | @@ -185,3 +192,22 @@ int str2int(char *string) | ||
185 | } | 192 | } |
186 | return num; | 193 | return num; |
187 | } | 194 | } |
195 | + | ||
196 | +void reverse_two_complement(char *bin) | ||
197 | +{ | ||
198 | + int i; | ||
199 | + for(i = 0; i < strlen(bin); i++) | ||
200 | + { | ||
201 | + if(bin[i] == '0') | ||
202 | + { | ||
203 | + bin[i] = '1'; | ||
204 | + } | ||
205 | + else | ||
206 | + { | ||
207 | + bin[i] = '0'; | ||
208 | + } | ||
209 | + } | ||
210 | + char *bin_added = dec2bin(bin2dec(bin)+1,strlen(bin)); | ||
211 | + strcpy(bin,bin_added); | ||
212 | + free(bin_added); | ||
213 | +} | ||
188 | \ No newline at end of file | 214 | \ No newline at end of file |
common/auxiliar.h
@@ -17,3 +17,4 @@ void normalize(char **inputs, int num_inputs); | @@ -17,3 +17,4 @@ void normalize(char **inputs, int num_inputs); | ||
17 | int bin2dec(char *bin); | 17 | int bin2dec(char *bin); |
18 | char* dec2bin(int dec, int length); | 18 | char* dec2bin(int dec, int length); |
19 | void expand_input(char **input, int required_length); | 19 | void expand_input(char **input, int required_length); |
20 | +void reverse_two_complement(char *bin); |