Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

ECE 3620

Introduction to Microcomputers
© 2005 Prof. Mohamad Hassoun
WSU/ECE

Unit # 1
Microcomputer Execution of Assembly Programs: A Brief Overview

Objectives: After completing this unit you should be able to:


1. Define the following terms: Computer, processor, microprocessor, memory, memory address, memory content, hardware, software, bit, nibble, byte, word, mnemonic, opcode, instruction, operation, operand, address bus, data bus, program counter, memory address register, memory
data register, instruction register, ALU, accumulator, and addressing mode.
2. Convert between the following data representations: decimal, binary and hexadecimal.
3. Explain the differences between the immediate, direct and inherent addressing modes.
4. Disassemble machine code into mnemonic assembly language instructions.
5. Explain the microprocessor instruction execution cycle.
6. Employ the HC11 Simulator to enter, assemble, and disassemble programs.
7. Write simple programs (few instructions in length) to perform basic load/store operations.

This Unit presents an overview of the internal workings of a microprocessor and the way it interacts with memory as it executes a simple low-level program (machine code). This module covers the following material.

· The fundamental computer


· Data bus, address bus and memory
· The representation of operations and data inside a microcomputer: Base-2 numbers, operation codes
· A convenient representation of opcodes and data: The hexadecimal code
· Some basic microprocessor instructions
· Machine code of a simple assembly program
· Machine code inside a microcomputer
· Microprocessor internal registers
· The instruction execution cycle
· An Excel-based microprocessor system simulator
o Simulator
o Tutorial movie

· Exercises
· Laboratory experiments

The Fundamental Computer


A computer is made up of hardware and software. The hardware primarily consists of a processor [also called the central processing unit (CPU)] interconnected to memory. Memory is where software (programs and data) is stored. The CPU is connected to memory by means of a set of
signal lines (conducting wires) referred to as a bus system. The common bus system consists of a data bus, address bus, and control bus.

A microprocessor is a processor packaged in a single integrated circuit.


A microcomputer is a computer that uses a microprocessor as its CPU.

Data Bus, Address Bus and Memory


The basic unit of information in a computer is called a bit. A single bit can assume one of two values: 0 or 1. Therefore, all information inside a computer is represented by binary codes. The CPU is capable of interpreting/manipulating several bits at the same time. The microprocessor we
consider in this course uses data in 8-bit groups and is referred to as an 8-bit microprocessor.

Groups of bits of a particular size have special names:

4 bits 1 nibble
(hexadecimal digit)
8 bits 1 byte = 2 nibbles
16 bits 1 word = 2 bytes

In an 8-bit microprocessor, all data (numbers, letters, instructions, and other information) are broken down into chunks of 1-byte each. For example, the decimal number 1 is represented as 00000001. Such data would normally be stored in the memory of the microcomputer system, but can
also be loaded into one of the internal microprocessor registers for processing. Therefore, it is assumed that all memory storage locations are designed to hold, precisely, 1-byte each. Also, the data bus consists of a bundle of 8 conducting wires and is designed to move one full byte at a time.

The microprocessor can fetch (load) data from memory. This requires the microprocessor to signal the memory with the precise address of the data byte it is looking for. The microprocessor system we describe here has a 1-word (16 bits) address bus. This allows the microprocessor to
specify any one of the 216 (65,536) possible memory locations. The lowest memory location has the decimal address “0” and the highest possible memory location has the decimal address “65,535.”

The microprocessor utilizes two special internal registers [a program counter (PC) and a memory address register (MAR)] that help it point to the specific memory location it is interested in. These registers are 2-byte registers. For instance, if the microprocessor is about to read the contents
of the second memory location (designated by the decimal address “1”), then the MAR content would be set to 0000000000000001. We use the notation MAR = 0000000000000001 to refer to the contents of MAR. On the other hand, the notation (0000000000000001) refers to the content of
the memory location whose address is represented by the binary string “0000 0000 0000 0001” (note: displaying a binary string as shown in the above line makes it easier to read).

