A computer is controlled by a program, which is composed of sequences of instructions. At the assembly language level, each instruction specifies one fundamental operation to be performed by the CPU. Instructions can be classified into six basic categories:
These conceptual categories do not necessarily map one-to-one onto any actual machine architecture. For instance, the Itanium architecture uses two very different instruction types for data movement: one type to move data between memory and registers and another type to move data among registers. Instructions are stored in memory, as are the data on which they operate. Depending on the architecture, an instruction in memory can occupy several consecutive words or bytes (Tables 1-3, 1-4, and 1-5). Each architecture has its own representation of instructions, but every instruction is typically composed of two basic components:
The processor fetches an instruction from memory, decodes the operation code, and executes the appropriate function on its operands. The location (memory address) of the next instruction to be executed is held in a special internal CPU register called the instruction pointer (IP) or the program counter (PC). When a program is first loaded into memory, the IP is initialized with the address of the first instruction of the program. Using the IP, the CPU fetches this instruction along with any operand specifiers. The operands themselves are then fetched, the operation is performed, and any results generated are stored back in memory or in a register. Figure 2-4 shows the complete cycle of fetching, interpreting, and decoding an instruction. Figure 2-4. Instruction executionWhenever an instruction is fetched, the IP is modified to point to the next instruction. In this way, the processor can readily execute instructions one after another in sequence. A few instruction types produce skip, branch, or jump effects; these are the machine language equivalents of GOTO in a language such as BASIC. When one of these instruction types is executed, it changes the contents of the IP in such a way that a new location, no longer the next one in sequence, will be taken as the information unit containing the "next" instruction to be executed. A complete computer program will consist of both instructions and data, with each instruction or data element loaded at a unique address in memory when the program runs. If we were to inspect arbitrary memory units, we would have great difficulty differentiating data from instructions because both are merely sequences of binary digits. Moreover, there may well be numerous sorts of data elements, such as integers, floating-point numbers, or character strings. How does the computer "know" that some of these numbers are machine instructions, while others are data? When a program is produced using a compiler or assembler and other system software, its starting address is made part of the stored form of the program. The loading process sets that address, which could be anywhere in memory, into the IP just before the program starts to run. Once this single master "pointer" is known, everything has a context: Instructions come along in sequence unless there are changes in the direction of program flow, and the operand specifiers within each instruction guide the processor towards finding and appropriately working with each of the data locations. |