我如何修复此代码它是alu代码 [英] How do i.can fix this code it is alu code
本文介绍了我如何修复此代码它是alu代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
#include <stdlib.h>
//defineOpCodes
int OR(int A, int B);
int AddSub(int A,int B, int OpCode);
int mux(short In1, short In2, short Sel);
int AND(int A,int B);
int main(){
#define OP_ADD 0x00
#define OP_SUB 0x01
#define OP_LEFT 0x02
#define OP_RIGHT 0x03
int A = 0b0000000001111011;
int B = 0b0000000001100100;
int Result = 0;
char operation[4];
printf ("Which operation would you like? ADD/SUB/OR/AND > ");
sscanf("%s",operation);
int opcode=0;
if (operation == "ADD")
int OpCode = ADD( A, B);
else
if(operation == "SUB")
int OpCode = SUB(A ,B);
else
if(operation == "AND")
OpCode = 0;
else
if (operation == "OR")
OpCode = 0;
int z = AddSub(int A, int B, int OpCode);
int AndOut = AND(int A,int B);
int OrOut = OR(int A,int B);
int MuxOut = mux(AndOut, OrOut, s0);
int MuxOut2 = mux(MuxOut1,z, s1);
return MuxOut2;
}
// Input and Output Registers
int A = 0;
int B = 0;
int Result = 0;
// Status registers
int Z = 0; // bitstring.Bits(uint=bytes, length=8)
int Ovf = 0;
// Arithemtic and Logical Unit
int AddSub(int A,int B, int OpCode){
if (OpCode == ADD)
Sum = A + B;
else
Sum = A - B;
//set up the Z flag. Ignore the Ovf right flag for now
if (Sum == 0)
Z = 1;
else
Z = 0;
return Sum, Z;
}
// 2-to-1 Multiplexer
int mux(short In1, short In2, short Sel)
{
int Out;
if (Sel == 0)
Out = In1;
else
Out = In2;
return(Out);
}
// AND Gate
int AND(A, B){
return A&B;
}
//OR Gate
int OR(A, B){
return A|B;
}
我的尝试:
我不知道有人可以帮助我的错误在哪里
What I have tried:
I don't know where is the mistake if someone can help me
推荐答案
你的代码充满了bug。你真的需要阅读C
编程语言的教程。
程序的'编译'版本如下
Your code is full of bugs. You really need to read a tutorial onC
programming language.
A 'compiling' version of your program follows
#include <stdio.h>
#include <string.h>
#define ADD 0
#define SUB 1
int or(int a, int b);
int and(int a, int b);
int addsub(int a, int b, int opcode);
int mux(int a, int b, int sel);
int global_z; // zero flag (as matter of fact you shouldn't use globals)
int main()
{
int a = 0b0000000001111011;
int b = 0b0000000001100100;
int result;
char operation[4];
printf ("Which operation would you like? ADD/SUB/OR/AND > ");
if (! fgets(operation, 4, stdin) )
return -1;
if ( strcmp( operation, "ADD") == 0)
result = addsub(a, b, ADD);
else if ( strcmp( operation, "SUB") == 0)
result = addsub(a, b, SUB);
else if ( strcmp( operation, "OR") == 0)
result = or(a,b);
else if ( strcmp( operation, "AND") == 0)
result = and(a,b);
else
printf("invalid operation %s\n", operation);
printf("the result of the %s operation is %d\n", operation, result);
printf("zero flag value = %d\n", global_z);
int muxout0 = mux(a,b,0);
int muxout1 = mux(a,b,1);
printf("muxout0 = %d, muxout1 = %d\n",muxout0, muxout1);
return 0;
}
int or(int a, int b)
{
int r = a | b;
global_z = r == 0 ? 1 : 0;
return r;
}
int and(int a, int b)
{
int r = a & b;
global_z = r == 0 ? 1 : 0;
return r;
}
int addsub(int a, int b, int opcode)
{
int r;
if ( opcode == ADD)
r = a + b;
else
r = a - b;
global_z = r == 0 ? 1 : 0;
return r;
}
int mux(int a, int b, int sel)
{
int r;
r = sel == 0 ? a : b;
global_z = r == 0 ? 1 : 0;
return r;
}
这篇关于我如何修复此代码它是alu代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文