Reading from Memory


The following figure illustrates how the microprocessor reads the contents of the second memory location. After loading the 2-byte address onto the address bus, the microprocessor tells the memory to send a copy of the contents of that location. The content of memory (00111010 in this
example) is loaded onto the data bus, which in turn deposit its contents into the data register inside the microprocessor.

The Representation of Operations and Data Inside a Microcomputer


So far we have seen that all information inside a microprocessor system is represented using 1-byte binary strings stored in memory, and 1 or 2-byte strings inside the microprocessor. Normally, numbers, letters and instructions have 1-byte representations. On the other hand, memory
addresses are represented using 1-word (2-bytes). In the following sections, proper binary codes for representing decimal numbers and instructions are introduced. Also, the hexadecimal code is introduced as an efficient code for binary data representation.

Representation of Numbers: Base-2


The following table lists the base-2 codes for the integers 0-255. Note that the largest decimal value that can be represented inside a 1-byte register (or memory location) is 255 (or 28 – 1).

Consider a 1-byte number and refer to its bits as:

b7b6b5b4b3b2b1b0

Here, b0 is referred to as the least significant bit (LSB). Similarly, b7 is referred to as the most significant bit (MSB). The subscript is called the bit number. The bit number can be used to generate the decimal equivalent of the corresponding bit. For example, if b3 = 1, then its decimal value
is 1*23 = 1*8 = 8. In general, the decimal equivalent of bi is bi*2i .

In order to eliminate confusion, the subscripts “2” and “10” are used in the above example to indicate base-2 (binary) and base-10 (decimal) representations, respectively.

In the context of data saved in 1-byte memory location or in a 1-byte microprocessor register, the integer 19 is represented as “0001 0011.”

What is the 2-byte representation of the memory address 3,85510? Repeat for 49,15210?

Representation of Program Instructions: Opcodes

