Introduction To Motorola 68HC11: 1.1 Objectives

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

HUANG Ch01_001-036 3/28/00 12:38 PM Page 1

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

HUANG Ch01_001-036 3/28/00 12:38 PM Page 2

Chapter 1

Introduction to Motorola 68HC11

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.1 The Processor


The processor, which is also called the central processing unit (CPU), can be further divided into three major parts: Registers. A register is a storage location in the CPU. It is used to hold data and/or a memory address during execution of an instruction. Access to data in registers is faster than access to data in memory. Registers play an essential role in the efficient execution of programs. The number of registers varies greatly from computer to computer. Arithmetic logic unit. The arithmetic logic unit (ALU) is the computers numerical calculator and logical operation evaluator. The ALU receives data from main memory and/or registers, performs a computation, and, if necessary, writes the result back to main memory or registers. Todays fastest personal computer can perform hundreds of millions of additions in one second. Control unit. The control unit contains the hardware instruction logic. The control unit decodes and monitors the execution of instructions. The control unit also acts as an arbiter as various portions of the computer system compete for the resources of the CPU. The activities of the CPU are synchronized by the system clock. All CPU activities are measured by clock cycles. The clock rates of modern microprocessors have reached more than 800 MHz by the time of this writing, where 1 MHz = 1 million ticks (or cycles) per second The period of a 1 MHz clock signal is 1 s (10-6 second). The control unit also maintains a register called a 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 a status register. The resultant flags are then used by the programmer for program control and decision-making.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 3

1.2

What is a Computer?

Control Unit

Common Bus

Arithmetic Logic Unit

Memory Registers Program Storage Figure 1.1


I

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

HUANG Ch01_001-036 3/28/00 12:38 PM Page 4

Chapter 1

Introduction to Motorola 68HC11

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)

HUANG Ch01_001-036 3/28/00 12:38 PM Page 5

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).

R ANDOM-A CCESS M EMORY


Random-access memory is volatile in the sense that it cannot retain data in the absence of power. RAM is also called read/write memory because it allows the processor to read from and write into it. Both read and write accesses to a RAM chip take roughly the same amount of time. The microprocessor can temporarily store or write data into RAM, and it can later read that data back. Reading memory is nondestructive to the contents of the memory location. Writing memory is destructive. When the microprocessor writes data to memory, the old data is written over and destroyed. There are two types of RAM technologies: static RAM (SRAM) and dynamic RAM (DRAM). Static RAM uses from four to six transistors to store one bit of information. As long as power is stable, the information stored in the SRAM will not be degraded. Dynamic RAM uses one transistor and one capacitor to store one bit of information. The information is stored in the capacitor in the form of an electric charge. The charge stored in the capacitor will leak away over time, so a periodic refresh operation is needed to maintain the contents of DRAM.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 6

Chapter 1

Introduction to Motorola 68HC11

ROM-8KB

PULSE ACCUMULATOR O C 1

PAI OC2 OC3 OC4 OC5 IC1 IC2 IC3 SS SCK

PA7 PA6 PA5 PA4 PA3 PA2 PA1 PA0 PD5

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

SPI PORT E A/D CONVERTER SCI

MOSI MISO TxD RxD

DATA DIRECTION

PD4 PD3 PORT D PD2 PD1 PD0

M68HC11 CPU ADDRESS DATA BUS INTERRUPTS HANDSHAKE I/O

XTAL EXTAL E MODA LIR MODB (V STBY ) P P P P P P P P B B B B B B B B 7 6 5 4 3 2 1 0

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

68HC11A8 block diagram (redrawn with permission of Motorola)

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.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 7

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

HUANG Ch01_001-036 3/28/00 12:38 PM Page 8

Chapter 1

Introduction to Motorola 68HC11

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

The Computers Software


