Chapter 2.1
Chapter 2.1
1
Computer Arithmetic
•The ALU is that part of the computer that performs arithmetic and logic operations
on data.
2
Cont.……..
• All the other elements: registers, control unit, memory and I/O, are there mainly to
bring data into the ALU for it to process and then to take the results back out.
• Operands for arithmetic and logic operations are presented to the ALU in
registers and the results of an operation are stored in registers.
• These registers are temporary storage locations within the processor that are
connected by signal paths to the ALU.
Cont.…….
• The ALU may also set flags as the result of an operation.
• The flag values are also stored in registers within the processor.
• The processor provides signals that control the operation of the ALU and the
movement of the data into and out of the ALU.
Integer Representation
• In the binary number system, numbers can be represented with just the digits zero
and one, the minus sign (for negative numbers), and the period, or radix point (for
numbers with a fractional component).
• For purposes of computer storage and processing, we do not have the benefit of
special symbols for the sign and radix point.
• Only binary digits (0 and 1) may be used to represent numbers.
5
• In general, if an n-bit sequence of binary digits an-1an-2…a1a0 is
interpreted as an unsigned integer A, its value is:
• In general,
7
Instruction Sets: Characteristics
• What is an Instruction Set?
• The operation of the processor is determined by the instructions it executes,
referred to as machine instructions.
• The collection of different instructions that the processor can execute is referred to
as the processor’s instruction set.
Elements of a Machine Instruction
• Each instruction must contain the information required by the processor for
execution including:
8
Cont.………
• Operation code (opcode): a binary code that specifies the operation to be
performed (e.g., ADD, SUB).
• Source operand reference: the operation may involve one or more source
operands (operands that are inputs for the operation).
• Result operand reference: the operation may produce a result. This tells
where to put the result produced.
• Next instruction reference: this tells the processor where to fetch the next
instruction after the execution of this instruction is complete.
Source and result operands can be in one of four areas:
1. Main or virtual memory: as with next instruction references, the main or virtual memory
address must be supplied.
2. Processor register: with rare exceptions, a processor contains one or more registers that
may be referenced by machine instructions.
• If only one register exists, reference to it may be implicit. If more than one register
exists, then each register is assigned a unique name or number, and the instruction must
contain the number of the desired register.
3. Immediate: the value of the operand is contained in a field in the instruction being executed.
4. I/O device: the instruction must specify the I/O module and device for the operation. If
memory-mapped I/O is used, this is just another main or virtual memory address.
10
Instruction Representation
12
• Opcodes are represented by abbreviations, called mnemonics, that indicate the
operation.
• Common examples include:
• ADD Add
• SUB Subtract
• MUL Multiply
• DIV Divide
• LOAD Load data from memory
• STOR Store data to memory
• Operands are also represented symbolically.
• For example, the instruction ADD R, Y may mean add the value contained in data
location Y to the contents of register R.
• In this example, Y refers to the address of a location in memory, and R refers to a
particular register. Note that the operation is performed on the contents of a
location, not on its address.
13
Figure 1. A Simple Instruction Format
14
Instruction Types
• A computer should have a set of instructions that allows the user to formulate any
data processing task.
• Any program written in a high-level language must be translated into machine
language to be executed.
• Thus, the set of machine instructions must be sufficient to express any of the
instructions from a high-level language.
• With this in mind we can categorize instruction types as follows:
• Data processing: Arithmetic and logic instructions
• Data storage: Movement of data into or out of register and or memory
locations
• Data movement: I/O instructions
• Control: Test and branch instructions
15
• Arithmetic instructions: processing of numeric data.
• Logic (Boolean) instructions: operate on the bits of a word as bits rather than as
numbers; thus, they provide capabilities for processing any other type of data the user
may wish to employ.
• Data Storage instructions: arithmetic and Boolean operations are performed
primarily on data in processor registers.
• Therefore, there must be memory instructions for moving data between memory
and the registers.
• I/O instructions: are needed to transfer programs and data into memory and the
results of computations back out to the user.
• Test instructions: are used to test the value of a data word or the status of a
computation.
• Branch instructions: are then used to branch to a different set of instructions
depending on the decision made.
16
Number of Addresses
• All arithmetic and logic operations are either unary (one source operand) or
binary (two source operands).
17
• The result of an operation must be stored, suggesting a third address, which defines
a destination operand.
• Finally, after completion of an instruction, the next instruction must be fetched, and
its address is needed.
• Hence an instruction could contain four address references: two source operands,
one destination operand, and the address of the next instruction.
• In most architectures, most instructions have one, two, or three operand addresses,
with the address of the next instruction being implicit (obtained from the program
counter).
Comparison of one-, two- and three-address instructions to
compute: Y = (A – B)/[C + (D X E)].
19
Exercise: Repeat the above using 0-address
Instructions
Y = (A – B)/[C + (D X E)]
PUSH A
PUSH B
SUB
PUSH C
PUSH D
PUSH E
MUL
ADD
DIV
POP Y
20
• Three-address instruction formats: are not common because they require long instruction
format to hold the three address references.
• Two-address instruction format: one address doubles as operand and result.
• ADD a, b: carries out the calculation a+b and stores the result in a.
• Reduces length of instruction
• Requires some extra work (move instruction)
• Temporary storage to hold some results
• One-address instruction format: Implicit second address
• Usually a register (accumulator, AC). Also holds the result.
• Common on early machines
• Zero-address instruction format: applicable to a special memory organization called a stack.
• A stack is a last-in-first-out set of locations.
• The stack is in a known location and, often, at least the top two elements are in processor
registers.
• Thus, zero-address instructions would reference the top two stack elements.
21
Table 2. Utilization of Instruction Addresses
More addresses
• More complex instructions
• More registers
Inter-register operations are quicker
• Fewer instructions per program
Fewer addresses
• Less complex instructions
• More instructions per program
• Faster fetch/execution of instructions
22
Instruction Set Design
• One of the most interesting, and most analyzed, aspects of computer design is
instruction set design.
• The instruction set defines many of the functions performed by the processor and
thus has a significant effect on the implementation of the processor.
• Operation repertoire: How many and which operations to provide, and how
complex operations should be.
• Data types: The various types of data upon which operations are performed.
• whereas in a computer, numbers are represented with a finite number of digits. This
is because computers use a binary number system, which has only two digits (0 and
1), whereas ordinary mathematics uses a decimal number system, which has ten
digits (0 through 9).
• In addition to being limited in size, numbers stored in a computer are also subject to
rounding errors and other inaccuracies due to the finite precision of the binary
number system. This can lead to errors in calculations and other problems.
Cont.…..
• Floating-point numbers are a way of representing real numbers in a computer.
They are limited in precision because they use a fixed number of bits to represent
the number, and this limits the number of decimal places that can be represented.
This can lead to rounding errors and other inaccuracies in calculations.
• But avoids the conversion overhead. Conversion overhead refers to the additional
resources required to convert data from one format to another.
• Negative numbers can be represented by including a 4-bit sign digit at either the left
or right end of a string of packed decimal digits.
• Standard sign values are 1100 for positive (+) and 1101 for negative (-).
29
Characters
• A number of codes have been devised to represent characters by a sequence of bits.
• The most commonly used character code in the International Reference Alphabet (IRA)/referred
also as the American Standard Code for Information Interchange (ASCII)
• ASCII-encoded characters are almost always stored and transmitted using 8 bits per character.
• The eighth bit may be set to 0 or used as a parity bit for error detection. (odd parity/even parity)
30
Logical Data
• Normally, each word or other addressable unit (byte, halfword, and so on) is treated as a
single unit of data.
• Sometimes an n-bit unit can be considered as consisting of n 1-bit items.
• For example, an 8-bit unit can be considered as consisting of 8 separate 1-bit items. Each of
these 1-bit items can represent either a 0 or a 1, which means that the 8-bit unit can represent
2^8 (256) different values.
• By using n-bit units, we can store and process large amounts of data using a relatively small
amount of memory
• When data are viewed this way, they are considered to be logical data.
• There are two advantages to the bit-oriented view.
• To store an array of Boolean or binary data items, in which each item can take on only the
values 1 (true) and 0 (false).
• To manipulate the bits of a data item. For example, if floating-point operations are
implemented in software, we need to be able to shift significant bits in some operations.
31
Table 3:
x86
Data
Types
x86 numeric data
formats
33
Types of Operations
• The number of different opcodes varies widely from machine to machine.
• However, the same general types of operations are found on all machines.
• A typical categorization is the following:
• Data transfer
• Arithmetic
• Logical
• Conversion
• I/O
• System control
• Transfer of control
34
35
Common Instruction Set Operations
36
Common Instruction Set Operations
…
Processor actions for various types of operations
37
Data Transfer
• The most fundamental type of machine instruction.
• The data transfer instruction must specify several things.
• Location of source and destination operands:
• Each location could be memory, a register, or the top of the stack
• The length of data to be transferred.
• The mode of addressing for each operand.
• May be different instructions for different movements or one instruction and
different addresses.
• If one or both operands are in memory, then the processor performs the following:
• Calculate the memory address, based on the address mode
• If the address refers to virtual memory, translate from virtual to real memory
address.
• Determine whether the addressed item is in cache.
• If not, issue a command to the memory module.
38
Examples of IBM EAS/390 Data Transfer Operations
39
Arithmetic
• Most machines provide the basic arithmetic operations:
• add, subtract, multiply, and divide.
• These are invariably provided for signed integer (fixed-point) numbers.
• Often they are also provided for floating-point and packed decimal numbers.
• Other operations include single-operand instructions;
• Absolute: Take the absolute value of the operand.
• Negate: Negate the operand.
• Increment: Add 1 to the operand.
• Decrement: Subtract 1 from the operand.
• Execution of an arithmetic instruction may involve data transfer operations
• Bring operands for input to the ALU
• Deliver the output of the ALU
40
Logical
• Operations for manipulating individual bits of a word or other addressable units.
• They are based upon Boolean operations.
• Some of the basic logical operations that can be performed on Boolean or binary data
are shown in Table below.
• The NOT operation inverts a bit.
• AND, OR, and Exclusive-OR (XOR):
• The most common logical functions with two operands.
• EQUAL is a useful binary test.
• Can be applied bitwise to n-bit logical data units.
• If two registers contain the data: (R1) = 10100101
(R2) = 00001111
(R1) AND (R2) = 00000101
41
Table 4: Basic Logical Operations
Shifting and Rotating Operations
Logical shift:
• the bits of a word are shifted left or right. On one end, the bit shifted out is lost.
• On the other end, a 0 is shifted in.
• Logical shifts are used to isolate fields within a word.
• The 0s that are shifted into a word displace unwanted information that is shifted off
the other end
43
• Example: We want to transmit characters of data to an I/O device 1 character at a
time.
• Assume each memory word is 16 bits in length containing two characters.
• To send the two characters in a word: first the left-hand character
1. Load the word into a register.
2. Shift to the right eight times. This shifts the remaining character to the right
half of the register.
3. Perform I/O. The I/O module reads the lower-order 8 bits from the data bus.
• To send the right-hand character,
1. Load the word again into the register.
2. AND with 0000000011111111. (Masking)
3. Perform I/O.
44
Arithmetic shift
• Treats the data as a signed integer and does not shift the sign bit.
• On a right arithmetic shift, the sign bit is replicated into the bit position to its right.
• On a left arithmetic shift, a logical left shift is performed on all bits but the sign bit,
which is retained.
• These operations can speed up certain arithmetic operations.
• With numbers in twos complement notation, a right arithmetic shift corresponds to a
division by 2, with truncation for odd numbers.
• Both an arithmetic left shift and a logical left shift correspond to a multiplication
by 2 when there is no overflow.
• If overflow occurs, arithmetic and logical left shift operations produce different
results, but the arithmetic left shift retains the sign of the number.
45
Rotate, or cyclic shift
• preserve all of the bits being operated on.
• One use of a rotate is to bring each bit successively into the leftmost bit,
where it can be identified by testing the sign of the data.
Examples of Shift and Rotate Operations
46
Conversion
• Conversion instructions are those that change the format or operate on the format
of data. An example is converting from decimal to binary.
Input/Output
• Transfer data from specified I/O port or device to destination.
System Control
• Those instructions that can be executed only while the processor is in a certain
privileged state or is executing a program in a special privileged area of memory.
• Instructions reserved for the use of the operating system.
• Some examples:
• A system control instruction may read or alter a control register.
• An instruction to read or modify a storage protection key.
• Access to process control blocks in a multiprogramming system.
47
Transfer of Control
• For these instructions, processor updates the program counter to contain the address
of some instruction in memory.
• To execute each instruction more than once and perhaps many thousands of times.
If a table or a list of items is to be processed, a program loop is needed.
• Decision making. do one thing if one condition holds, and another thing if another
condition holds.
• Breaking a bigger program up into smaller pieces that can be worked on one at a
time.
48
Branch
• e.g. branch to x if result is zero
• Conditional and unconditional branches
• Unconditional: in which the branch is always taken
• Conditional: 1 or multiple bits condition code.
• Example: Consider an arithmetic operation (ADD, SUBTRACT, …) could set a 2-
bit condition code with one of the four values:
• zero, positive, negative, overflow.
• On such a machine, there could be four different conditional branch instructions:
• BRP X Branch to location X if result is positive.
• BRN X Branch to location X if result is negative.
• BRZ X Branch to location X if result is zero.
• BRO X Branch to location X if overflow occurs.
49
• Another approach that can be used with a three-address instruction format
is to perform a comparison and specify a branch in the same instruction.
• Example:
• BRE R1, R2, X
• Branch to X if contents of R1 = contents of R2.\
50
SKIP INSTRUCTIONS
• Another form of transfer-of-control instruction;
• includes an implied address;
• the skip implies that one instruction be skipped;
• the implied address = address of the next instruction plus one instruction length.
• Because the skip instruction does not require a destination address field, it is free to
do other things.
• Example: increment-and-skip-if-zero (ISZ)
51
• In this fragment, the two transfer-of-control instructions are used to implement
an iterative loop.
• Otherwise, the branch is skipped, and the program continues with the next
instruction after the end of the loop.
52
PROCEDURE CALL INSTRUCTIONS
• Perhaps the most important innovation in the development of programming
languages is the procedure.
• A procedure is a selfcontained computer program that is incorporated into a larger
program.
• At any point in the program the procedure may be invoked, or called.
• The processor is instructed to go and execute the entire procedure and then return to
the point from which the call took place.
• Two reasons for the use of procedures are economy and modularity.
• A procedure allows the same piece of code to be used many times.
• This is important for
• economy in programming effort and
• making the most efficient use of storage space in the system (the program must be
stored).
53
• Procedures also allow large programming tasks to be subdivided into smaller units.
• This use of modularity greatly eases the programming task.
• The procedure mechanism involves two basic instructions:
• a call instruction that branches from the present location to the procedure, and
• a return instruction that returns from the procedure to the place from which it was
called.
• Both of these are forms of branching instructions.
• Figure below illustrates the use of procedures to construct a program.
• There is a main program starting at location 4000.
• This program includes a call to procedure PROC1, starting at location 4500.
• When this call instruction is encountered, the processor suspends execution of the main
program and begins execution of PROC1 by fetching the next instruction from location
4500.
54
55
• Within PROC1, there are two calls to PROC2 at location 4800.
• The RETURN statement causes the processor to go back to the calling program and
continue execution at the instruction after the corresponding CALL instruction.
56
• The processor must save the return address so that the return can take place
appropriately.
• There are three common places for storing the return address:
• Register
• Start of called procedure
• Top of stack
• Consider a machine-language instruction CALL X, (call procedure at location X).
• If the register approach is used, CALL X causes the following actions:
RN PC + 1
PC X
• RN = a register always used for this purpose,
• PC = is the program counter
• = the instruction length.
• The called procedure can use the contents of RN later return.
57
• A second option is to store the return address at the start of the procedure.
• In this case, CALL X causes
X PC
PC X + 1
• Limitation: both these approaches complicate the use of reentrant procedures.
• A reentrant procedure is one in which it is possible to have several calls open to it at
the same time. (E.g. recursive procedure (one that calls itself) is reentrant.
• If parameters are passed via registers/memory, for a reentrant procedure, some code
must be responsible for saving the parameters so that the registers or memory space
are available for other procedure calls.
58
• Stacks
• A powerful approach to store return address and parameters
• When a call is executed, return address is kept on the stack.
• When a return is executed, the address on the stack is used.
59