A computer program (in assembly language) is made up of a series of instructions. Every instruction consists of an operation and (sometime) an operand. For example, the assembly instruction to load the integer 8 into accumulator A (ACCA) is “LDAA #8.” ACCA is a special 1-byte
working register located inside the microprocessor. Here, the abbreviation or mnemonic “LDAA” stands for the instruction “load 1-byte into accumulator A.” The operand #8 represents the data to be loaded. It has two parts: “8” represents a decimal number, and “#” indicates that the value
to be loaded is the designated number. (As we will see later, “#” implies that the immediate addressing mode is to be used. This is one of several possible addressing modes that are supported).

Since all information inside a computer must be converted into binary bytes, how does one represent operations? The answer is that the designers of the microprocessor have already chosen a set of special binary codes (called opcodes) to represent operations. Most operations utilize a 1-byte
code. For instance, the binary code “1000 0110” is the opcode for the LDAA operation. The complete machine code for the instruction “LDAA #8” are the two consecutive bytes “1000 0110” and “0000 1000.” Note that the machine code for the integer “8” is just the base-2 representation of
“8.” On the other hand, the opcode byte “1000 0110” is determined by a predefined binary code that has nothing to do with the base-2 representation.

When the “LDAA #8” instruction is saved in memory (as part of a user program), it is actually the corresponding machine code is what gets saved. In this case, the instruction would occupy two consecutive bytes of memory.

A Convenient Representation of Opcodes and Data: The Hexadecimal Code


The binary representations considered above can become very cumbersome even for short programs. The microcomputer has no difficulty interpreting and manipulating the binary machine code of a program consisting of several hundred instructions. On the other hand, such a task would be
very daunting for the student or programmer. Next, the hexadecimal code is introduced and is shown to be a relatively efficient code for representing machine language.

Based on the above, it can be easily shown that the hexadecimal representation of the instruction “LDAA #8 is given by the two bytes: 86 08.

Example: The memory address 0000 0001 0100 10002 has the hex representation: 014816. We may also use the hex notation $0148 to represent our answer. This answer can be broken down to two bytes: The high-byte is $01 and the low-byte is $48.

Find the hex representation of the memory address 3,85510? Repeat for 49,15210 and 65,53510. Also, identify the high and low bytes of each hex address.

Some Basic Microprocessor Instructions


The following table lists a few microprocessor operations along with their hex opcodes.

Mnemonic Operation Description Opcode


LDAA Load accumulator A 1-byte à ACCA 86
LDAB Load accumulator B 1-byte à ACCB C6
INCA Increment ACCA ACCA + 1 à ACCA 4C
ABA Add accumulators ACCA + ACCB à 1B
ACCA
STAA Store ACCA in memory ACCA à Memory 97*
STAB Store ACCB in memory ACCB à Memory D7*
WAI Wait/Halt 3E

* These opcodes assume that the memory location has a low address which is located in the range $0000-$00FF. Here, the high-byte is always $00.

Machine Code of a Simple Assembly Program

Consider the Assembly Language program:

LDAA #1
LDAB #4
ABA
STAA $0000
WAI

This program loads ACCA with decimal 1 and ACCB with decimal 4. The prefix # indicates that the operand is itself the data to be loaded into the accumulator (i.e., it indicates that the instruction is utilizing the immediate addressing mode). The third operation adds the contents of the
accumulators and saves the result in ACCA. The fourth instruction saves the contents of ACCA (i.e., 5) in memory at the hex address $0000 (i.e., at the first memory location). The prefix “$” indicates that the operand is expressed in hexadecimal code. Since the address is assumed to be 2-
bytes long, a 4-digit hex operand is generally required. Finally, WAI halts the execution; the microprocessor does not proceed to the next instruction. The following table lists the prefix for number representation in assembly code.

Base Prefix
Hexadecimal $
Binary %
Decimal (nothing)

Employing the table of opcodes presented earlier one can generate the hex machine code (i.e., assemble) for the above simple program as shown below.

Assembly Code Machine code (in hex)


LDAA #1 86 01
LDAB #4 C6 04
ABA 1B
STAA $0000 97 00
WAI 3E

Note how the first two instructions resulted in two bytes each (this is a property of the immediate addressing mode). The third instruction only required a single byte because there is no need for an operand (here, the operand is implied or inherent). The same applies for the last instruction.
The STAA instructions would normally require three bytes of storage: one for the opcode and two for the address operand. However, this instruction only required two bytes of machine code since the address has a $00 high byte; the microprocessor would know to add $00 upon decoding the
opcode “$97”. This is a property of direct addressing, and it leads to faster execution. The total number of memory bytes required by the above program is eight.

Machine Code Inside a Microcomputer

Another way of listing the machine code for the above example is
86 LDAA #1
01
C6 LDAB #4
04
1B ABA
97 STAA $0000
00
3E WAI

In fact, the left hand side column in the above table represents the way the machine code is organized (in consecutive locations) in memory upon downloading it to the microprocessor system. However, the actual code in memory is binary and not hex (for us, the hex code is more convenient
to use). Assuming that the program is saved in memory starting at the address $C000, then the actual contents of program memory would be

Address Content (hex)


$C000 86
$C001 01
$C002 C6
$C003 04
$C004 1B
$C005 97
$C006 00
$C007 3E

Here, ($C000) = $86, ($C001) = $01, ($C002) = $C6, etc.

Microprocessor Internal registers

The microprocessor is the control and processing center in a computer. It consists of a control unit, arithmetic and logic unit (ALU) and registers. The control unit is responsible for decoding or interpreting the opcodes of instruction. It also provides timing and controls of data flow. The
ALU is where arithmetic and logic operations are carried out.

As noted earlier in this Unit, the microprocessor’s registers are needed for handling and manipulating data and addresses. We have already introduced some of these registers earlier: 1-byte working registers (ACCA & ACCB), a 2-byte program counter (PC), a 2-byte memory address register
(MAR), and a 1-byte memory data register (MDR). A typical microprocessor has additional registers:

· Instruction Register (IR): A 1-byte register that holds the instruction opcode so that it can be decoded (interpreted) by the control unit.
· Condition Code (CC) Register: A 1-byte register that keeps track of certain properties (e.g., carry, negative result, zero result, overflow, etc.) of the result of the last instruction executed.
· Stack Pointer (SP): A 2-byte register that establishes a memory stack.
· Index Register (XR): A 2-byte register that is essential for indexed addressing.

The following is a snapshot block diagram of a basic microprocessor (Motorola M6800). It shows that the content of both accumulators is $00, a program counter pointing to the address ($C000) of the next byte of program to be fetched from memory, and a condition code register that has
all flags reset (set to zero).

The Instruction Execution Cycle


A microprocessor executes a program stored in memory one instruction at a time. The microprocessor needs to load all machine code bytes associated with a given instruction from memory before it can execute that particular instruction. The complete process can be broken down into the
following steps: Fetch opcode, decode opcode, fetch additional bytes (addresses and/or data), and then execute the instruction. In the following, the simple addition example considered earlier will be used to illustrate the way a microprocessor executes instructions.

Address Content (hex)


$C000 86
$C001 01
$C002 C6
$C003 04
$C004 1B
$C005 97
$C006 00
$C007 3E

The above table shows the machine code of a simple program that starts at memory location $C000. Before execution can start, the starting address of the program must be placed in the program counter (i.e., PC = $C000). The step-by-step execution of the first program instruction LDAA
#1 and the last instruction STAA $00 are described next.

Step 1
a. The contents of the PC are sent to the memory address register (MAR).

b. The microprocessor provides this address to the memory (via the address bus) and read back (via the data bus) its content, $86.

c. Because this is an instruction fetch (why? Because the microprocessor is designed to interpret the first byte of code it fetches as an opcode), the opcode is placed in the instruction register (IR).

d. The control unit has an instruction decoder that decodes 86 as the opcode for a “load accumulator A from the next memory location” instruction. Here, the microprocessor increments its PC.

Step 2
a. The PC has been incremented in Step 1. Its new content, C001 is sent to the MAR.

b. The microprocessor reads $01 (via the data bus) into the MDR. The instruction is completed by copying the MDR content into ACCA.

c. The PC is incremented to $C002.

d. The contents (flags) of the condition code registers are also updated to reflect the nature of the number loaded in ACCA (this will be discussed later).

It took two read cycles in order to complete the execution of the LDAA instruction (in immediate addressing mode). At this point, the PC has the address of the opcode of the next instruction and the microprocessor is ready to execute such instruction.

The following diagram depicts the contents of the microprocessor registers at the instant just after completing the ABA instruction.

The following set of diagrams are snapshots of the microprocessor state as it handles the STAA $00 instruction.

a. Opcode is fetched and decoded and PC incremented.

b. Lower byte of address is read and placed in a temporary register.

c. The opcode $97 also instructs the microprocessor to insert a $00 as the high byte in the holding register. This 2-byte data is then copied into the MAR.

d. The contents of ACCA are copied into MDR and then transported by the data bus and saved in memory at the address $0000 pointed to by the MAR. Now, the processor is ready to execute the instruction starting at the address pointed to by the just incremented PC.

An Excel-Based Microprocessor System Simulator

My student, Dave Conger, has written a user-friendly microprocessor simulator. He started it as a project in ECE2620 (fall 04) and then completed it in the winter/spring of 2005 (project was funded by a WSU grant). The simulator runs under Microsoft’s Excel. The simulator has a built-in
assembler and disassembler.

Download the simulator


Watch a movie tutorial for the simulator

Exercises

Laboratory Experiments

You might also like