A computer is useful because it can execute programs. Programs are known as software. A program is a set of instructions that the computer hardware can execute. The program is stored in the computers memory in the form of binary numbers called machine instructions. For example, the 68HC11 machine instruction 0001 1011 adds the contents of accumulator A and accumulator B together and leaves the sum in accumulator A. 10000110 00000001 places the value 1 in accumulator A. 00111101 multiplies the values in accumulators A and B and leaves the product in accumulators A and B. Several tasks are very difficult in developing programs in machine language: 1. Program writing. The programmer will need to memorize the binary pattern of each machine instruction, which can be very challenging because a microprocessor may have several hundred different machine instructions, and each machine instruction may have different length. Constant table lookup will be necessary if the programmer cannot memorize every binary pattern. On the other hand, programmers are forced to work on program logic at a very low level because every machine instruction implements only a very primitive operation. 2. Program debugging. Whenever there are errors, it is extremely difficult to trace the program because the program consists of only sequences of 0s and 1s. A programmer will need to identify each machine instruction and then think about what operation is performed by that instruction. This is not an easy task. 3. Program maintenance. Most programs will need to be maintained in the long run. A programmer who did not write the program will have a hard time reading the program and following the program logic.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 9

1.3

The Computers Software

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

HUANG Ch01_001-036 3/28/00 12:38 PM Page 10

10

Chapter 1

Introduction to Motorola 68HC11

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

The 68HC11 CPU Registers


The 68HC11 microcontroller has many registers. These registers can be classified into two categories: CPU registers and I/O registers. CPU registers are used solely to perform general-purpose operations such as arithmetic, logic, and program flow control. I/O registers are mainly used to control the operations of I/O subsystems and record the status of I/O operations, etc. I/O registers are treated as memory locations when they are accessed. CPU registers do not occupy the 68HC11 memory space. The CPU registers of the 68HC11 are shown in Figure 1.3 and are listed below. Some of the registers are 8-bit and some are 16-bit. General-purpose accumulators A and B. Both A and B are 8-bit registers. Most arithmetic functions are performed on these two registers. These two accumulators can also be concatenated to form a single 16-bit accumulator that is referred to as the D accumulator. Index registers IX (or X) and IY (or Y). These two registers are used mainly in addressing memory operands. However, they are also used in several arithmetic operations. Stack pointer (SP). A stack is a first-in-last-out data structure. The 68HC11 has a 16-bit stack pointer that initially points to the location above the top element of the stack, as shown in Figure 1.4. The stack will be discussed in chapter 3. Program counter (PC). The address of the next instruction to be executed is specified by the 16-bit program counter. The 68HC11 fetches the instruction one byte at a time and increments the PC by 1 after fetching each instruction byte. After the execution of an instruction, the PC is incremented by the number of bytes of the executed instruction.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 11

1.4

The 68HC11 CPU Registers

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.

All of these registers are accessible to the programmer. SP Top element

Figure 1.4

68HC11 stack structure

HUANG Ch01_001-036 3/28/00 12:38 PM Page 12

12

Chapter 1

Introduction to Motorola 68HC11

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

The components of a memory location

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.

Address bus lines CPU Data bus lines Figure 1.6


I

Memory

Transferring data between CPU and 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

HUANG Ch01_001-036 3/28/00 12:38 PM Page 13

1.6

The 68HC11 Addressing Modes

13

1.6

The 68HC11 Addressing Modes


A 68HC11 instruction consists of one or two bytes of opcode and zero to three bytes of operand information. Instructions that use indexed addressing mode with Y as the index register have two bytes of opcode. All other instructions have only one byte of opcode. Addressing modes are used to specify the operands needed in an instruction. Six addressing modes are provided in the 68HC11: immediate, direct, extended, indexed (with either of two 16-bit index registers and an 8-bit offset), inherent, and relative. The immediate mode specifies an operand to be operated on. Direct, extended, and indexed modes are used to specify the address of a memory operand. The relative mode is used to specify the branch target for conditional branch instructions. Each of the addressing modes (except for immediate and inherent modes) results in an internally generated, double-byte value referred to as the effective address. This value appears on the address bus during the external memory reference portion of the instruction. The following paragraphs describe each of the addressing modes. In these descriptions, the effective address is used to specify the address of the memory location, from which the argument is fetched, at which the argument is to be stored, or from which execution is to proceed. Address and data values are represented in binary format inside the computer. However, a large binary number is not easy for a human being to deal with, so decimal and hexadecimal formats are often used instead. Octal numbers are also used in some cross assemblers. In this text, we will use a notation that adds a prefix to a number to indicate the base used in the number representation. The prefixes for binary, octal, decimal, and hexadecimal numbers are given in Table 1.1. This method is also used in Motorola microcontroller and microprocessor manuals. The word hexadecimal has a shorthand called hex. We will use hex instead of hexadecimal from now on.

