ref: 7fa4a7f3243cfcd2e4c839df66c10147459fb99d
parent: 90361ec4ee72e3f2944e6e326d51678f3f73830c
author: mkf <mkf@d510>
date: Fri May 5 14:32:33 EDT 2023
fix build
--- a/5551a.c
+++ b/5551a.c
@@ -2,16 +2,16 @@
#include <stdio.h>
#include <string.h>
-#include "5555.h"
+#include "5551.h"
int main(int argc, char* argv[])
{
unsigned int inst, a, b, c;
- char string[32], *op;
+ char string[32], op[4];
while(scanf("%s %[^\n;]%*c", op, string) > 0)
{
- sscanf(string, " %u %u %u", &a, &b, &c);
+ sscanf(string, "%u %u %u", &a, &b, &c);
if(!strcmp(op, "DIE"))
inst = DIE;
if(!strcmp(op, "OUT"))
@@ -40,40 +40,40 @@
inst = LDM;
if(!strcmp(op, "NTH"))
inst = NTH;
- }
- switch(inst)
- {
- /* no args */
- case DIE:
- printf("%u\n", inst);
+
+ switch(inst)
+ {
+ /* no args */
+ case DIE:
+ printf("%u\n", inst);
+ break;
+ /* one arg */
+ case NOT:
+ case OUT:
+ printf("%u %u\n", inst, a);
+ break;
+ /* two args */
+ case PUT:
+ case MOV:
+ case LDM:
+ printf("%u %u %u\n", inst, a, b);
break;
- /* one arg */
- case NOT:
- case OUT:
- printf("%u %u\n", inst, a);
+ /* three args */
+ case AND:
+ case NOR:
+ case XOR:
+ case SUB:
+ case SHL:
+ case SHR:
+ case ADD:
+ case NTH:
+ printf("%u %u %u %u\n", inst, a, b, c);
break;
- /* two args */
- case PUT:
- case MOV:
- case LDM:
- printf("%u %u %u\n", inst, a, b);
- break;
- /* three args */
- case AND:
- case NOR:
- case XOR:
- case SUB:
- case SHL:
- case SHR:
- case ADD:
- case NTH:
- printf("%u %u %u %u\n", inst, a, b, c);
- break;
- /* should this ever happen? */
- default:
- {
- fprintf(stderr, "unkown instr.\n");
- return -1;
+ /* should this ever happen? */
+ default:
+ fprintf(stderr, "unkown instr.\n");
+ return -1;
+
}
}
return 0;
--- a/5551e.c
+++ b/5551e.c
@@ -11,14 +11,12 @@
int regs[8];
}reg_t;
-bool add_bit(bool a, bool b, bool &carry)
+bool add_bit(bool a, bool b, bool *carry)
{
- bool out, temp, tcarry = carry;
- temp = a & b;
- out = ((a ^ b) | (temp & tcarry));
- temp = a ^ b;
- carry = ((a & b) | (temp & tcarry));
- return temp;
+ bool out, temp = carry;
+ out = ((a ^ b) ^ temp);
+ carry = (((a ^ b) & temp) | (a & b));
+ return out;
}
int main()
@@ -29,15 +27,16 @@
memory[MMAX-1] = -1;
char string[16];
-
+ r.flags[ON] = 1;
+ r.flags[NSIGN] = 0;
unsigned int op, a, b, c;
/* there is no space between op and args
since some can have no args */
- while(scanf(r.flags[ON] && "%u%[^\n]", &op, string))
+ while(r.flags[ON] && scanf("%u %[^\n]", &op, string))
{
- sscanf(string, " %u %u", &a, &b, &c);
+ sscanf(string, "%u %u %u", &a, &b, &c);
switch(op)
{
case DIE:
@@ -46,43 +45,43 @@
case OUT:
/* just in case output is "manfi" */
if(r.flags[NSIGN])
- printf("-%u\n", r.reg[a]);
+ printf("-%u\n", r.regs[a]);
else
- printf("%u\n" r.reg[a]);
+ printf("%u\n", r.regs[a]);
break;
case NOT:
- r.reg[b] = !a;
+ r.regs[b] = !a;
break;
case AND:
- r.reg[c] = a & b;
+ r.regs[c] = a & b;
break;
case NOR:
- r.reg[c] = !(a | b)
+ r.regs[c] = !(a | b);
case XOR:
- r.reg[c] = a ^ b;
+ r.regs[c] = a ^ b;
break;
case SUB:
b = ~b;
- r.carry = 1;
+ r.regs[CARRY] = 1;
case ADD:
- r.reg[c] = add_bit(a, b, &r.flags[carry]);
+ r.regs[c] = add_bit(a, b, &r.flags[CARRY]);
break;
case SHR:
- r.reg[c] = reg[a] >> b;
+ r.regs[c] = r.regs[a] >> b;
break;
case SHL:
- r.reg[c] = reg[a] << b;
+ r.regs[c] = r.regs[a] << b;
break;
case PUT:
memory[b] = a;
case MOV:
- memory[b] = reg[a];
+ memory[b] = r.regs[a];
break;
case LDM:
- r.reg[b] = memory[a];
+ r.regs[b] = memory[a];
break;
case NTH:
- r.reg[c] = a >> n;
+ r.regs[c] = a >> b;
break;
default:
fprintf(stderr, "unkown op\n");
@@ -89,10 +88,10 @@
return 1;
}
/* clear them */
- a = 0
- b = 0
+ a = 0;
+ b = 0;
op = 0;
- string = "";
+ string[0] = '\0';
}
return 0;
}