pointers, arrays and simpletron

Hi I am having trouble getting my simpletron to add two numbers and output the result. it is meant to be an exercise in pointers and arrays. I can't see where I'm going wrong at the moment. If any expert can 'point' me in the right direction I would really appreciate it!!

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  //
//  main.cpp
//  simpletron exercise8.18
//
//  Created by James Farrow on 17/02/2016.
//  Copyright © 2016 James Farrow. All rights reserved.
////
//8.18 (Machine-Language Programming) Let’s create a computer we’ll call the Simpletron. As its name implies, it’s a simple machine, but, as we’ll soon see, it’s a powerful one as well. The Sim- pletron runs programs written in the only language it directly understands, that is, Simpletron Ma- chine Language, or SML for short.
//The Simpletron contains an accumulator—a “special register” in which information is put before the Simpletron uses that information in calculations or examines it in various ways. All information in the Simpletron is handled in terms of words. A word is a signed four-digit decimal number, such as +3364, -1293, +0007, -0001, etc. The Simpletron is equipped with a 100-word memory, and these words are referenced by their location numbers 00, 01, ..., 99.
//Before running an SML program, we must load, or place, the program into memory. The first instruction (or statement) of every SML program is always placed in location 00. The simulator will start executing at this location.
//Each instruction written in SML occupies one word of the Simpletron’s memory; thus, instructions are signed four-digit decimal numbers. Assume that the sign of an SML instruction is always plus, but the sign of a data word may be either plus or minus. Each location in the Simpletron’s memory may contain an instruction, a data value used by a program or an unused (and hence undefined) area of memory. The first two digits of each SML instruction are the operation code that specifies the operation to be performed. SML operation codes are shown in Fig. 8.23.
//The last two digits of an SML instruction are the operand—the address of the memory loca- tion containing the word to which the operation applies.

//Input/output operations
//const int READ = 10; Readawordfromthekeyboardintoaspecificlocationin memory.
//const int WRITE = 11; Writeawordfromaspecificlocationinmemorytothe screen.

//Load and store operations
//const int LOAD = 20; Loadawordfromaspecificlocationinmemoryintothe accumulator.
//const int STORE = 21; Storeawordfromtheaccumulatorintoaspecificloca- tion in memory.

//Arithmetic operations
//const int ADD = 30; Addawordfromaspecificlocationinmemorytothe word in the accumulator (leave result in accumulator).
//const int SUBTRACT = 31; Subtractawordfromaspecificlocationinmemoryfrom the word in the accumulator (leave result in accumula-tor).
//const int DIVIDE = 32; Divideawordfromaspecificlocationinmemoryinto the word in the accumulator (leave result in accumula-tor).
//const int MULTIPLY = 33; Multiplyawordfromaspecificlocationinmemoryby the word in the accumulator (leave result in accumula-tor).

//Transfer-of-control operations
//const int BRANCH = 40; Branchtoaspecificlocationinmemory.
//const int BRANCHNEG = 41; Branchtoaspecificlocationinmemoryiftheaccumula- tor is negative.
//const int BRANCHZERO = 42; Branchtoaspecificlocationinmemoryiftheaccumula- tor is zero.
//const int HALT = 43; Halt—theprogramhascompleteditstask.

//Now let’s consider two simple SML programs. The first (Fig. 8.24) reads two numbers from the keyboard and computes and prints their sum. The instruction +1007 reads the first number from the keyboard and places it into location 07 (which has been initialized to zero). Instruction +1008 reads the next number into location 08. The load instruction, +2007, places (copies) the first number into the accumulator, and the add instruction, +3008, adds the second number to the number in the accumulator. All SML arithmetic instructions leave their results in the accumulator. The store instruction, +2109, places (copies) the result back into memory location 09. Then the write instruction, +1109, takes the number and prints it (as a signed four-digit decimal number). The halt instruction, +4300, terminates execution.


//program as below
//00 +1007 (Read A)
//01 +1008 (Read B)
//02 +2007 (Load A)
//03 +3008 (Add B)
//04 +2109 (Store C)
//05 +1109 (Write C)
//06 +4300 (Halt)
//07 +0000 (Variable A)
//08 +0000 (Variable B)
//09 +0000 (Result C)

#include <iostream>
#include <string>
#include <iomanip>

void inputCommands(int *);
void copyAccumulatorToMemory(int *, int *);
void processCommandsAndExecute(int *, int *);
void printAccumulator(int *);

int main()
{
    const int arraySize = 50;//arraysize for accumulator & memory
    
    int accumulator[arraySize] = {0};
    int memory[arraySize] = {10};
    
    int *accumulatorPtr;
    accumulatorPtr = accumulator;
    
    int *memoryPtr;
    memoryPtr = memory;
    
    inputCommands(memoryPtr);
    copyAccumulatorToMemory(memory, accumulator);
    processCommandsAndExecute(accumulator, memory);
    printAccumulator(accumulatorPtr);
    std::cout << std::endl;
    
    return 0;
    
}


//input commands to register memory
void inputCommands(int *memoryPtr)
{
    int inputFromKeyboard = 0;
    int counter = 0;
    
    while (inputFromKeyboard != 4300) {
        std::cout << "Please inpt your SML commands HALT:4300 to end input: ";
        std::cin >> inputFromKeyboard;
        *(memoryPtr + counter) = inputFromKeyboard;
        counter++;
    }
}


//copy contents of memory to accumulator for simpletron to process
void copyAccumulatorToMemory(int *memPtr, int *accumPtr)
{
    int counter = 0;
    
    while (*(memPtr + counter) != 0) {
        *(accumPtr + counter) = *(memPtr + counter);
        counter++;
    }
}

void printAccumulator(int *accumPtr)
{
    std::cout << "In print array" << std::endl;
    int arrayCounter = 0;
    while (arrayCounter < 10) {
        std::cout << *(accumPtr + arrayCounter) << std::endl;
        arrayCounter++;
    }
}

void processCommandsAndExecute(int *accumulatorPtr, int *memoryPtr)
{
    int command = 0;
    int operand = 0;
    int pointerCounter = 0;
    
    while (pointerCounter != 4300){
        int integer = 0;
        command = memoryPtr[pointerCounter];
        int memLocation = command % 100;
        command /= 100;
        operand = command % 100;
        switch (operand) {
            case 10:
                std::cout << "Input an integer: ";
                std::cin >> integer;
                while (!std::cin) {
                    std::cin.clear();
                    std::cin.ignore();
                    std::cout << "Input an number: ";
                    std::cin >> integer;
                }
                memoryPtr[memLocation] = integer;
                pointerCounter++;
                break;
            case 11:
                std::cout << "Answer: " << *(accumulatorPtr + memLocation ) << std::endl;
                pointerCounter = 4300;
                break;
            case 20:
                accumulatorPtr[memLocation] = memoryPtr[memLocation];
                pointerCounter++;
                break;
            case 21:
                accumulatorPtr[memLocation -2] = accumulatorPtr[memLocation];
                pointerCounter++;
                break;
            case 30:
                accumulatorPtr[memLocation -1] += memoryPtr[memLocation];
                pointerCounter++;
                break;
//            case 31:
//                accum[memLocation] = accum[memLocation] - mem[memLocation];
//                break;
            default:
                break;
            }
        }
}
bump 😜
Topic archived. No new replies allowed.