Base binary octal decimal hexadecimal

Prefix % @ (nothing)* $

*Note: Some assemblers use &

Table 1.1

Prefix for number representation

HUANG Ch01_001-036 3/28/00 12:38 PM Page 14

14

Chapter 1

Introduction to Motorola 68HC11

1.6.1 Immediate (IMM)


In the immediate addressing mode, the actual argument is contained in the byte or bytes immediately following the instruction opcode. The number of bytes matches the size of the register. In assembly language syntax, an immediate value is preceded by a # character. The following instructions illustrate the immediate addressing mode: LDAA #22 loads the decimal value 22 into the accumulator A. ADDA #@32 adds the octal value 32 to accumulator A. LDAB #$17 loads the hex value 17 into accumulator B. LDX #$1000 loads the hex value 1000 into the index register X, where the upper byte of X receives the value of $10 and the lower byte of X gets the value of $00.

1.6.2 Direct Mode (DIR)


In the direct addressing mode, the least significant byte of the effective address of the instruction operand appears in the byte following the opcode. The high-order byte of the effective address is assumed to be $00 and is not included in the instruction. This limits use of the direct mode to operands in the $0000-$00FF area of the memory. Instructions using the direct mode execute one clock cycle faster than their counterparts using the extended mode. The following instructions illustrate the direct addressing mode: ADDA $00 adds the value stored at the memory location with the effective address $0000 to accumulator A. SUBA $20 subtracts the value stored at the memory location with the effective address $0020 from accumulator A. LDD $10 loads the contents of the memory locations at $0010 and $0011 into double accumulator D, where the contents of the memory location at $0010 are loaded into accumulator A and those of the memory location at $0011 are loaded into accumulator B.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 15

1.6

The 68HC11 Addressing Modes

15

1.6.3

Extended Mode (EXT)

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.

1.6.4 Indexed Mode (INDX, INDY)


In the indexed addressing mode, one of the index registers (X or Y) is used in calculating the effective address. Thus the effective address is variable and depends on the current contents of the index register X (or Y) and a fixed, 8-bit unsigned offset contained in the instruction. Because the offset byte is unsigned, only positive offsets in the range from 0 to 255 can be represented. If no offset is specified, the machine code will contain $00 in the offset byte. For example, ADDA 10,X adds the value stored at the memory location pointed to by the sum of 10 and the contents of the index register X to accumulator A. Each of the following instructions subtracts the value stored at the memory location pointed to by the contents of index register X from accumulator A: SUBA 0,X SUBA ,X SUBA X Please note that the third format is not acceptable to the Motorola as11 freeware assembler. You probably wonder why the index addressing mode is useful. Programs often need to change the address part of an instruction as the program runs. The index addressing mode requires part of the address to be placed in the index register in the microprocessor. The contents of the index register can be changed by the program, thus changing the effective address of the instruction

HUANG Ch01_001-036 3/28/00 12:38 PM Page 16

16

Chapter 1

Introduction to Motorola 68HC11

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.5 Inherent Mode (INH)


In the inherent mode, everything needed to execute the instruction is encoded in the opcode. The operands are CPU registers and thus are not fetched from memory. These instructions are usually one or two bytes. ABA adds the contents of accumulator B to accumulator A. INCB increments the value of accumulator B by 1. INX increments the value of the index register X by 1.

1.6.6

Relative Mode (REL)

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

Operand $e164 #10 $64 bytes

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

HUANG Ch01_001-036 3/28/00 12:38 PM Page 17

1.7

A Sample of 68HC11 Instructions

17
Operand there #10

Address

Opcode BEQ ADDA ... DECB ...


I

there

Figure 1.8

Using a label to specify the branch target

1.7

A Sample of 68HC11 Instructions


A 68HC11 instruction consists of one or two bytes of opcode and zero to three bytes of operand information. The opcode specifies the operation to be performed. A 68HC11 instruction can have from zero to three operands. One of the operands is used both as a source and as the destination of the operation. The operand information is represented by one of the addressing modes.

1.7.1 The LOAD Instruction


