A
directly, as follows:
Code
Comments
add A, 2, A
Add 2 to the contents of register A and place the result back into A,
overwriting whatever was there.
14
Chapter 1
I’ve actually been using immediate values all along in my examples, but
just not in any arithmetic instructions. In all of the preceding examples, each
load and store uses an immediate value in order to specify a memory address.
So the #12 in the load instruction in line 1 of Program 1-1 is just an immediate value (a regular whole number) prefixed by a # sign to let the computer
know that this particular immediate value is a memory address that desig-
nates a cell in memory.
Memory addresses are just regular whole numbers that are specially
marked with the # sign. Because they’re regular whole numbers, they can be
stored in registers—and stored in memory—just like any other number.
Thus, the whole-number contents of a register, like D, could be construed by
the computer as representing a memory address.
For example, say that we’ve stored the number 12 in register D, and that we
intend to use the contents of D as the address of a memory cell in Program 1-2.
Line
Code
Comments
1
load #D, A
Read the contents of the memory cell designated by the number
stored in D (where D = 12) into register A.
2
load #13, B
Read the contents of memory cell #13 into register B.
3
add A, B, C
Add the numbers in registers A and B and store the result in C.
4
store C, #14
Write the result of the addition from register C into memory cell #14.
Program 1-2: Program to add two numbers from main memory using an address stored in a register
Program 1-2 is essentially the same as Program 1-1, and given the same
input, it yields the same results. The only difference is in line 1:
Program 1-1, Line 1
Program 1-2, Line 1
load #12, A
load #D, A
Since the content of D is the number 12, we can tell the computer to
look in D for the memory cell address by substituting the register name
(this time marked with a # sign for use as an address), for the actual
memory cell number in line 1’s load instruction. Thus, the first lines of
Programs 1-1 and 1-2 are functionally equivalent.
This same trick works for store instructions, as well. For example, if we
place the number 14 in D we can modify the store command in line 4 of
Program 1-1 to read as follows: store C, #D. Again, this modification would
not change the program’s output.
Basic Computing Concepts
15
Because memory addresses are just regular numbers, they can be stored
in memory cells as well as in registers. Program 1-3 illustrates the use of a
memory address that’s stored in another memory cell. If we take the input
for Program 1-1 and apply it to Program 1-3, we get the same output as if
we’d just run Program 1-1 without modification:
Line
Code
Comments
1
load #11, D
Read the contents of memory cell #11 into D.
2
load #D, A
Read the contents of the memory cell designated by the number in D
(where D = 12) into register A.
3
load #13, B
Read the contents of memory cell #13 into register B.
4
add A, B, C
Add the numbers in registers A and B and store the result in C.
5
store C, #14
Write the result of the addition from register C into memory cell #14.
Program 1-3: Program to add two numbers from memory using an address stored in a memory cell.
The first instruction in Program 1-3 loads the number 12 from memory
cell #11 into register D. The second instruction then uses the content of D
(which is the value 12) as a memory address in order to load register A into
memory location #12.
But why go to the trouble of storing memory addresses in memory cells
and then loading the addresses from main memory into the registers before
they’re finally ready to be used to access memory again? Isn’t this an overly
complicated way to do things?
Actually, these capabilities are designed to make programmers’ lives
easier, because when used with the
Delilah Devlin
Maureen Carter
Jennifer Rose
Denise Swanson
Alaya Dawn Johnson
Kim Devereux
Stuart M. Kaminsky
Jeffrey Toobin
Tracy Falbe
A. M. Hudson