Homework 1
Processor Architecture
Assembly Language

Modified

Download

Example programs and link libraries for Visual Studio 2010

Programs and libraries for previous Visual Studio versions, see:

http://kipirvine.com/asm/examples/index.htm

 

Assignment 1 - Assembly Language Program

Use the machines in LF105 or LF111.

The text provides an introduction and example of the basic steps required to complete course programming assignments.

  1. Open http://kipirvine.com/asm/gettingStartedVS2010/index.htm
     
  2. Skip the first part, start at: Next: Install the Book's Example Programs
     
  3. The author has provided software that should be downloaded twice.
     
    1. Download and extract to C:\Irvine
    2. Download and extract to a thumb drive or to C:\Users\username


    Programs and libraries for previous Visual Studio versions, see:

    http://kipirvine.com/asm/examples/index.htm

     

  4. The author's instructions assume all example files are copied to C:\Irvine folder. Use the folder where you copied the files.
     
  5. In Opening a Project instructions section, open Examples\Project_sample\Project.sln from your folder.
     
  6. In Build the Project section, when Building, if there is an error try:
    1. Add to Irvine32.inc path to the Irvine folder.
    2. Open Project | Project Properties
    3. Open Configuration Properties | Linker | General
    4. Additional Library Directories should include the Irvine folder.
       
  7. Complete the tutorial through Adding a File to a Project.

 

Turn In

  1. Cover sheet with your name, date, Homework 1, Assignment 1. Staple all pages together.

    Follow steps 1-5 in section: Running the Sample Program in Debug Mode.
     

  2. From Registers section, take a screen shot of Registers window.
     
  3. From Setting a Breakpoint section, take a screen shot of main.asm program with breakpoint set .
     
  4. Do Building and Running other Programs and Adding a File to a Project.

    Take a screen shot of Console window.

 

You should have a cover sheet and 3 screen shots.


Hypothetical Machine

We examine a simple, hypothetical (not real) machine to more easily understand machine operation.

The Hypothetical Machine has been implemented in Java and can be run in a browser by clicking Hypothetical Machine Simulator.

The hypothetical machine architecture described in class is simple enough to be implemented in software by a one page C++ program! The software merely simulates the operation of the hypothetical computer CPU which produces the same results as if the hypothetical CPU existed.

Simulating the hypothetical hardware in software is a truly powerful idea since programs can be written for the hypothetical machine but executed by simulation software on a real computer.  See the Hypothetical Machine in C++ at the end of this document for the simulator software. Software for new hardware is often implemented in this way while the hardware is still under development.

How does the simulator work? In fact, much like the hardware it simulates, both implement a Fetch/Execute cycle.

Suppose X = 35 and Y = 56, for the following:

 Z = X + Y

the corresponding Hypothetical Machine operations are:

LDA    Y           ; Load A (Accumulator) with value at address of Y

ADD    X           ; Add value at address of X to A

STA    Z           ; Store A to address of Z

The complete program, with 35 and 56 in memory for X and Y variables, would be as in Figure 1.

Figure 1

Address  Contents    Mnemonic     Comments 
000       01004       LDA Y         A = Y
001       20003       ADD X         A = A + X
002       02005       STA Z         Z = A
003       00035                     Variable X
004       00056                     Variable Y
005       00000                     Variable Z


NOTE

The MNEMONIC column entries correspond to the INSTRUCTION column, so mnemonic:

ADD X is stored as 20003 (ADD is code 20) at memory ADDRESS 001.

Variable Y address is 004 so that mnemonic LDA Y instruction code is 01004 (LDA is code 01 and Y is at address 004).

To run this program, the simulator executes the fetch/execute cycle for the hypothetical machine. The execution starts by fetching the LDA Y instruction and executing. The simulator program simply implements the hypothetical machine fetch/execute cycle as below:

ALGORITHM:

Simply stated, a hardware CPU runs a program by repeatedly FETCH'ING one instruction from memory and EXECUTE'ING that instruction in an endless cycle. This is called the FETCH/EXECUTE cycle.

To simulate the Hypothetical CPU hardware, we must implement this cycle as below.

1.   Fetch instruction at PC by:    MAR = PC
                                    MDR = Memory[ MAR ]
                                    IR  = MDR
2.   PC = PC + 1                    PC = next instruction address
3.   Decode instruction:            OP = IR operation field
                                    Addr = IR address field