LOAD is the generic name of a group of instructions that place a value or copy the contents of a memory location (or memory locations) into a register. Most 68HC11 arithmetic and logical instructions include a register as one of the operands. Before a meaningful operation can be performed, a value must be placed in the register. The LOAD instruction places or copies a value from a memory location into a register. The 68HC11 has LOAD instructions to load values into accumulator A, accumulator B, double accumulator D, stack pointer SP, index register X, and index register Y. For example, the following instruction loads the decimal value 10 into accumulator A: LDAA #10 where, the # character indicates that the value that follows (that is, 10) is to be placed into accumulator A. The following instruction copies the contents of the memory location at $1000 into accumulator A: LDAA $1000 A more extensive sample of LOAD instructions is given in Table 1.2. When two consecutive memory bytes are loaded into a 16-bit register, the contents of the memory location at the lower address are loaded into the upper half of the register, while the contents of the memory location at the higher address are loaded into the lower half of the register. When the contents of a 16-bit register are saved in the memory, the upper byte of the register is saved at the lower address while the lower byte is saved at the higher address.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 18

18

Chapter 1

Introduction to Motorola 68HC11

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

A sample of LOAD instructions

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.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 19

1.7

A Sample of 68HC11 Instructions

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.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 20

20

Chapter 1

Introduction to Motorola 68HC11

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

A sample of ADD instructions.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 21

1.7

A Sample of 68HC11 Instructions

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

The SUB Instruction

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.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 22

22
Example 1.4

Chapter 1

Introduction to Motorola 68HC11

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

A sample of SUB instructions

1.7.4

The STORE Instruction

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.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 23

1.7

A Sample of 68HC11 Instructions

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:

HUANG Ch01_001-036 3/28/00 12:38 PM Page 24

24

Chapter 1

Introduction to Motorola 68HC11

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

The 68HC11 Machine Code


We have learned that each 68HC11 instruction consists of one to two bytes of opcode and zero to three bytes of operand information. In this section, we will look at the machine codes of a sample of 68HC11 instructions, the disassembly of machine instructions, and instruction execution timing.

1.8.1 A Machine Code Sequence


The basic assembly language or machine code instructions can be sequenced to perform calculations, as we have seen in previous examples. Consider the following high-level language statements: I := 29 L := I + M Assume that the variables I, L, and M refer to memory locations $00, $01, $02, respectively. The first statement assigns the value 29 to variable I, and the second statement assigns the sum of variables I and M to variable L. The highlevel language statements translate to the following equivalent assembly language and machine instructions:

Assembly instructions LDAA #29 STAA $00 ADDA $02 STAA $01

Machine instructions (in hexadecimal format) 86 1D 97 00 9B 02 97 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:

HUANG Ch01_001-036 3/28/00 12:38 PM Page 25

1.8

The 68HC11 Machine Code

25
Machine code 86 1D 97 00 9B 02 97 01

Address $C000 $C001 $C002 $C003 $C004 $C005 $C006 $C007

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.)

1.8.2 Decoding Machine Language Instructions


The process of decoding (disassembling) a machine language instruction is more difficult than assembling it. The opcode is the first one or two bytes of an instruction. By decomposing its bit pattern, the assembly instruction mnemonic and the addressing mode of the operand can be identified. To illustrate the disassembling process, we will use Table 1.5. In Table 1.5, the left column shows the machine code byte while the right column shows the instruction format corresponding to the machine code byte. Before program execution ??? ??? 20 ???
I

Address $00 $01 $02 A Figure 1.9

After program execution 1D 3D 20 3D

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

HUANG Ch01_001-036 3/28/00 12:38 PM Page 26

26

Chapter 1

Introduction to Motorola 68HC11

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

Machine opcodes and their corresponding assembly instructions

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

HUANG Ch01_001-036 3/28/00 12:38 PM Page 27

1.8

The 68HC11 Machine Code

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.

1.8.3 The Instruction Execution Cycle


In order to execute a program, the microprocessor or microcontroller must access memory to fetch instructions or operands. The process of accessing a memory location is called a read cycle, the process of storing a value in a memory location is called a write cycle, and the process of executing an instruction is called an instruction execution cycle. When executing an instruction, the 68HC11 performs a combination of the following operations: one or multiple read cycles to fetch instruction opcode byte(s) and addressing information one or two read cycles required to fetch the memory operand(s) (optional) the operation specified by the opcode one or two write cycles to write back the result to either a register or a memory location (optional) We will illustrate the instruction execution cycle using the LOAD, ADD, and STORE instructions shown in Table 1.6. The details of the data transfer on the buses are included to illustrate the read/write cycles. Assume the program counter PC is set at $C000, the starting address for the machine instructions. The contents of the memory locations at $2000 and $3000 are $19 and $37, respectively.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 28

