5.Week
5.Week
3. Instruction sets
—Instruction Sets: Characteristics and Functions
—Instruction Sets: Addressing Modes and Formats
—Assembly Language and Related Topics
+
3.1 Instruction Sets: Characteristics
and Functions
3.1 Outline
• Machine Instruction Characteristics
• Types of Operands
• Intel x86 and ARM Data Types
• Types of Operations
• Intel x86 and ARM Operation Types
Machine Instruction Characteristics
• The operation of the processor is determined by the
instructions it executes, referred to as machine
instructions or computer instructions
• The collection of different instructions that the processor
can execute is referred to as the processor’s instruction
set
• Each instruction must contain the information required by
the processor for execution
Instruction Cycle State Diagram
I nstruction Operand Operand
fetch fetch store
M ultiple M ultiple
operands results
16 bits
Data Data
processing storage
Data
Control
movement
• Test instructions are used to test the • I/O instructions are needed
value of a data word or the status of a to transfer programs and
computation data into memory and the
• Branch instructions are used to branch results of computations
to a different set of instructions back out to the user
depending on the decision made
𝐀–𝐁
Programs to Execute Y =
𝐂 + (𝐃𝐄)
I nstruction Comment
SUB Y, A, B Y¬A–B
MPY T, D, E T¬D´E
ADD T, T, C T¬T+C
DIV Y, Y, T Y¬Y÷T
I nstruction Comment
(a) Three-address instructions
LOAD D AC ¬ D
MPY E AC ¬ AC ´ E
I nstruction Comment ADD C AC ¬ AC + C
MOVE Y, A Y¬A STOR Y Y ¬ AC
SUB Y, B Y¬Y–B LOAD A AC ¬ A
MOVE T, D T¬D SUB B AC ¬ AC – B
MPY T, E T¬T´E DIV Y AC ¬ AC ÷ Y
ADD T, C T¬T+C STOR Y Y ¬ AC
DIV Y, T Y¬Y÷T
3 OP A, B, C A ← B OP C
2 OP A, B A ← A OP B
1 OP A AC ← AC OP A
0 OP T ← (T – 1) OP T
AC = accumulator
T = top of stack
(T – 1) = second element of stack
A, B, C = memory or register locations
Instruction Set Design
Very complex because it affects so many aspects of the computer system
• Packed decimal
– Each decimal digit is represented by a 4-bit code with two digits stored per
byte
– To form numbers 4-bit codes are strung together, usually in multiples of 8 bits
Characters
• A common form of data is text or character strings
• Textual data in character form cannot be easily stored or
transmitted by data processing and communications systems
because they are designed for binary data
• Most commonly used character code is the International
Reference Alphabet (IRA)
– Referred to in the United States as the American Standard Code
for Information Interchange (ASCII)
Integer A signed binary value contained in a byte, word, or doubleword, using twos
complement representation.
x86 Data Types
Ordinal An unsigned integer contained in a byte, word, or doubleword.
Unpacked binary coded A representation of a BCD digit in the range 0 through 9, with one digit in
decimal (BCD) each byte.
Packed BCD Packed byte representation of two BCD digits; value in the range 0 to 99.
Near pointer A 16-bit, 32-bit, or 64-bit effective address that represents the offset within a
segment. Used for all pointers in a nonsegmented memory and for references
within a segment in a segmented memory.
Far pointer A logical address consisting of a 16-bit segment selector and an offset of 16,
32, or 64 bits. Far pointers are used for memory references in a segmented
memory model where the identity of a segment being accessed must be
specified explicitly.
Bit field A contiguous sequence of bits in which the position of each bit is considered
as an independent unit. A bit string can begin at any bit position of any byte
and can contain up to 32 bits.
Bit string A contiguous sequence of bits, containing from zero to 223 – 1 bits.
Byte 2
Byte 1
Byte 0
31 0 31 0
Byte 3 Byte 2 Byte 1 Byte 0 Byte 0 Byte 1 Byte 2 Byte 3
POP Dest
to the top of stack.
Copies top of stack to destination and increments ESP.
Instruction
(b) Arithmetic Set
Operation Name Description
Adds the destination and the source operand and stores the result in the
Operations
ADD Dest, Source destination. Destination can be register or memory. Source can be register,
Subtracts the source from the destination and stores the result in the destination.
(1 of 3)
Unsigned integer multiplication of the operand by the AL, AX, or EAX register and
MUL Op
stores in the register. Opcode indicates size of register.
IMUL Op Signed integer multiplication.
Divides unsigned the value in the AX, DX:AX, EDX:EAX, or RDX:RAX registers
DIV Op (dividend) by the source operand (divisor) and stores the result in the AX
(AH:AL), DX:AX, EDX:EAX, or RDX:RAX registers.
IDIV Op Signed integer division.
INC Op Adds 1 to the destination operand, while preserving the state of the CF flag.
Subtracts 1 from the destination operand, while preserving the state of the CF
DEC Op
flag.
Replaces the value of operand with (0 – operand), using twos complement
NEG Op
representation.
Compares the two operands by subtracting the second operand from the first
CMP Op1, Op2
operand and sets the status flags in the EFLAGS register according to the results.
(c) Shift and Rotate
Operation Name Description
Shifts the source operand left by from 1 to 31 bit positions. Empty bit positions are
SAL Op, Quantity
cleared. The CF flag is loaded with the last bit shifted out of the operand.
Performs a bitwise AND operation on the two operands and sets the S, Z, and P
TEST Op1, Op2
status flags. The operands are unchanged.
(e) Transfer of Control
Operation Name Description
Saves procedure linking information on the stack and branches to the called procedure
CALL proc specified using the operand. The operand specifies the address of the first instruction in
RET
the called procedure.
Transfers program control to a return address located on the top of the stack. The
Common x86
return is made to the instruction that follows the CALL instruction.
JMP Dest
Transfers program control to a different point in the instruction stream without recording
return information. The operand specifies the address of the instruction being jumped to.
Instruction
Jcc Dest
Checks the state of one or more of the status flags in the EFLAGS register (CF, OF, PF,
SF, and ZF) and, if the flags are in the specified state (condition), performs a jump to the
target instruction specified by the destination operand. See Tables 13.8 and 13.9.
Set
NOP
This instruction performs no operation. It is a one-byte or multi-byte NOP that takes up
space in the instruction stream but does not impact machine context, except for the EIP
register.
Operations
HLT
Stops instruction execution and places the processor in a HALT state. An enabled
interrupt, a debug exception, the BINIT# signal, the INIT# signal, or the RESET#
(3 of 3)
signal will resume execution.
Causes the processor to repeatedly check for and handle pending, unmasked, floating-
WAIT
point exceptions before proceeding.
INT Nr Interrupts current program, runs specified interrupt program
(f) Input/Output
Operation Name Description
Copies the data from the I/O port specified by the source operand to the
IN Dest, Source
destination operand, which is a register location.
Copies the data from the I/O port specified by the source operand to the
INS Dest, Source
destination operand, which is a memory location.
Copies the byte, word, or doubleword value from the source register to the I/O
OUT Dest, Source
port specified by the destination operand.
Copies byte, word, or doubleword from the source operand to the I/O port
XOR Dest, Source
specified with the destination operand. The source operand is a memory location.
Processor Actions for Various Types of
Operations
Transfer data from one location to another
If memory is involved:
Data transfer Determine memory address
Perform virtual-to-actual-memory address transformation
Check cache
Initiate memory read/write
Must specify:
• Location of the source and
destination operands
Most fundamental type of • The length of data to be
machine instruction transferred must be indicated
• The mode of addressing for each
operand must be specified
Examples of IBM EAS/390 Data Transfer
Operations
Operation Number of Bits
Mnemonic Name Transferred Description
0 0 1 0 0 0 1
0 1 1 0 1 1 0
1 0 0 0 1 1 0
1 1 0 1 1 0 1
Shift and Rotate Operations
0
An example of a
more complex
editing
instruction is the
An example EAS/390
is converting Translate (TR)
from instruction
decimal to
binary
Input/Output
• Variety of approaches taken:
– Isolated programmed I/O
– Memory-mapped programmed I/O
– DMA
– Use of an I/O processor
200
201
202 SUB X, Y
203 BRZ 211
Unconditional
branch Conditional
branch
210 BR 202
211
Conditional
branch
235
M ain
4100 CALL Proc1
4101 Program
4500
RETURN
4800
Procedure
Proc2
RETURN
4601 4651
4101 4101 4101 4101 4101
(a) I nitial stack (b) After (c) I nitial (d) After (e) After (f) After (g) After
contents CALL Proc1 CALL Proc2 RETURN CALL Proc2 RETURN RETURN
• When a new procedure is called the following must be performed upon entry
to the new procedure:
– Push the return point on the stack
– Push the current frame pointer on the stack
– Copy the stack pointer as the new value of the frame pointer
– Adjust the stack pointer to allocate a frame
x86 Status Flags
Status Bit Name Description
Indicates carrying or borrowing out of the left-
C Carry most bit position following an arithmetic operation. Also
modified by some of the shift and rotate operations.
Parity of the least-significant byte of the result of an arithmetic
P Parity or logic operation. 1 indicates even parity; 0 indicates odd
parity.
Represents carrying or borrowing between half-
A Auxiliary Carry bytes of an 8-bit arithmetic or logic operation. Used in binary-
coded decimal arithmetic.
Z Zero Indicates that the result of an arithmetic or logic operation is 0.
Indicates the sign of the result of an arithmetic or logic
S Sign
operation.
AE, NB, NC C=0 Above or equal; Not below (greater than or equal,
unsigned); Not carry
x86 Condition
B, NAE, C C=1 Below; Not above or equal (less than, unsigned);
Carry set Codes for
BE, NA C = 1 OR Z = 1 Below or equal; Not above (less than or equal, unsigned)
NO O=0 No overflow
O O=1 Overflow
PADD [B, W, D] Parallel add of packed eight bytes, four 16-bit words, or two
32-bit doublewords, with wraparound.
PSUB [B, W, D]
Add unsigned with saturation.
Instruction Arithmetic
PSUBS [B, W]
PSUBUS [B, W]
Subtract with saturation.
Set PMULLW
order 16 bits of 32-bit result chosen.
Parallel multiply of four signed 16-bit words, with low-order 16 bits of 32-bit result
chosen.
PCMPEQ [B, W, D] Parallel compare for equality; result is mask of 1s if true or 0s if false.
Comparison
PCMPGT [B, W, D] Parallel compare for greater than; result is mask of 1s if true or 0s if false.
PACKSS [WB, DW] Pack words into bytes, or doublewords into words, with signed saturation.
Conversion PUNPCKH [BW, WD, DQ] Parallel unpack (interleaved merge) high-
order bytes, words, or doublewords from MMX register.
PSLL [W, D, Q] Parallel logical left shift of packed words, doublewords, or quadword by amount
specified in MMX register or immediate value.
Shift
PSRL [W, D, Q] Parallel logical right shift of packed words, doublewords, or quadword.
PSRA [W, D] Parallel arithmetic right shift of packed words, doublewords, or quadword.
Data transfer MOV [D, Q] Move doubleword or quadword to/from MMX register.
Note: If an instruction supports multiple data types [byte (B), word (W), doubleword (D), quadword (Q)], the data
types are indicated in brackets.
ARM Operation Types
Parallel addition
Multiply Extend
and subtraction
instructions instructions
instructions
Status register
access
instructions
Code Symbol Condition Tested Comment
0000 EQ Z=1 Equal