Wolmer’s Trust High School for Girls
INFORMATION TECHNOLOGY (GENERAL)
Grade 11 Teacher: Mrs. McCallum-Rodney
HOW MACHINE INSTRUCTIONS ARE REPRESENTED IN A COMPUTER
INTRODUCTION TO MACHINE INSTRUCTION
· Machine code or machine language is a system of instructions and data directly executed by a computer's central processing unit.
· Users and programmers of computers usually don't think about the millions of tiny electronic operations that go on each second. The situation is (very roughly) similar to when you are driving your car. You think about the "big operations" it can perform, such as "accelerate", "turn left", "brake", and so on. You don't think about the valves in your engine opening and closing 24,000 times per minute.
Each tiny electronic operation that a processor can perform is called a machine operation. A processor (a "machine") performs these one at a time, but millions of them in a second.
· A machine instruction consists of several bytes in memory that tells the processor to perform one machine operation. The processor looks at machine instructions in main memory one after another, and performs one machine operation for each machine instruction. The collection of machine instructions in main memory is called a machine language program or (more commonly) an executable program.
Example of Machine Language
Let us say that an electric toothbrush has a processor and main memory. The processor can rotate the bristles left and right, and can check the on/off switch. The machine instructions are one byte long, and correspond to the following machine operations:
Machine Instruction |
Machine Operation |
0000 0000 |
Stop |
0000 0001 |
Rotate bristles left |
0000 0010 |
Rotate bristles right |
0000 0100 |
Go back to start of program |
0000 1000 |
Skip next instruction if switch is off |
Here is the main memory for the toothbrush system:
Address |
Machine Instruction |
0 |
|
1 |
|
2 |
|
3 |
|
4 |
|
The toothbrush is wired so that when the switch is turned on, the instructions are performed one at a time, in order, starting at address zero. The program can be re-started at address zero with the instruction 0000 0100. The toothbrush is supposed to rotate the bristles left and right while the switch is on, and stop when the switch is off.
Machine Language Program
There are at least two sensible programs for the toothbrush. Here is one program:
Address |
Machine Instruction |
Meaning |
0 |
0000 0001 |
Rotate bristles left |
1 |
0000 0010 |
Rotate bristles right |
2 |
0000 1000 |
Skip next instruction if switch is off |
3 |
0000 0100 |
Go back to start of program |
4 |
0000 0000 |
Stop running |
In the electronic memory of the toothbrush the program is stored as a sequence of bits:
0000 0001 0000 0010 0000 1000 0000 0100 0000 0000
The processor starts at the beginning and performs the action described by each code. This is, of course, a stupid example. Electric toothbrushes are not controlled by computer processors. And, the machine instructions of actual processors are much more detailed. But the essential ideas of the example are these:
· A machine language program is a sequence of machine language instructions in main memory.
· A machine instruction consists of one or more bytes (in the example, only one.)
· The processor runs a program one machine instruction at a time.
· All the little machine operations add up to something useful.
If the toothbrush user leaves the switch "on" for a while, the program repeats its operations many times. This is how most programs in real computers run--many little operations add up to a useful function, which is then repeated many times.
Executing Instructions
The word "execute" is often used to mean "perform the machine operation that an instruction asks for." So you can say that "executing the instruction 0000 0000 stops the toothbrush," or "millions of instructions execute per second." "Execute" is also used for an entire program or part of a program: "to execute the program, turn the switch to on."
Most machine language programs are made up of instructions that are executed again and again. In the toothbrush program there was an instruction that caused the processor to start the program again from the beginning. In a real computer, millions of instructions execute per second, so something like this is necessary if the 32 megabytes of a typical computer are to hold a program that runs for more than a few seconds.
A group of machine instructions that can be repeatedly executed is called a loop.
A typical processor is made up of several million transistors, all on one small wafer of silicon called an integrated circuit (also called a chip.) The toothbrush processor could probably be built with just a few hundred transistors. Integrated circuits are used for other electronic parts of a computer: for example main memory is implemented with memory chips.
OPERATION CODE
Sometimes referred to as op code, operation code is code that instructs the computer on what task it should perform.
In computer technology, an opcode (operation code) is the portion of a machine language instruction that specifies the operation to be performed. Their specification and format are laid out in the instruction set architecture of the processor in question (which may be a general CPU or a more specialized processing unit). Apart from the opcode itself, an instruction normally also has one or more specifiers for operands (i.e. data) on which the operation should act, although some operations may have implicit operands, or none at all.
REGISTER
This is a special, high-speed storage area within the CPU. All data must be represented in a register before it can be processed. For example, if two numbers are to be multiplied, both numbers must be in registers, and the result is also placed in a register. (The register can contain the address of a memory location where data is stored rather than the actual data itself.)
The number of registers that a CPU has and the size of each (number of bits) help determine the power and speed of a CPU. For example a 32-bit CPU is one in which each register is 32 bits wide. Therefore, each CPU instruction can manipulate 32 bits of data. The registers on Pentium chips, for example, are 32 bits.
Usually, the movement of data in and out of registers is completely transparent to users, and even to programmers. Only assembly language programs can manipulate registers. In high-level languages, the compiler is responsible for translating high-level operations into low-level operations that access registers.
Example 1
A typical instruction to add two numbers might be ADD A, B, C
, which adds the values found in memory locations A and B, and then puts the result in memory location C. In a pipelined processor the pipeline controller would break this into a series of tasks similar to:
LOAD A, R1
LOAD B, R2
ADD R1, R2, R3
STORE R3, C
LOAD next instruction
The locations 'R1' and 'R2' are registers in the CPU. The values stored in memory locations labeled 'A' and 'B' are loaded (copied) into these registers, then added, and the result is stored in a memory location labeled 'C'.
What is an Accumulator?
In a computer CPU, an accumulator is register in which intermediate arithmetic and logic results are stored. Without a register like an accumulator, it would be necessary to write the result of each calculation (addition, multiplication, shift, etc.) to main memory, perhaps only to be read right back again for use in the next operation. Access to main memory is slower than access to a register like the accumulator because the technology used for the large main memory is slower (but cheaper) than that used for a register.
The example for accumulator use is summing a list of numbers. The accumulator is initially set to zero, then each number in turn is added to the value in the accumulator. Only when all numbers have been added is the result held in the accumulator written to main memory or to another, non-accumulator, CPU register.
Just a few of the instructions are, for example (with some modern interpretation):
· Clear accumulator and add number from memory location x
· Clear accumulator and subtract number from memory location x
· Add number copied from memory location x to the contents of the accumulator
· Subtract number copied from memory location x from the contents of the accumulator
Class Activity/Assignment – Past Paper 2002; Ques 4(e)
MEMORY ADDRESS
All programs are stored in main memory when they are executed. All data referenced by those programs are also stored in main memory so that they can be accessed. Main memory can be seen as a big chunk of space divided into groups of 8, 16, or 32 bits. Each byte or word of memory has a corresponding address, which is simply an integer that uniquely identifies that particular part of memory. The first memory address is 0.
In a multiprogramming environment, multiple programs (and their data) are stored in main memory at the same time. Thus operating systems must employ techniques to:
o track where and how a program resides in memory
o convert logical program addresses into actual memory address
A program is filled with references to variables and to other parts of the program code. When the program is compiled, these references are changed into the addresses in memory where the data and code reside.
But since we don’t know exactly where in memory the program will be loaded into main memory, how can we know which address to use for anything?
The solution is to use two kinds of addresses: logical and physical addresses.
A Logical Address (sometimes called virtual or relative address) is a value that specifies a generic location, relative to the program but not relative of main memory.
A Physical Address is an actual address in the main memory.
ADDRESSING MODES
Most of the instructions must refer to the address or content of a specific memory location. These so-called memory reference instructions must somehow identify the address of the location as part of the instruction encoding. The manner in which this target address or effective address is identified within the instructions is called the addressing mode.
The two common addressing modes used in microprocessor are direct and indirect addressing.
DIRECT ADDRESSING
· Direct addressing is where the address specified in the instruction is the address of the location to be used.
· Absolute (Direct) Address is a fixed address in memory. The term absolute distinguishes it from a relative address, which indicates a location by specifying a distance from another location. Absolute addresses are also called real addresses and machine addresses.
· When the instruction explicitly states the location of an operand or a destination (either in memory or in a processor register), the addressing mode is known as direct addressing. The effective address itself is included in the subsequent words of the instruction (post-word).
· Two sub-classifications within the direct addressing mode are often recognised.
o When the location is in memory, the mode may be referred to as absolute addressing.
o When the location is a processor register it may be referred to as register direct addressing.
· The figure below represents both direct addressing modes.
In part (a) the instruction specifies the address of the operand; in part (b) the instruction specifies the register containing the operand.
Direct addressing is used when the memory address or the selected register is to be fixed in the program.
INDIRECT ADDRESSING
· Indirect address is an address that serves as a reference point instead of the address to the direct location. An address specified by indicating its distance from another address, called the base address. For example, a relative address might be B+15, B being the base address and 15 the distance (called the offset).
· In the indirect addressing mode the instruction tells the processor neither the address of the operand nor the operand itself. Instead, it tells the processor where to go to find the address of a location in memory or the name of a processor register, but the binary number which is found there is not the operand. Instead, it is the effective address, the address of a location in memory to which the processor must go to find the operand. The result is that the processor must take one extra step in order to locate the operand.
· The op-word for the instruction includes a group of bits which identifies this mode of addressing, and the indirect address is specified in one or more additional post-words.
· If the instruction names a processor register as the source of the effective address, then the register identification number may fit into the op-word itself.
· Indirect address is used when a program must operate upon different data values under different circumstances.
· Consider the following figure: