diff --git a/arith/Makefile b/arith/Makefile index a183b5b..b752a6e 100644 --- a/arith/Makefile +++ b/arith/Makefile @@ -1,4 +1,4 @@ -TARGETS = adder +TARGETS = adder substractor .PHONY: all clean @@ -8,4 +8,6 @@ clean: rm -rf $(TARGETS) adder: - gcc -I../common/ ../common/auxiliar.c adder.c -o adder \ No newline at end of file + gcc -I../common/ ../common/auxiliar.c adder.c -o adder +substractor: + gcc -I../common/ ../common/auxiliar.c substractor.c -o substractor \ No newline at end of file diff --git a/arith/substractor.c b/arith/substractor.c new file mode 100644 index 0000000..f3071a2 --- /dev/null +++ b/arith/substractor.c @@ -0,0 +1,60 @@ +#include +#include +#include "auxiliar.h" + +int check_length(char **inputs, uint8_t num_inputs) +{ + int input_length = strlen(inputs[0]); + if((input_length != strlen(inputs[1])) || (strlen(inputs[2]) != 1)) + { + return -1; + } + return input_length; +} + +int main(int argc, char **argv) +{ + /*int i; + int pos; + if(argc != 2) + { + error(); + } + char *input = argv[1]; + if(!strcmp(input,"error")) + { + error(); + } + char **inputs = comma_separate(input); + if(inputs == NULL) + { + error(); + } + int num_inputs = num_occurrences(input,','); + if(num_inputs != 3) + { + error(); + } + int input_length = check_length(inputs,num_inputs); + if(input_length == -1) + { + error(); + } + int a,b,borrow_in,c; + char borrow_out = '0'; + a = bin2dec(inputs[0]); + b = bin2dec(inputs[1]); + borrow_in = bin2dec(inputs[2]); + c = a-(b+borrow_in); + if(c < 0) + { + borrow_out = '1'; + } + char *result = dec2bin(c,input_length); + printf("%s,%c",result,borrow_out); + free(result); + free_mem(inputs,num_inputs); + return 0;*/ + printf("%d",bin2dec("11000000")); + return 0; +} diff --git a/common/auxiliar.c b/common/auxiliar.c index 4a1200a..b6cb383 100644 --- a/common/auxiliar.c +++ b/common/auxiliar.c @@ -126,10 +126,16 @@ int bin2dec(char *bin) { int i; int num = 0; + int factor = 1; int length = strlen(bin); int pos = length - 1; char *aux = (char*) malloc(sizeof(char) * (length+1)); strcpy(aux,bin); + if(aux[0] == '1') + { + reverse_two_complement(aux); + factor = -1; + } normalize(&aux,1); for(i = 0; i < length; i++) { @@ -138,6 +144,7 @@ int bin2dec(char *bin) num += 0x1 << i; } } + num *= factor; free(aux); return num; } @@ -185,3 +192,22 @@ int str2int(char *string) } return num; } + +void reverse_two_complement(char *bin) +{ + int i; + for(i = 0; i < strlen(bin); i++) + { + if(bin[i] == '0') + { + bin[i] = '1'; + } + else + { + bin[i] = '0'; + } + } + char *bin_added = dec2bin(bin2dec(bin)+1,strlen(bin)); + strcpy(bin,bin_added); + free(bin_added); +} \ No newline at end of file diff --git a/common/auxiliar.h b/common/auxiliar.h index e98d974..e5dc34e 100644 --- a/common/auxiliar.h +++ b/common/auxiliar.h @@ -17,3 +17,4 @@ void normalize(char **inputs, int num_inputs); int bin2dec(char *bin); char* dec2bin(int dec, int length); void expand_input(char **input, int required_length); +void reverse_two_complement(char *bin);