I asked this yesterday, after the input given I'm still having trouble implementing
I'm not sure how to fix this or what I did wrong, but whenever I enter in a value it just closes out the run prompt.
So, seems I do have a problem somewhere in my coding. Whenever I run the program and input a variable, it always returns the same answer.."The content at location 76 is 0." On that note, someone told me that "I don't know, but I suspect that Program A incorrectly has a fixed address being branched to on instructions 10 and 11." - mctylr but I'm not sure how to fix that..
I'm trying to figure out how to incorporate this idea from R Samuel Klatchko.. I'm still not sure what I'm missing but I can't get it to work..
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;
I'm new to programming, I'm not the best, so I'm going for simplicity. Also this is an SML program. Anyway, this IS a homework assignment and I'm wanting a good grade on this. So I was looking for input and making sure this program will do what I'm hoping they are looking for. Anyway, here are the instructions: Write SML (Simpletron Machine language) programs to accomplish each of the following task:
A) Use a sentinel-controlled loop to read positive number s and compute and print their sum. Terminate input when a neg number is entered. B) Use a counter-controlled loop to read seven numbers, some positive and some negative, and compute + print the avg. C) Read a series of numbers, and determine and print the largest number. The first number read indicates how many numbers should be processed.
Without further a due, here is my program. All together.
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;
}
So, seems I do have a problem somewhere in my coding. Whenever I run the program and input a variable, it always returns the same answer.."The content at location 76 is 0."
Look at what your program is doing:
memory [0] = 1010; /* Read from user and store at address 10 */
memory [01] = 2009; /* Read garbage into acc from address 9 */
memory [02] = 3008; /* Add whatever garbage is in address 8 into accumulator */
memory [03] = 2109; /* Store garbage from accumulator into address 9 */
memory [04] = 1109; /* Print the contents of address 9, which is garbage */
memory [05] = 4300; /* Stop */
memory [06] = 1009; /* Read from user and store in address 9 */
So... yeah. To debug something like this, you just need to print out all the pertinent variables of your program to see whether they're what you think they are. For example, in case 10 you could have done cout << "10: operand is " << operand << endl;
and then in case 11 you could have done cout << "11: operand is " << operand << endl;
and you would have seen right away that the operand was 10 in the first instruction, 9 in the second, and 8 in the 3rd.
If you want to guarantee that memory always starts with the value 0, write a for loop (you can change your declaration to do it but there are subtleties you'd need to learn):
for( int i = 0; i < sizeof(memory); ++i ) { memory[i] = 0; }
Here's working program that adds 2 values the user enters and prints the result:
memory [0] = 1009;
memory [1] = 1008;
memory [2] = 2009;
memory [3] = 3008;
memory [4] = 2109;
memory [5] = 1109;
memory [6] = 4300;
As I suggested yesterday in your original question. I believe you may have an error in case 10
and 11
hard coded to modify the "stack pointer" (j
) to 5 if I'm reading your code correctly.
The (This has been fixed in your example)case
statements for switch (operation)
don't match your opcode constants (e.g. READ = 10
, BRANCH = 40
).
For debugging at least having a default statement in the switch to catch unknown operations is recommended to catch mistakes.
Added:
I'd also suggest printing the operation and operand as they are being executed, to help you follow the Simpletron's program execution.
You still have not fixed the usage of leading zeros of memory
addresses. The C/C++ compiler interprets the leading zero as meaning octal (base 8) number system.
Your example code as posted does not even compile. Please edit and fix the variable name usage (hint: mixing mem
and memory
).
Code fixes removed.
The values in the switch statement are incorrect. The operation values will be 10, 11, 20, 21 etc. not 1, 2, 3 etc as you check for in your cases. Which means none of your code gets executed as you don't have cases for these numbers.
Good luck!
精彩评论