4.4 A Simple Program
We now present a simple program written for MARIE. In Section 4.6, we present several additional examples to illustrate the power of this minimal architecture. It can even be used to run programs with procedures, various looping constructs, and different selection options.
Our first program adds two numbers together (both of which are found in main memory), storing the sum in memory. (We forgo input/output for now.)
Table 4.3 lists an assembly language program to do this, along with its corresponding machine-language program. The list of instructions under the Instruction column constitutes the actual assembly language program. We know that the fetch-decode-execute cycle starts by fetching the first instruction of the program, which it finds by loading the PC with the address of the first instruction when the program is loaded for execution. For simplicity, let's assume our programs in MARIE are always loaded starting at address 100 (in hex).
Table 4.3: A Program to Add Two Numbers
|
Hex Address
|
Instruction
|
Binary Contents of Memory Address
|
Hex Contents of Memory
|
|
100
101
102
103
104
105
106
|
Load 104
Add 105
Store 106
Halt
0023
FFE9
0000
|
0001000100000100
0011000100000101
0010000100000110
0111000000000000
0000000000100011
1111111111101001
0000000000000000
|
1104
3105
2106
7000
0023
FFE9
0000
|
The list of instructions under the Binary Contents of Memory Address column constitutes the actual machine language program. It is often easier for humans to read hexadecimal as opposed to binary, so the actual contents of memory are displayed in hexadecimal.
This program loads 002316 (or decimal value 35) into the AC. It then adds the hex value FFE9 (decimal -23) that it finds at address 105. This results in a value of 12 in the AC. The Store instruction stores this value at memory location 106. When the program is done, the binary contents of location 106 change to 0000000000001100, which is hex 000C, or decimal 12. Figure 4.13 indicates the contents of the registers as the program executes.
The last RTN instruction in Part c places the sum at the proper memory location. The statement "decode IR[15-12]" simply means the instruction must be decoded to determine what is to be done. This decoding can be done in software (using a microprogram) or in hardware (using hardwired circuits). These two concepts are covered in more detail in Section 4.7.
Note that there is a one-to-one correspondence between the assembly language and the machine language instructions. This makes it easy to convert assembly language into machine code. Using the instruction tables given in this chapter, you should be able to hand assemble any of our example programs. For this reason, we look at only the assembly language code from this point on. Before we present more programming examples, however, a discussion of the assembly process is in order.