4.   Execute operation OP.
5.   Go to 1.
DATA:
Notice that the CPU architecture (internal memory: PC, IR, A, B, MDR, MAR) and external memory are simulated using variables to hold each register's data. We accurately represent the hypothetical CPU internal memory by:
PC      3 digits, holds address in range 000 to 999 of next instruction.
IR      5 digits, holds instruction (2-digit operation and 3-digit operand).
A, B    5 digits, holds numerical value in range -99999 to 99999.
MAR     3 digits, holds address in range 000 to 999 of Memory index.
MDR     5 digits, holds numerical value accessed to/from Memory[ MAR ].
Memory  5 digits, array indexed from 000 to 999, holds values -99999 to 99999.


Assignment 2 - Hypothetical Machine Simulator

Machine language programs for the hypothetical machine can be executed using a Web-based simulator that executes programs as discussed above.

The simulator allows direct modification of memory and CPU registers, step-by-step execution of programs, and observation of internal CPU operations.

We will use the simulator to help understand how a CPU executes machine language programs by observing the step-by-step operations performed by the CPU.

The following program:

Loads the value (00035) from address 002.

Exchanges the contents of the A and B CPU registers.

Not much but enough to observe the execution of a small program.

Address Contents Operation
000
001
002
003
01002
03000
00035

LDA 002
XAB
 

Assignment 2 Questions

  1. For the program listed above:
    1. Run the Hypothetical Machine Simulator.
    2. Enter the three lines of the program Contents into Main Memory at the addresses listed.
    3. The CPU state is displayed below the Reset button. Record each CPU state (e.g. MDR <- Memory[MAR]) required to perform FETCH/EXECUTE of the instructions:
  2. What CPU state starts every FETCH.
  3. Which instruction executes faster? Why?
  4. What and where is the final result of executing the two instructions?
  5. A common CPU power rating is its operational clock speed, for example, an CPU operating at 1 GHz. roughly meaning that it can perform a state change (not execute an instruction) in one billionth of a second or 1,000,000,000 state changes per second. Assuming the Hypothetical Machine CPU is rated at 1 GHz., how long will it take to FETCH/EXECUTE the following instruction:

Turn In

  1. Cover sheet with your name, date, Homework 1, Assignment 2. Staple all pages together.
  2. Answers to Assignment 2 Questions.

Hypothetical Machine in C++ (Enrichment Only)

The following listing is a complete example C++ program of a simulator for the Hypothetical Machine of the text. It is given for enrichment only and is not to be turned in. To execute a Hypothetical Machine program using the simulator under DOS or UNIX operating systems use:

Hypothetical Machine Implementation in C++

//      Machine language interpreter for Hypothetical Machine architecture
#include <iomanip.h>
#include <stdlib.h>
typedef         long     WordSize;
typedef         int      AddressRange;
typedef         int      CodeSize;

WordSize        Memory[ 1000 ], IR, A, B, MDR;         // Machine architecture
AddressRange    PC, MAR;

void Interpret()        // Interpret program instructions stored in memory
{       WordSize        Temp;
        CodeSize        OP;
        AddressRange    Addr;
        cout << "PC  OP  Addr      A      B" << "\n";   A = 0;  B = 0;

        PC = 0;

        while (1)  {                                   // Forever
            MAR = PC;
            MDR = Memory[MAR];
            IR  = MDR;                                 // Fetch

            cout << setw(2) << PC;

            PC = PC + 1;                               // Increment PC

            OP   = IR / 1000;                          // Decode
            Addr = IR % 1000;

            cout << setw(4) << OP << setw(6) << Addr << setw(7) << A
                 << setw(7) << B << "\n";

            switch (OP) {                               // Execute OP
                 case 01 : MAR = Addr;                  // LDA Addr
                           MDR = Memory[ MAR ];
                           A   = MDR;
                           break;
                 case 02 : MAR = Addr;                  // STA Addr
                           MDR = A;
                           Memory[ MAR ] = MDR;
                           break;
                 case 03 : {
                                  Temp = A;             // XAB
                                  A    = B;
                                  B    = Temp;
                                  break;
                           }
                 case 04 : A = 0;                        // CLA
                           break;
                 case 20 : MAR = Addr;                   // ADD Addr
                           MDR = Memory[ MAR ];
                           A = A + MDR;
                           break;
                 case 21 : MAR = Addr;                   // SUB Addr
                           MDR = Memory[ MAR ];
                           A = A - MDR;
                           break;
                 case 30 : PC = Addr;                    // JMP
                           break;
                 case 99 : exit(0);                      // HLT
            }
      }
}
void Load()                                               // Load instructions into memory from file
{       char Line[80];
        PC = 0;
        cin >> dec >> IR;               cin.getline(Line, 80, '\n');
        while (cin) {
                Memory[ PC ] = IR;
                PC = PC + 1;
                cin >> dec >> IR;       cin.getline(Line, 80, '\n');
        }
}
void main()
{    Load();
     Interpret();
}