Professional Documents
Culture Documents
PC144
PC144
Introduction to Microcomputers
© 2005 Prof. Mohamad Hassoun
WSU/ECE
Unit # 1
Microcomputer Execution of Assembly Programs: A Brief Overview
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.
· Exercises
· Laboratory experiments
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).
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?
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.
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.
* 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.
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.
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.
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
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 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.
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.
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.
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.
Exercises
Laboratory Experiments