我不确定如何解决这个问题或我做错了什么,但每当我输入一个值时,它只会关闭运行提示。
所以,似乎我在编码的某个地方确实有问题。每当我运行程序并输入变量时,它总是返回相同的答案..“位置 76 的内容为 0。” 在那张纸条上,有人告诉我“我不知道,但我怀疑程序 A 在指令 10 和 11 上错误地分支了一个固定地址。” - mctylr 但我不知道如何解决这个问题..
我试图弄清楚如何从 R Samuel Klatchko 中融入这个想法。我仍然不确定我错过了什么,但我无法让它发挥作用。
const int OP_LOAD = 3;
const int OP_STORE = 4;
const int OP_ADD = 5;
...
const int OP_LOCATION_MULTIPLIER = 100;
mem[0] = OP_LOAD * OP_LOCATION_MULTIPLIER + ...;
mem[1] = OP_ADD * OP_LOCATION_MULTIPLIER + ...;
operand = memory[ j ] % OP_LOCATION_MULTIPLIER;
operation = memory[ j ] / OP_LOCATION_MULTIPLIER;
我是编程新手,我不是最好的,所以我会为了简单起见。这也是一个 SML 程序。无论如何,这是一个家庭作业,我想要一个好成绩。所以我一直在寻找意见,并确保这个程序能做我希望他们正在寻找的东西。无论如何,这里有说明: 编写 SML(Simpletron 机器语言)程序来完成以下每一项任务:
A) 使用哨兵控制的循环读取正数 s 并计算和打印它们的总和。输入负数时终止输入。B)使用计数器控制循环读取七个数字,一些正数和一些负数,然后计算 + 打印平均值。C) 读取一系列数字,确定并打印最大的数字。读取的第一个数字表示应该处理多少个数字。
不用多说,这是我的程序。全部一起。
int main()
{
const int READ = 10;
const int WRITE = 11;
const int LOAD = 20;
const int STORE = 21;
const int ADD = 30;
const int SUBTRACT = 31;
const int DIVIDE = 32;
const int MULTIPLY = 33;
const int BRANCH = 40;
const int BRANCHNEG = 41;
const int BRANCHZERO = 41;
const int HALT = 43;
int mem[100] = {0}; //Making it 100, since simpletron contains a 100 word mem.
int operation; //taking the rest of these variables straight out of the book seeing as how they were italisized.
int operand;
int accum = 0; // the special register is starting at 0
int j;
// This is for part a, it will take in positive variables in a sent-controlled loop and compute + print their sum. Variables from example in text.
memory [0] = 1010;
memory [01] = 2009;
memory [02] = 3008;
memory [03] = 2109;
memory [04] = 1109;
memory [05] = 4300;
memory [06] = 1009;
j = 0; //Makes the variable j start at 0.
while ( true )
{
operand = memory[ j ]%100; // Finds the op codes from the limit on the memory (100)
operation = memory[ j ]/100;
//using a switch loop to set up the loops for the cases
switch ( operation ){
case 10: //reads a variable into a word from loc. Enter in -1 to exit
cout <<"\n Input a positive variable: ";
cin >> memory[ operand ]; break;
case 11: // takes a word from location
cout << "\n\nThe content at location " << operand << "is " << memory[operand]; break;
case 20:// loads
accum = memory[ operand ]; break;
case 21: //stores
memory[ operand ] = accum; break;
case 30: //adds
accum += mem[operand]; break;
case 31: // subtracts
accum-= memory[ operand ]; break;
case 32: //divides
accum /=(memory[ operand ]); break;
case 33: // multiplies
accum*= memory [ operand ]; break;
case 40: // Branches to location
j = -1; break;
case 41: //branches if acc. is < 0
if (accum < 0)
j = 5;
break;
case 42: //branches if acc = 0
if (accum == 0)
j = 5;
break;
case 43: // Program ends
exit(0); break;
}
j++;
}
return 0;
}