Computer Science 161.01

Professor Valente

 

Wednesday, September 25th             

 

Writing VSC-32 Programs That Do Arithmetic!

 

To this point, our VSC-32 programs (such as Echo) have performed only input and output.

We now take the next step and try to write programs that do arithmetic.

Any program that does arithmetic will have to make use of the CPU’s Arithmetic Logic Unit.

In particular, a VSC-32 program that does arithmetic will make use of a special location within the

CPU called the Accumulator.

 

Check out the VSC-32 web site and see if you can locate the Accumulator.

It is abbreviated “Accum”.

 

The Accumulator is where all the “action” takes place in an arithmetic problem.

 

For example, if the program needs to add the number at location 7 to the number at location 8,

it will do so in these steps:

1.      LOAD the accumulator with the number at location 7.  (00100111)

2.      ADD to the accumulator the number at location 8.         (10101000)

 

At this point, the accumulator will have the sum of the two numbers sitting in it.

The program will probably want to STORE that sum somewhere in memory, say at location 9.

It will do so by

3.  STORE the accumulator’s value into location 9.            (01001001)

 

Thus, LOAD loads a number from memory into the accumulator.

But, STORE stores the number currently in the accumulator into memory.

 

So,  LOAD’s direction is  from Main Memory to CPU.

And STORE’s direction is from CPU into Main Memory.

 

A Programming Example

 

The first program I would suggest might be called EchoPlus1. 

 

That is, this program should read in some number and write to the screen that number plus 1.

So, if the number 23 is entered, the output value written to the screen is 24.

 

Let’s think carefully of the steps that are required.

 

  1. READ some number into memory.
  2. LOAD that number into the accumulator.
  3. ADD the number 1 to the accumulator.
  4. STORE the sum into memory.
  5. WRITE the sum from memory to the screen.
  6. STOP

 

This suggests 6 instructions which will occupy addresses 0 through 5.

We will need a data value of 1 after that, say, at address 6, our first available slot.

The program can then use address 7 for the number and 8 for the sum.

 

The above 6 steps are can now be made more specific:

 

1.      READ into location 7.

2.      LOAD from location 7.

3.      ADD from location 6 (adds the 1 located there!)

4.      STORE into location 8.

5.      WRITE what’s at location 8.

6.      STOP

 

Let’s encode these 6 instructions in binary, followed by 00000001 for the 1:

 

            01100111

            00100111

            10100110

            01001000

            10001000

            00000000

            00000001

 

Enter these also at the VSC-32 web site , press LOAD AND GO,

and see if the EchoPlus1 program does what we hoped!

 

Of course, entering binary is an error-prone process, and we might wish to

remember that we have this wonderful shorthand known as hexadecimal.

 

For example, the 8 instruction program for EchoPlus1 could be written as

6727A648880001

That’s it!  (Can you believe that 6727A648880001 is actually a program?)

 

It would be SO GREAT if we could enter that as a program, and have a new,

improved VSC-32 automatically expand it into binary.

 

We can do all of this!!!

 

Let’s try it out:  simply copy 6727A648880001 into the space provided

for Hex Input at this web site then press the Expand Hex button and

see this wonder occur!

 

 

EXERCISES

 

  1. A VSC-32 program to read in two numbers, sum them, and write the result. 

Run the program at our web site. 

  1. A VSC-32 program to read in two numbers, compute the first plus twice the second, and write the result.

      Run the program at our web site.

  1. A VSC-32 program to read in two numbers x and y, compute (x + y) – 5, and write the result.

Run the program at our web site. 

Finally, once it is working, convert your program to hex and use this web site to expand, then

Load and Go!

  1. Determine by hand what this VSC-32 program does:  25C6478700A00F .

See if you were correct by entering it at this web site , expanding, and running!