Introduction To Motorola 68HC11: 1.1 Objectives
Introduction To Motorola 68HC11: 1.1 Objectives
Introduction To Motorola 68HC11: 1.1 Objectives
1
Introduction to Motorola 68HC11
1.1 Objectives
After completing this chapter you should be able to define or explain the following terms: computer, processor, microprocessor, microcontroller, hardware, software, cross assembler, cross compiler, RAM, SRAM, ROM, EPROM, EEPROM, flash memory, byte, nibble, bus, KB, MB, mnemonic, opcode, and operand explain the differences between the immediate, direct, extended, indexed, relative, and inherent addressing modes disassemble machine code into mnemonic assembly language instructions explain the 68HC11 instruction execution cycles
Chapter 1
1.2
What is a Computer?
A computer is made up of hardware and software. The computer hardware consists of four main components: (1) a processor, which serves as the computers brain, (2) an input unit, through which programs and data can be entered into the computer, (3) an output unit, on which computational results can be displayed, and (4) memory, in which the computer software programs and data are stored. Figure 1.1 shows a simple block diagram of a computer. The processor communicates with memory and input/output (I/O) devices through a set of signal lines referred to as a bus. The common bus actually consists of three buses: a data bus, an address bus, and a control bus.
1.2
What is a Computer?
Control Unit
Common Bus
Data Storage
Output Units
Input Units
Computer Organization
W H AT I S A M I C R O P R O C E S S O R ?
The processor in a very large computer is built from a number of integrated circuits. A microprocessor is a processor packaged in a single integrated circuit. A microcomputer is a computer that uses a microprocessor as its CPU. Early microcomputers are quite simple and slow. However, many of todays desktop microcomputers have become very sophisticated and are even faster than many large computers manufactured only a few years ago. Microprocessors come in 4-bit, 8-bit, 16-bit, 32-bit, and 64-bit models. The number of bits refers to the number of binary digits that the microprocessor can manipulate in one operation. A 4-bit microprocessor, for example, is capable of manipulating 4 bits of information in one operation. Four-bit microprocessors are used for the electronic control of relatively simple machines. Some pocket calculators, for example, contain 4-bit microprocessors. The access of main memory takes much longer than the period of the CPU control clock signal. To enhance the CPU performance, many 32-bit and 64-bit microprocessors contain on-chip high-speed cache memory. The cache memory has a copy of the most recently accessed instructions and data. A cache memory is much smaller than the main memory. However, due to the fact that program execution demonstrates locality behavior in its references to instructions and data (CPU tends to access a small area of the memory in a short period of time), a relatively small cache can achieve very high hit ratio. Since a cache memory can satisfy the memory requests from the CPU most of the time, adding cache memory on the CPU chip improves the processor performance dramatically. Because processors and input/output have very different characteristics and speeds, peripheral chips are required to interface I/O devices to the
Chapter 1
microprocessor. For example, the integrated circuit M6821 is designed to interface a parallel device such as a printer or seven-segment display to the Motorola M6800 8-bit microprocessors. Microprocessors have been widely used since their invention. It is not exaggerating to say that the invention of microprocessors has revolutionized the electronics industry. However, the following limitations of microprocessors led to the invention of microcontrollers: A microprocessor requires external memory to execute programs. A microprocessor cannot directly interface to I/O devices; peripheral chips are needed. Glue logic (such as address decoders and buffers) is needed to interconnect external memory and peripheral interface chips to the microprocessor. Because of these limitations, a microprocessor-based design cannot be made as small as might be desirable. The invention of microcontrollers not only eliminated most of these problems but also simplified the hardware design of microprocessor-based products.
W H AT I S A M I C R O C O N T R O L L E R ?
A microcontroller is a computer implemented on a single very large scale integration (VLSI) chip. A microcontroller contains everything contained in a microprocessor along with one or more of the following components: memory timer analog-to-digital converter digital-to-analog converter direct memory access (DMA) controller parallel I/O interface (often called a parallel port) serial I/O interface memory component interface circuitry The Motorola 68HC11 is an 8-bit microcontroller family developed in 1985. The 68HC11 microcontroller family has more than fifty members, and the number is still increasing. The microcontrollers in this family differ mainly in the size of their on-chip memories and in their I/O capabilities. The characteristics of different memory technologies will be discussed shortly. As shown in Figure 1.2, the 68HC11A8 has the following features: 256 bytes on-chip static random access memory (SRAM) 512 bytes on-chip electrically-erasable, programmable read-only memory (EEPROM) 8-KB on-chip read-only memory (ROM)
1.2
What is a Computer?
three input-capture functions (ICi, i 1, 2, 3) five output-compare functions (OCi, i 1,..., 5) an 8-bit pulse accumulator circuit a serial communication interface (SCI) an 8-channel, 8-bit analog-to-digital converter a serial peripheral interface (SPI) a real-time interrupt (RTI) circuit a computer operating properly (COP) watchdog system These functions will be discussed in detail in subsequent chapters.
A P P L I C AT I O N S O F M I C R O C O N T R O L L E R S
Since their introduction microcontrollers have been used in every application that we can imagine. They are used as controllers for displays, printers, keyboards, modems, charge card phones, and home appliances such as refrigerators, washing machines, and microwave ovens. They are also used to control the operation of automobile engines and machines in factories. Today, most homes have one or more microcontroller-controlled appliances.
1.2.2 Memory
Memory is where software programs and data are stored. A computer may contain semiconductor, magnetic, and/or optical memory. Only semiconductor memory will be discussed in this book. Semiconductor memory can be further classified into two major types: random-access memory (RAM) and read-only memory (ROM).
Chapter 1
ROM-8KB
PULSE ACCUMULATOR O C 1
RAM-256 bytes
PERIODIC INTERRUPT
PORT A
EEPROM-512 bytes
PE7 PE6 PE5 PE4 PE3 PE2 PE1 PE0 VREFH VREFL RESET XIRQ IRQ
(VPPBULK )
COP WATCHDOG
DATA DIRECTION
DATA DIRECTION C
OSCILLATOR
PORT B
PORT C
P P P P P P P P S S C C C C C C C C T T 7 6 5 4 3 2 1 0 R R B A
PARALLEL I/O
MODE SELECT
SINGLE CHIP
VDD VSS
POWER
I
A A A A A A A A 1 1 1 1 1 1 9 8 5 4 3 2 1 0
A A A A A A A A D D D D D D D D 7 6 5 4 3 2 1 0
R/W AS
EXPAND
Figure 1.2
RAM is mainly used to store dynamic programs and data. A computer user often wants to run different programs on the same computer, and these programs usually operate on different sets of data. The programs and data must therefore be loaded into RAM from hard disk or other secondary storage, and for this reason they are called dynamic.
1.2
What is a Computer?
R E A D - O N LY M E M O R Y
ROM is nonvolatile. If power is removed from ROM and then reapplied, the original data will still be there. However, as its name implies, ROM data can only be read. If the processor attempts to write data to a ROM location, ROM will not accept the data, and the data in the addressed ROM memory location will not be changed. There are many different kinds of ROM technologies in use today: Masked-programmed read-only memory (MROM) is a type of ROM that is programmed when it is manufactured. The semiconductor manufacturer places binary data in the memory according to the request of the customer. To be cost-effective, many thousands of MROM memory chips, each containing a copy of the same data (or program), must be sold. Programmable read-only memory (PROM) is a type of read-only memory that can be programmed in the field (often by the end user) using a device called a PROM programmer or a PROM burner. Once a PROM has been programmed, its contents cannot be changed. PROMs are fuse-based; i.e., end users program the fuses to configure the contents of the memory. Erasable programmable read-only memory (EPROM) is a type of read-only memory that can be erased by subjecting it to strong ultraviolet light. The circuit design of EPROM requires us to erase the contents of a location before we can write a new value into it. A quartz window on top of the EPROM integrated circuit permits ultraviolet light to be shone directly on the silicon chip inside. Once the chip is programmed, the window can be covered with dark tape to prevent gradual erasure of the data. If no window is provided, the EPROM chip becomes one-time programmable (OTP) only. Many microcontrollers incorporate on-chip one-time programmable EPROM. EPROM is often used in prototype computers, where the software may be revised many times until it is perfected. EPROM does not allow erasure of the contents of an individual location. The only way to make change is to erase the entire EPROM chip and reprogram it. The programming of an EPROM chip is done electrically by using a device called an EPROM programmer. Today, most programmers are universal in the sense that they can program many different types of devices including EPROM, EEPROM, flash memory, and programmable logic devices. Electrically erasable programmable read-only memory (EEPROM) is a type of nonvolatile memory that can be erased by electrical signals and reprogrammed. Like EPROM, the circuit design of EEPROM also requires us to erase the contents of a memory location before we can write a new value into it. EEPROM allows each individual location to be erased and reprogrammed. Unlike EPROM, EEPROM can be erased and programmed using the same programmer. However, EEPROM pays the price for being so flexible in its erasability. The cost of an EEPROM chip is much higher than that of an EPROM chip of comparable density. Flash memory was invented to incorporate the advantages and avoid the drawbacks of both EPROM and EEPROM technologies. Flash memory can be erased and reprogrammed in the system without using a dedicated programmer. It achieves the density of EPROM, but it does not require a window for
Chapter 1
erasure. Like EEPROM, flash memory can be programmed and erased electrically. However, it does not allow the erasure of an individual memory locationthe user can only erase the entire chip. Today, more and more microcontrollers are incorporating on-chip flash memory for storing programs and static data.
1.3
1.3
Assembly language was invented to simplify the programming job. An assembly program consists of assembly instructions. An assembly instruction is the mnemonic representation of a machine instruction. For example, in the 68HC11: ABA stands for add the contents of accumulator B to accumulator A. The corresponding machine instruction is 00011011. DECA stands for decrement the contents of accumulator A by 1. The corresponding machine instruction is 01001010. A programmer no longer needs to scan through the 0s and 1s in order to identify what instructions are in the program. This is a significant improvement over machine language programming. The assembly program that the programmer enters is called source program or source code. A software program called an assembler is then invoked to translate the program written in assembly language into machine instructions. The output of the assembly process is called object code. It is a common practice to use a cross assembler to assemble assembly programs. A cross assembler is an assembler that runs on one computer but generates machine instructions that will be executed by another computer that has a different instruction set. In contrast, a native assembler runs on a computer and generates machine instructions to be executed by machines that have the same instruction set. The Motorola freeware as11 is a cross assembler that runs on an IBM PC or Apple Macintosh and generates machine code that can be downloaded into a 68HC11-based computer for execution. There are several drawbacks to programming in assembly language: The programmer must be very familiar with the hardware organization of the computer on which the program is to be executed. A program (especially a long one) written in assembly language is extremely difficult to understand for anyone other than the author. Programming productivity is not satisfactory for large programming projects because the programmer needs to work on the program logic at a very low level. For these reasons, high-level languages such as Fortran, PASCAL, C, C , and Java were invented to avoid the problems of assembly language programming. High-level languages are very close to plain English and hence a program written in a high-level language becomes easier to understand. A statement in high-level language often needs to be implemented by tens of assembly instructions. The programmer can now work on the program logic at a much higher level, which makes the programming job much easier. A program written in a high-level language is also called a source program, and it requires a software program called a compiler to translate it into machine instructions. A compiler compiles a program into object code. Just as there are cross assemblers, there are
10
Chapter 1
cross compilers that run on one computer but translate programs into machine instructions to be executed on a computer with a different instruction set. High-level languages are not perfect, either. One of the major problems with high-level languages is that the machine code compiled from a program written in a high-level language cannot run as fast as its equivalent in the assembly language. For this reason, many time-critical programs are still written in assembly language. C language has been used extensively in microcontroller programming in the industry, which can be proved by the fact that most microcontroller software tool developers provide C cross compilers. Both C and assembly language will be used throughout this text. The C programs in this text are compiled by the Imagecraft C cross-compiler and tested on Motorola and Axiom evaluation boards. A text editor is used to develop a program using a computer. A text editor allows the user to type, modify, and save the program source code in a text file.
1.4
1.4
11
Accumulator B 0 A:B 0 D 0 IX 0 IY 0 SP 0 PC H I N Z V C CCR Carry Overflow Zero Negative I interrupt mask Half-Carry (from bit 3) X Interrupt Mask Stop Disable
7 15 15 15 15 15
Accumulator A
0 7
Double Accumulator D Index Register IX Index Register IY Stack Pointer Program Counter S X
Figure 1.3
MC68HC11 programmers model Condition code register (CCR). This 8-bit register is used to keep track of the program execution status, control the execution of conditional branch instructions, and enable/disable the interrupt handling. The contents of the CCR register are shown in Figure 1.3. The function of each condition code bit will be explained in later sections and chapters.
Figure 1.4
12
Chapter 1
1.5
Memory Addressing
Memory consists of a sequence of directly addressable locations. A memory location is referred to as an information unit. A memory location can be used to store data, instruction, the status of peripheral devices, etc. An information unit has two components: its address and its contents, shown in Figure 1.5. Address Figure 1.5
I
Contents
Each location in memory has an address that must be supplied before its contents can be accessed. The CPU communicates with memory by first identifying the locations address and then passing this address on the address bus. This is similar to the fact that a mailman needs an address in order to deliver a letter. The data are transferred between memory and the CPU along the data bus (see Figure 1.6). The number of bits that can be transferred on the data bus at one time is called the data bus width of the processor.
Memory
The 68HC11 has an 8-bit data bus and can access only one memory byte at a time. The 68HC11 has an address bus of 16 signal lines and can address up to 216 (65636) different locations. The accessible memory addresses are in the range from 000016 to FFFF16. The size of memory is measured in bytes. Each byte has 8 bits. A 4-bit quantity is called a nibble. To simplify the quantification of memory, the unit kilobyte (KB) is often used. K is given by the following formula: K = 1024 = 210 Another frequently used unit is megabyte (MB), which is given by the following formula: M = K2 = 1024 x 1024 = 1048576
1.6
13
1.6
Prefix % @ (nothing)* $
Table 1.1
14
Chapter 1
1.6
15
1.6.3
In the extended addressing mode, the effective address of the operand appears explicitly in the two bytes following the opcode: LDAA $1003 loads the 8-bit value stored at the memory location with effective address $1003 into accumulator A. LDX $1000 loads the 16-bit value stored at the memory locations with the effective addresses $1000 and $1001 into the index register X. The byte at $1000 will be loaded into the upper byte of X and the byte at $1001 will be loaded into the lower byte of X. ADDD $1030 adds the 16-bit value stored at the memory locations with the effective addresses $1030 and $1031 to double accumulator D.
16
Chapter 1
operand while the offset byte of the instruction remains unchanged. Using the index addressing mode also shortens the instruction by one byte if the effective address is higher than the hex value $FF.
1.6.6
The relative addressing mode is used only for branch instructions. Branch instructions, other than the branching versions of the bit-manipulation instructions, generate two machine-code bytes, one for the opcode and one for the branch offset. The branch offset is the distance relative to the first byte of the instruction immediately following the branch instruction. The branch offset has a range of 128 to 127 bytes. When the branch is taken, the branch offset is added to the program counter to form the effective address. The source program specifies the destination of any branch instruction by its absolute address, given as either a numerical value or a symbol or expression that can be numerically evaluated by the assembler. In Figure 1.7, the 68HC11 will branch to execute the instruction DECB if the Z bit in the CCR register is 1 when the instruction BEQ $e164 is executed. A better way to specify the branch target is to use a symbolic label. Figure 1.8 is an improvement to the example in Figure 1.7. Address $e100 $e164 Opcode BEQ ADDA ... DECB ...
I
Figure 1.7
Example of this relative addressing mode. The opcode byte of the instruction DECB is $64 bytes away from the opcode byte of the instruction ADDA #10
1.7
17
Operand there #10
Address
there
Figure 1.8
1.7
18
Chapter 1
Instruction LDAA #10 LDAA $1000 LDAB #10 LDAB $1000 LDD #10 LDD $1000 LDS #255 LDS $1000 LDX #$1000 LDX $1000 LDY #1000 LDY $1000
Meaning Place the decimal value 10 (hex A) into accumulator A. Copy the contents of the memory location at $1000 into accumulator A. Place the decimal value 10 into accumulator B. Copy the contents of the memory location at $1000 into accumulator B. Place the decimal value 10 into double accumulator D. Copy the contents of the memory locations at $1000 & $1001 into the upper and lower bytes of double accumulator D, respectively. Place the decimal value 255 (hex FF) into the stack pointer SP. Copy the contents of the memory location at $1000 and $1001 into the upper and lower bytes of register SP, respectively. Place the hex value $1000 into index register X. Copy the contents of the memory locations at $1000 and $1001 into the upper and lower bytes of index register X, respectively. Place the decimal value 1000 (hex 3E8) into index register Y. Copy the contents of the memory locations at $1000 and $1001 into the upper and lower bytes of index register Y, respectively.
Addressing mode immediate extended immediate extended immediate extended immediate extended immediate extended immediate extended
Table 1.2
Example 1.1
M
Write an instruction to place the decimal value 1023 (or $3FF) into the stack pointer SP. The following instruction will place the decimal value 1023 into the stack pointer SP:
Solution:
LDS #1023 The binary representation of the decimal value 1023 is 111111111112. After the execution of this instruction, the 16-bit stack pointer SP contains the value of 00000011111111112. In order to unify the representation, we will use the following notations throughout this book: [reg]: refers to the contents of the register reg. reg can be any one of the following: A, B, D, X, Y, SP, or PC. [addr]: refers to the contents of the memory location at address addr. mem[addr]: refers to the memory location at address addr.
1.7
19
Example 1.2
M
Write an instruction to load the contents of the memory locations at $0000 and $0001 into double accumulator D. Initially, the contents of D and the memory locations at $0000 and $0001 are $1010, $20, and $30, respectively. Show the new values in these registers after the execution of the instruction. To load two consecutive memory bytes into the double accumulator D, we need to specify only the address of the most significant byte, that is, $0000, in this example. The instruction is:
Solution:
LDD $0000 The contents of D and memory locations $0000 and $0001 before and after execution of the instruction are as follows:
Before execution of LDD $0000 [D] = $1010 [$0000] = $20 [$0001] = $30
After execution of LDD $0000 [D] = $2030 [$0000] = $20 [$0001] = $30
L
1.7.2 The ADD Instruction
ADD is the generic name of a group of instructions that perform the addition operation. The ADD instruction is one of the most important arithmetic instructions in the 68HC11. The ADD instruction can have either two or three operands. In a three-operand ADD instruction, the C flag of the condition code register is always included as one of the source operands. Threeoperand ADD instructions are used mainly in multiprecision arithmetic, which will be discussed in chapter 2. The ADD instruction has the following constraints: The ADD instruction can specify at most one memory location as a source operand. The memory operand can be used only as a source operand. The destination operand must be a register (it can be A, B, X, Y, or D). The register specified as the destination operand must also be used as a source operand.
20
Chapter 1
For example, ADDA #20 adds the decimal value 20 (hex $14) to the contents of accumulator A and places the result in accumulator A. ADDA $40 adds the contents of the memory location at $40 to the contents of accumulator A and places the result in accumulator A. ADCA $00 adds the carry bit (in CCR) and the contents of the memory location at $00 to accumulator A and places the result in accumulator A. More examples of ADD instructions are given in Table 1.3. The ADD instructions that specify one of the index registers as the destination are mainly used in address calculation, not for general-purpose 16-bit addition. The instruction ABX adds the contents of accumulator B to the lower byte of the index register X. If there is a carry out, it will be added to the upper byte of the index register X. The instruction ABY is similar except that the destination is the index register Y.
Addressing mode inherent inherent inherent immediate direct immediate direct immediate direct immediate direct immediate direct
Instruction ABA ABX ABY ADCA #12 ADCA $20 ADCB #12 ADCB $20 ADDA #12 ADDA $20 ADDB #12 ADDB $20 ADDD #0012 ADDD $0020
Meaning Add accumulator B and accumulator A and store the sum in A. Add accumulator B and the index register X and store the sum in X. Add accumulator B and the index register Y and store the sum in Y. Add the decimal value 12 and the C flag in the CCR register to accumulator A and store the sum in A. Add the contents of the memory location at $20 and the carry flag in the CCR register to accumulator A and store the sum in A. Add the decimal value 12 and the C flag in the CCR register to accumulator B and store the sum in B. Add the contents of the memory location at $20 and the C flag in the CCR register to accumulator B and store the sum in B. Add the decimal value 12 to accumulator A and store the sum in A. Add the contents of the memory location at $20 to accumulator A and store the sum in A. Add the decimal value 12 to accumulator B and store the sum in B. Add the contents of the memory location at $20 to accumulator B and store the sum in B. Add the decimal value 12 to double accumulator D and store the sum in double accumulator D. Add the 16-bit value stored at memory locations $20 and $21 to double accumulator D and store the sum in double accumulator D.
Table 1.3
1.7
21
Example 1.3
M
Write an instruction sequence to add the contents of the memory locations at $10 and $20 and leave the sum in accumulator A. This problem can be solved by loading the contents of one of the memory locations into accumulator A and then adding the contents of the other memory location into accumulator A, as is done by the following instructions:
Solution:
LDAA $10 ADDA $20 The first instruction loads the contents of the memory location at $10 into accumulator A. The second instruction then adds the contents of the memory location at $20 to accumulator A.
1.7.3
SUB is the generic name of a group of instructions that perform the subtraction operation. Like the ADD instruction, the SUB instruction can have either two or three operands. The three-operand SUB instruction includes the C flag of the CCR register as one of the source operands. Three-operand SUB instructions are mainly used in multiprecision arithmetic. The SUB instruction has the following constraints: The SUB instruction can specify at most one memory location as a source operand. The memory operand can be used only as a source operand. The destination operand must be an accumulator (it can be either A, B, or D). The register specified as the destination operand is also used as a source operand. For example, SUBA #10 subtracts the decimal value 10 from accumulator A and leaves the difference in accumulator A. SUBB $10 subtracts the contents of the memory location at $10 from accumulator B and leaves the difference in accumulator B.
22
Example 1.4
Chapter 1
M
Write an instruction sequence to subtract the value of the memory location at $00 from that of the memory location at $30 and leave the difference in accumulator B. We need to load the contents of the memory location at $30 into accumulator B and then subtract the contents of the memory location at $00 directly from accumulator B. The appropriate instructions are:
Solution:
LDAB $30 SUBB $00 More examples of SUB instructions are given in Table 1.4.
Addressing mode inherent immediate direct immediate direct immediate direct immediate direct immediate extended
Instruction SBA SBCA #10 SBCA $20 SBCB #10 SBCB $20 SUBA #10 SUBA $20 SUBB #10 SUBB $20 SUBD #$0010 SUBD $0020
Meaning Subtract the value of accumulator B from accumulator A and store the difference in accumulator A. Subtract the decimal value 10 and the C bit of the CCR register from accumulator A and store the difference in A. Subtract the contents of the memory location at $20 and the C flag from accumulator A and store the difference in A. Subtract the decimal value 10 and the C bit of the CCR register from accumulator B and store the difference in B. Subtract the contents of the memory location at $20 and the C flag from accumulator B and store the difference in B. Subtract the decimal value 10 from accumulator A and store the difference in A. Subtract the contents of memory location at $20 from accumulator A and store the difference in A. Subtract the decimal value 10 from accumulator B and store the difference in B. Subtract the contents of memory location at $20 from accumulator B and store the difference in B. Subtract the hex value 10 from double accumulator D and store the difference in D. Subtract the 16-bit value stored at memory locations at $0020 and $0021 from double accumulator D and store the difference in D.
Table 1.4
1.7.4
STORE is the generic name of a group of instructions that store the contents of a register into a memory location or memory locations. The 68HC11 has six STORE instructions. The STORE instruction allows the contents of accumulator A, accumulator B, the stack pointer SP, index register X, or index register Y to be stored at one or two memory locations. The destination must be a memory location.
1.7
23
For example, STAA $10 stores the contents of accumulator A in the memory location at $10. STAB $10 stores the contents of accumulator B in the memory location at $10. STD $10 stores the upper and lower eight bits of double accumulator D in the memory locations at $10 and $11, respectively. STX $2000 stores the upper and lower eight bits of index register X in the memory locations at $2000 and $2001.
Example 1.5
M
Write an instruction sequence to add the contents of the memory locations at $00 and $01 and then store the sum in the memory location at $10.
Solution:
This problem can be solved in three steps: Step 1 Load the contents of the memory location at $00 into accumulator A. Step 2 Add the contents of the memory location at $01 to accumulator A. Step 3 Store the contents of accumulator A in the memory location at $10. The appropriate instructions are: LDAA ADDA STAA $00 $01 $10 ; load the contents of the memory location at $00 into A ; add the contents of the memory location at $01 to A ; store the sum in A at the memory location at $10
Example 1.6
M
Write an instruction sequence to swap the contents of the memory locations at $00 and $10. To swap, we need to load the contents of the memory at $00 and $10 into accumulator A and B, respectively, and then store the contents of A and
Solution:
24
Chapter 1
B in the memory locations at $10 and $00, respectively. The following instructions would be used: LDAA LDAB STAA STAB $00 $10 $10 $00 ; load the contents of $00 into A ; load the contents of $10 into B ; store A into $10 ; store B into $00
1.8
Assembly instructions LDAA #29 STAA $00 ADDA $02 STAA $01
Note that the decimal number 29 is equivalent to hex value $1D. Assume that these four instructions are stored in consecutive memory locations starting at $C000. Then the contents of the memory locations from $C000 to $C007 are as follows:
1.8
25
Machine code 86 1D 97 00 9B 02 97 01
If the memory locations at $02 contains $20, then the memory location at $01 is assigned the value $1D $20 $3D. Figure 1.9 shows the changes in the values stored in the memory locations and in accumulator A when the instructions are executed. (These values are represented in hex format.)
Changes in the contents of memory locations and accumulator A after program execution
Example 1.7
M
A segment of program machine code contains the following opcode and addressing information: 96 30 8B 07 97 30 96 31 8B 08 97 31
26
Chapter 1
Machine Code 01 86 96 C6 D6 CC DC 8B 9B CB DB C3 D3 97 D7 DD
Assembly Instruction Format NOP LDAA LDAA LDAB LDAB LDD LDD ADDA ADDA ADDB ADDB ADDD ADDD STAA STAB STD IMM DIR IMM DIR IMM DIR IMM DIR IMM DIR IMM DIR DIR DIR DIR
Note: 1. IMM is a one-byte immediate value for instructions that involve A or B, and it is a two-byte immediate value for instructions that involve D. 2. DIR stands for a one-byte direct address between $00 and $FF.
Table 1.5
Using the machine opcodes and corresponding assembly instructions in Table 1.5, decode the given machine code into assembly instructions. The process of decoding the machine language instruction begins with the opcode byte 96.
Solution:
a. The opcode byte 96 corresponds to the following LOAD instruction format: LDAA DIR To complete the decoding of this instruction, the byte that immediately follows 96 (that is, 30) should be included. Therefore, the machine code of the first instruction is 96 30. The corresponding assembly instruction is LDAA $30. b. The opcode of the second instruction is 8B, which corresponds to the following ADD instruction format: ADDA IMM To decode this instruction completely, the byte that immediately follows 8B (that is, 07) should be included. The machine code of
1.8
27
the second instruction is thus 8B 07. The corresponding assembly instruction is ADDA #07. c. The opcode of the third instruction is 97, which corresponds to the following STORE instruction format: STAA DIR
Including the byte that immediately follows 97 (that is, 30), we see that the machine code of the third instruction is 97 30. The corresponding assembly instruction is STAA $30. Continuing in this manner, we can decode the remaining machine code bytes into the following assembly instructions: LDAA ADDA STAA $31 #08 $31
A program that can disassemble machine code into assembly instructions is called a disassembler. A disassembler can be used to translate the machine code in ROM into assembly instructions.
28
Chapter 1
Machine code B6 20 00 BB 30 00 B7 20 00
Table 1.6
A sample of instructions
Address
$C000 $C001 $C002 $C003 $C004 $C005 $C006 $C007 $C008 After $C001
$20 $00
1.8
29
Step 3 The control unit recognizes that the LOAD instruction requires a twobyte value for the operand address. This is found in the two bytes immediately following the opcode byte (at locations $C001 and $C002). Therefore, two read cycles are executed, and the value of the PC is incremented by 2. The PC has a final value of $C003, and the address $2000 is stored in an internal register (invisible to the programmer) inside the CPU. Figure 1.11 shows the address read cycles. Step 4 The actual execution of the LOAD instruction requires an additional read cycle. The address $2000 is put on the address bus with a read request. The contents of memory location $2000 are placed on the data bus and stored in accumulator A, as shown in Figure 1.12.
Address Address bus $C000 $C001 $C002 $C003 $C004 $C005 $C006 $C007 $C008 $00 Data bus CPU $C002
Memory contents
Address
30
Chapter 1
Address
$2000
. . .
$37 $19 Data bus $3000
Figure 1.12
control unit recognizes that this version of the ADD instruction requires two more read cycles to fetch the extended address. These two read cycles are performed in the following two steps. Step 2 Fetch the upper extended address byte ($30) from the memory location at $C004. The PC is then incremented to $C005. Step 3 Fetch the lower extended address byte ($00) from the memory location at $C005. The PC is then incremented to $C006. Step 4 Execution of this instruction requires an additional read cycle to read in the operand at location $3000. The control unit places the value $3000 on the address bus to fetch the contents of memory location $3000. Step 5 The returned value $37 is added to accumulator A. The accumulator now has the value $50 ($19 $35 $50).
1.8
31
been fetched. The control unit recognizes that this version of the STORE instruction requires two more read cycles to fetch the extended address. These two read cycles are performed in the next two steps. Step 2 Fetch the upper extended address byte ($20) from memory location at $C007. The PC is then incremented to $C008. Step 3 Fetch the lower extended address byte ($00) from the memory location at $C008. The PC is then incremented to $C009. Step 4 The purpose of this instruction is to store the contents of accumulator A in memory, so the control unit places the extended address $2000 on the address bus, and the value in accumulator A ($50) is written into the memory location at $2000. Figure 1.13 shows the execution write cycle.
Address
CPU
$19
$2000
32
Chapter 1
1.9
Summary
A computer is made up of hardware and software. The computer hardware consists of four main components: (1) a processor (called CPU), (2) an input unit, (3) an output unit, and (4) memory. The processor can be further divided into three major parts: (1) registers, (2) arithmetic logic unit, and (3) the control unit. The system clock synchronizes the activities of the CPU. The clock rates of modern microprocessors can be as high as several hundred MHz. The control unit maintains a register called program counter (PC), which controls the memory address of the next instruction to be executed. During the execution of an instruction, the presence of overflow, an addition carry, a subtraction borrow, and so forth, are flagged by the system and stored in another register called the status register. A microprocessor is a processor fabricated on a single integrated circuit. A microcomputer is a computer that uses a microprocessor as its CPU. Although microprocessors have been widely used since their invention, there are several limitations that led to the invention of microcontrollers. First, a microprocessor requires external memory to execute programs. Second, a microprocessor cannot interface directly to I/O devices; peripheral chips are needed. Third, glue logic is needed to interconnect external memory and peripheral interface chips to the microprocessor. A microcontroller is a computer implemented on a single very large scale integration (VLSI) chip. It contains everything contained in a microprocessor along with one or more of the following components: memory timer analog-to-digital converter digital-to-analog converter direct memory access (DMA) controller parallel I/O interface serial I/O interface memory component interface circuitry Memory is where software programs and data are stored. Semiconductor memory chips can be classified into two major categories: random-access memory (RAM) and read-only memory (ROM). There are many different types of ROM. MROM is a type of ROM that is programmed when it is fabricated. PROM is a type of ROM that can be programmed in the field by the end user. EPROM is a type of ROM that is programmed electrically and erased by ultraviolet light. EEPROM is a type of ROM that can be programmed and erased electrically. EEPROM can be erased in one location, in one row, or in a bulk in one operation. Flash memory can
1.9
Summary
33
be erased and programmed electrically. However, flash memory can only be erased in bulk. Programs are known as software. A program is a set of instructions that the computer hardware can execute. Programmers write a program in some kind of programming language. Only machine language was available during the early days of computers. A machine language program consists of a sequence of machine instructions. A machine instruction is a combination of 0s and 1s that informs the CPU to perform some specific operation. Using machine language to write programs is very difficult and hence the assembly language was invented to improve the productivity of programmers. Programs written in assembly language consist of a sequence of assembly instructions. An assembly instruction is the mnemonic representation of some machine instruction. Programs written in assembly language are difficult to understand and programming productivity is not high. High-level languages such as FORTRAN, COBOL, PASCAL, C, C , and JAVA were invented to avoid the drawback of the assembly language. Programs written in assembly language or high-level languages are called source code. Source code must be translated before it can be executed. The translator of a program written in assembly language is called an assembler, whereas the translator of a program in a highlevel language is called a compiler. The 68HC11 has a few CPU registers, including accumulators A and B, the condition code register (CCR), index registers X and Y, the stack pointer SP, and the program counter (PC). A memory location has two components: its contents and its address. When accessing a memory location, the CPU sends out the address on the address bus and the memory component will place the requested value on the data bus. A 68HC11 instruction consists of one or two bytes of opcode and zero to three bytes of operand information. The operands of an instruction are specified by addressing modes. The 68HC11 provides the following addressing modes: immediate direct extended indexed relative A programmer may occasionally need to decode the machine instructions into assembly instructions for debugging or other purposes. The first step is to identify the format of the instruction that an opcode corresponds to. The second step is to identify the remaining operand bytes. The third step is to combine the opcode byte(s) and operand byte(s) into an instruction.
34
Chapter 1
The execution cycle of an instruction includes the following steps: Perform one or multiple read cycles to fetch the instruction. Perform the required read cycles to fetch the memory operands (optional). Perform the operation specified by the opcode. Write back the result to a register or a memory location (optional).
1.10 Exercises
E1.1 What is a processor? What sections of a computer make up a processor? E1.2 What makes a microprocessor different from the processors used in large computers? E1.3 What makes a microcontroller different from the microprocessor used in a PC? E1.4 How many bits of data are stored in each memory location of a microcontroller trainer built around the 68HC11 microcontroller? E1.5 How many different memory locations can the 68HC11 microcontroller address? E1.6 Why must every computer have some nonvolatile memory? E1.7 What are the differences between MROM, PROM, EPROM, EEPROM and flash memory? For what type of application is each most suitable? E1.8 What is the difference between source code and object code? E1.9 What register is used to keep track of the address of the next instruction to be executed? E1.10 Convert 5K, 13K, and 24K into decimal representation. E1.11 Write an instruction sequence to swap the contents of accumulators A and B. Hint: use a memory location as a swap buffer. E1.12 Write an instruction sequence to subtract 4 from memory locations $00 to $02. E1.13 Write an instruction sequence to add the contents of memory locations $10,$11,$12 and store the sum at memory location $15. E1.14 Write an instruction sequence to swap the contents of memory locations at $11 and $12. E1.15 Write an instruction sequence to place 33 at memory locations $11, $12, and $13. E1.16 Write an instruction sequence that performs the operations equivalent to those performed by the following high-level language statements: I := 10; J := 20; K := J I; Assume variables I, J, and K are located at $01, $04, and $10, respectively. E1.17 Translate the following assembly instructions into machine instructions using Table 1.5.
1.10
Exercises
35
E1.18 Translate the following assembly instructions into machine instructions using Table 1.5. LDAA LDAB STD LDAB STD #00 $01 $60 $03 $62
E1.19 Disassemble the following machine code into 68HC11 assembly instructions using Table 1.5. 96 40 8B F8 97 40 96 41 8B FA 97 41 E1.20 Disassemble the following machine code into 68HC11 assembly instructions using Table 1.5. DC 00 8B FE CB FD DD 00 E1.21 Determine the number of read and write cycles performed during the execution of the following instructions: LDX #$1000 LDS #$FF SUBA $2000 ADDD 0,Y E1.22 Determine the number of read and write cycles performed during the execution of the following instructions: LDAA $00 LDAB $01 STAB $00 LDAB $02 STAB $01 STAA $02 E1.23 Determine the contents of the memory locations at $00, $01, and $02 after the execution of the following instruction sequence, given that [$00] $11, [$01] 22, and [$02] 33. LDAA $02 ADDA $01 STAA $01 ADDA $00 STAA $00
36
Chapter 1
E1.24 Refer to Appendix B to find the execution time (in E clock cycles) of the following instructions. LDAA #$00 STAA $1000 LDAB 10,Y ADDA 9,X STD $2000 E1.25 Assume that the E clock signal frequency is 2 MHz. You are given the following instructions: LOOP PSHA PULA PSHA PULA DEX BNE
LOOP
Find 1. The period of the E clock signal in seconds. 2. The total execution time (in number of E clock cycles and in seconds) of these six instructions. 3. The number of times that these six instructions must be executed in order to create a time delay of one second.