28

Chapter 1

Introduction to Motorola 68HC11

Assembly language instructions LDAA ADDA STAA $2000 $3000 $2000


I

Memory location address $C000 $C003 $C006

Machine code B6 20 00 BB 30 00 B7 20 00

Table 1.6

A sample of instructions

Instruction 1 LDAA $2000


Execution of this instruction involves the following steps: Step 1 The value in PC ($C000) is placed on the address bus with a request to read the contents of that location. Step 2 The eight-bit value at the location $C000 is the instruction opcode byte $B6. This value is placed on the data bus by the memory hardware and returned to the processor, where the control unit begins interpretation of the instruction. On the read cycle, the control unit causes the PC to be incremented by 1, so it now points to location $C001. Figure 1.10 shows the opcode read cycle. Memory contents Address bus $C000 $B6 $20 CPU $00 $BB $30 $00 $B7 $B6 Data bus Before $C000 Figure 1.10
I

Address

$C000 $C001 $C002 $C003 $C004 $C005 $C006 $C007 $C008 After $C001

$20 $00

PC PC Instruction 1opcode read cycle

HUANG Ch01_001-036 3/28/00 12:38 PM Page 29

1.8

The 68HC11 Machine Code

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.

Instruction 2 ADDA $3000


The PC initially has the value of $C003. Three read bus cycles are required to fetch the second instruction from memory. The execution cycle for this instruction involves the following steps: Step 1 Fetch the opcode byte at $C003. At the end of this read cycle, the PC is incremented to $C004 and the opcode byte $BB has been fetched. The Memory contents Address bus $C001 $B6 $20 $00 CPU $BB $30 $00 $20 Data bus $B7 $20 $00 Before $C001 Figure 1.11
I

Address Address bus $C000 $C001 $C002 $C003 $C004 $C005 $C006 $C007 $C008 $00 Data bus CPU $C002

Memory contents

Address

$B6 $20 $00 $BB $30 $00 $B7 $20 $00

$C000 $C001 $C002 $C003 $C004 $C005 $C006 $C007 $C008

After first read $C002

After second read $C003 PC

PC PC Instruction 1address byte read cycles

HUANG Ch01_001-036 3/28/00 12:38 PM Page 30

30

Chapter 1

Introduction to Motorola 68HC11

Memory contents Address bus $2000 $19 CPU

Address

$2000

. . .
$37 $19 Data bus $3000

Figure 1.12

Instruction 1execution read cycles

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).

Instruction 3 STAA $2000


As was the case with the previous two instructions, three read cycles are required to fetch this instruction from memory. The PC initially has the value $C006. The execution cycle for this instruction involves the following steps: Step 1 Fetch the opcode byte at the location $C006. At the end of the read cycle, the PC is incremented to $C007 and the opcode byte $B7 has

HUANG Ch01_001-036 3/28/00 12:38 PM Page 31

1.8

The 68HC11 Machine Code

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.

1.8.4 Instruction Timing


The 68HC11 Reference Manual lists the CPU execution time for every 68HC11 instruction (in number of E clock cycles). The 68HC11 internal operations and external read and write cycles are controlled by the E clock signal. The E clock signal is derived by dividing the external crystal oscillator output signal by 4.

Memory contents Address bus $2000

Address

CPU

$50 Data bus Figure 1.13


I

$19

$2000

Instruction 3execution write cycle

HUANG Ch01_001-036 3/28/00 12:38 PM Page 32

32

Chapter 1

Introduction to Motorola 68HC11

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

HUANG Ch01_001-036 3/28/00 12:38 PM Page 33

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.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 34

34

Chapter 1

Introduction to Motorola 68HC11

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.

HUANG Ch01_001-036 3/28/00 12:38 PM Page 35

1.10

Exercises

35

LDAA ADDA LDAB STD

#$20 $10 #$90 $00

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

HUANG Ch01_001-036 3/28/00 12:38 PM Page 36

36

Chapter 1

Introduction to Motorola 68HC11

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.

You might also like