wm: 5551

Download patch

ref: cff6c35a865e4563695928b12ea9f38c7b3099f4
parent: 34b0870d5fce23038d4435b181cd3a6ba2b4d174
author: Mahdi Hoessini <mahdi.hoseini1381@yahoo.com>
date: Thu May 11 04:22:19 EDT 2023

add mulitbit adder

--- a/5551e.c
+++ b/5551e.c
@@ -11,14 +11,29 @@
 	int regs[8];
 }reg_t;
 
-bool add_bit(bool a, bool b, bool *carry)
+bool carry = false;
+bool add_bit(bool a, bool b)
 {
-	bool out, temp = *carry;
-	out = ((a ^ b) ^ temp);
-	*carry = (((a ^ b) & temp) | (a & b));
+	bool out;
+	out = ((a ^ b) ^ carry);
+	carry = (((a ^ b) & carry) | (a & b));
 	return out;
 }
 
+int btoi(bool b[], int len)
+{
+  char arr[10];
+  int res = 0, i = 0, j = len;
+  while (i <= len) 
+    {
+		arr[i] = b[j] ? '1' : '0';
+		i++;
+		j--;
+    }
+  sscanf(arr, "%d", &res);
+    return res;
+}
+
 int main()
 {
 
@@ -29,7 +44,8 @@
 	char string[16];
 	r.flags[ON] = 1;
 	r.flags[NSIGN] = 0;
-	unsigned int op, a, b, c;
+	unsigned int op, a, b, c, ind = 0;
+    bool barr[10];
 
 		/* there is no space between op and args
 			since some can have no args */
@@ -63,10 +79,20 @@
 				break;
 			case SUB:
 				b = ~b;
-				r.regs[CARRY] = 1;
+				carry = 1;
 			case ADD:
-				r.regs[c] = add_bit(a, b, &r.flags[CARRY]);
+				while (a > 0 && b > 0) {
+					int diga = a % 10;
+					int digb = b % 10;
+					barr[ind] = add_bit(diga, digb);
+					a /= 10;
+					b /= 10;
+					ind++;
+				}
+				barr[ind] = carry;
+				r.regs[c] = btoi(barr,ind);
 				break;
+				break;
 			case SHR:
 				r.regs[c] = a >> b;
 				break;
@@ -85,7 +111,7 @@
 				r.regs[c] = a >> b;
 				break;
 			default:
-				fprintf(stderr, "unkown op\n");
+				fprintf(stderr, "unknown op\n");
 				return 1;
 		}
 		/* clear them */
@@ -93,6 +119,8 @@
 		b = 0;
 		op = 0;
 		string[0] = '\0'; 
+        ind = 0;
+        carry = 0;
 	}
 	return 0;
 }