Sei sulla pagina 1di 94

MICROPROCESSOR &

MICROCONTROLLER SYSTEM
IBB20204
INSTRUCTION SETS
ASSEMBLY LANGUAGE PROGRAMMING
The Instruction Set
1. Data transfer instructions (move: MOV, exchange: XCHG,
translate: XLAT, load register: LEA, LDS, LES)
2. Arithmetic instructions (addition: ADD, subtraction: SUB,
multiplication: MUL)
3. Logic instructions
4. String manipulation instructions (SHIFT and ROTATE)
DATA TRANSFER INSTRUCTION
DATA TRANSFER
 Provide the ability to MOVE data either between its internal
registers or between an internal register and a storage location
in memory.

 The data transfer functions include:


1. Move byte or word (MOV)
2. Exchange byte or word (XCHG)
3. Translate byte (XLAT)
4. Load effective address (LEA)
5. Load data segment (LDS)
6. Load extra segment (LES)
DATA TRANSFER - MOVE
 To transfer the data from the source operand to destination
operand.
 Note that the MOV instruction cannot transfer data directly
between external memory.

 Ex1: MOV DX, CS


• Source operand: Code Segment
• Destination operand: Data Register
• Register operand addressing mode
• Stands for “move the contents of CS into DX”
• That is (CS)(DX)
• If the contents of CS are 010016, execution of this
instruction makes (DX) = (CS) = 010016
DATA TRANSFER - MOVE
 In relative to the storage location in memory, the machine code for
the instruction includes an effective address (offset) relative to the
beginning of current data segment, by default.
 Ex2: MOV [SUM], AX
•  Memory operand addressing mode  Direct addressing mode
• Given that DS = 020016 and variable SUM = 121216.
• Then this instruction means “move the contents of
accumulator register AX to the memory location offset by
121216 from the starting location of the current data
segment”.
• Calculation of the PA gives: PA = 0200016 + 121216 =
0321216
• Thus the effect of this instruction will execute the
following:
• AX = AH|AL
• AL = contents of memory location 0321216
• AH = contents of memory location 0321316
DATA TRANSFER - MOVE
 Ex3: What is the effect of executing the following instruction
MOV CX, [SOURCE_MEM] where SOURCE_MEM equals to
displacement of 2016 relative to the current data segment
defined by data segment base address 1A0016?
DATA TRANSFER - MOVE
 Answer:
 Memory operand addressing mode  Direct addressing mode
 Calculation of the PA gives: PA = 1A00016 + 2016 = 1A02016
 Thus the effect of this instruction will execute the following:
 CX = CH|CL
 CL = contents of memory location 1A02016
 CH = contents of memory location 1A02116
DATA TRANSFER - EXCHANGE
 In some applications, we need to exchange the contents of two
registers.
 Comparison:
 MOV instruction – original contents of the source operand are
preserved, original contents of the destination operand are
destroyed (override).
 XCHG – used to swap data between two general-purpose
registers or between general-purpose register, including the
pointers and index registers and a storage location in memory.
DATA TRANSFER - EXCHANGE
 Allowed operands for XCHG function:

 Ex1: XCHG AX, DX


• Swaps the contents of AX with that of DX.
• (AX original)  (DX)
• (DX original)  (AX)
• Or (AX)  (DX)
DATA TRANSFER - EXCHANGE
 Ex2: For the data shown in the figure below, what is the result of
executing the following instruction XCHG [SUM], BX?
DATA TRANSFER - EXCHANGE
 Answer:
 Memory operand addressing mode  Direct addressing mode
 Calculate the PA of the destination operand, variable [SUM]:
 Refer to figure:
 DS = 120016 and direct address of variable SUM = 123416.
 PA = 1200016 + 123416 = 1323416
 At this location 1323416, contents are FF16 and at 1323516, the
contents are 0016.
 At base register BX, the contents are AA16 for BL and 1116 for BH.
 Execution of this instruction performs the following word (16-bit)
swap operation: (1323416) = (AA16) (BL) = (FF16)
(1323516) = (1116)  (BH) = (0016)
DATA TRANSFER - EXCHANGE
 Answer: XCHG [SUM], BX
DATA TRANSFER - TRANSLATE
 This instruction is useful for translating characters from one
code such as ASCII to another such as EBCDIC, that requires
look-up table.

 This is no operand instruction and is called an instruction with


implied addressing mode.

 Instruction: XLAT
 This means “replace the contents of AL by the contents of the
accessed look-up table location”.
DATA TRANSFER - TRANSLATE
 ((AL)+(BX)+(DS)0)  AL

 Means “loads AL with the contents of a 20 bit physical address


computed from DS, BX and AL”.
 PA = (DS)0 + (BX) + (AL)

Starting address for


the code conversion
 This instruction can be used to read the elements in a table where
BX can be loaded with a 16 bit value to point to the starting address
(offset from DS) and AL can be loaded with the element number (0
being the first element number).

 No flags are affected.


DATA TRANSFER - TRANSLATE
• The individual EBCDIC codes are
located in the table at element
displacements (memory address) equal
to their equivalent ASCII character
values.
• When XLAT is executed;
• Starting address of the conversion
code, PA = (DS)0+(BX) = 0300016
+ 010016 = 0310016.
• AL = 3F16
• So, PA = (DS)0 + (BX) + (AL) =
0300016 + 010016 + 3F16 = 0313F16
• At this address, 0313F16, the
EBCDIC code is 6F16, which is
equivalent to “?” in ASCII.
• Thus, the execution can be described
by (0313F16)  (AL)
DATA TRANSFER – LOAD REGISTER
 An important type of data-transfer operation is loading a
segment register and general-purpose register with an address
directly from memory.

 These instructions are:


1. Load register with effective address (LEA)
2. Load register and data segment register (LDS)
3. Load register and extra segment register (LES)

 These instructions provide programmers the ability to


manipulate memory addresses by loading either 16-bit offset
addresses into a general-purpose register together with a 16-bit
segment address into either DS or ES.
DATA TRANSFER – LOAD REGISTER
 LEA, LDS, and LES data transfer instruction:
DATA TRANSFER – LEA
 LEA instruction – used to load a specified register with a 16-bit
offset address.
 The value of this offset is represented by the EA.
 Value EA can be specified by any valid addressing mode.

 Ex: LEA SI, [DI + BX + 5H]

 Means “load the SI register with the value of corresponding EA


of the source operand”.
 If DI = 1000H, and BX = 20H, then EA = DI+BX+5H =
1000H+20H+5H = 1025H
 Then the execution of this instruction will load SI with the value
of (SI) = 1025H
DATA TRANSFER – LDS & LES
• LDS and LES are similar to LEA,
accept that they load the specified
register with DS and ES respectively.
• Ex: LDS SI, [200H]
Register SI must be loaded with DS
and [200H] respectively.
Calculate the PA of the source
operand
PA = (DS)0+200H =
12000H+200H
PA = 12200H
• The contents at this address, 12200H is
20H, followed by the content of 00 at
the following address.
• Therefore, SI is being loaded with
0020H.
DATA TRANSFER – LDS & LES
After execution of LDS SI, [200H]

PA = 12000H+0200H=12200H

[12200-12201]  SI = 0020H

The next two bytes – that is, the


contents at address 12202H and
12203H are loaded into DS
register.
[12202-12203]  DS = 1300H

Thus DS = 1300H becomes the


new data segment.
ARITHMETIC INSTRUCTIONS
ARITHMETIC INSTRUCTIONS
 Introduction
 8086/88 microprocessor contains a variety of arithmetic
instructions, including instructions for addition, subtraction,
multiplication, and division operations.
 These operations can be performed on numbers expressed in a
variety of numeric data formats.
 Be it unsigned or signed binary bytes or words, unpacked or packed
decimal bytes, or ASCII numbers.
 The status that results from the execution of an arithmetic
instruction is recorded in the flags of the microprocessor.
 The flags that are affected by the arithmetic instructions are:
 Carry flag (CF), auxiliary flag (AF), sign flag (SF), zero flag (ZF), parity flag
(PF), and overflow flag (OF).
ARITHMETIC INSTRUCTIONS
 Subgroups of arithmetic instructions.
ADDITION
ADDITION OF BINARY NUMBERS
 Recall Chapter 1: Rules of addition

Adding two binary numbers are same as addition of decimal


numbers. There are mainly 4 rules should follow in the process of
addition in binary numbers.

Sum Carry
Rule 1 0 + 0=0 0
Rule 2 0 + 1=1 0
Rule 3 1 + 0=1 0
Rule 4 1 + 1=0 1
ADDITION OF BINARY NUMBERS
 The binary add operation is described in general as:
A+B = S + Co
 Where Co = carry-out.

 In 8086/88 assembly language, binary additions are performed


in byte (8-bit) or word (16-bit) data format.
 Example of addition of two byte-wide numbers A and B is
represented as:
A7 A6 A5 A4 A3 A2 A1 A0
+ B7 B6 B5 B4 B3 B2 B1 B0
S8 S7 S6 S5 S4 S3 S2 S1 S0
 Where S8: carry-out from the sum on the 8th bit.
ADDITION OF BINARY NUMBERS
Example:

N1 = 17010 = 101010102 and N2 = 23810 = 111011102

N1 + N2 = 17010 + 23810 = 40810

Binary addition gives:


111 111 carry
101010102 N1
111011102 N2
1100110002 sum
ADDITION SUBGROUPS
 Addition subgroups are:

 Form of each instructions:


ADDITION SUBGROUPS
 Allowed operands for instruction ADD and ADC:

 Allowed operands for instruction INC:


ADD INSTRUCTION
 ADD = Add byte or word.
 In general, the result of executing ADD is expressed as:

(S) + (D)  (D)

 That is, the contents of the source operand are added to those of
the destination operand and the sum that results is put into the
location of the destination operand.

 The carry-out that may occur from the addition of the MSB of
the destination is reflected in the carry flag (CF).
ADD INSTRUCTION
Example:
Assume that the AX and BX registers contain 110016 and 0ABC16,
respectively.What is the result of executing the instruction:
ADD AX, BX
ADD INSTRUCTION
 Solution:

(BX)+(AX)= 0ABC16 + 110016=1BBC16

The sum ends up in destination register AX.


That is:

1B BC
AX
ADD INSTRUCTION
 Solution: ADD instruction before/after fetch and execution.
ADC INSTRUCTION
 ADC = Add byte or word with carry
 Works similarly to ADD, except, the content of the carry flag is
also added.
 ADC is generally expressed as:

(S) + (D) + (CF)  (D)

 Used for multiword add operations.


INC INSTRUCTION
 INC = Increment.
 Execution of the INC instruction adds 1 to the specified
operand.
 This instruction is typically used to increment the values of a
count or address.
 Example: INC AH
 This instruction executes increment of the high byte of AX.
 (AH)  (AH) +116
ADD, ADC, INC INSTRUCTION
Example:
The original contents of AX, BL, word-size memory location SUM,
and carry flag (CF) are 123416, AB16, 00CD16, and 016, respectively.
Describe the results of executing the following sequence of
instruction. Fill in the table with the execution results.
ADD AX, [SUM]
ADC BL, 05H
INC [SUM]
INSTRUCTION (AX) (BL) (SUM) (CF)
Initial State 1234H ABH 00CDH 0H
ADD AX, [SUM]
ADC BL, 05H
INC [SUM]
ADD, ADC, INC INSTRUCTION
Solution:

 1st instruction – Adds the word in the accumulator and the word
in the memory location pointed to by address SUM. The result is
placed in the accumulator.

 That is,

(AX)  (AX) + (SUM) = 123416 + 00CD16 = 130116

 Carry flag (CF) remains 0.


ADD, ADC, INC INSTRUCTION
Solution:

 2nd instruction – Adds to the lower byte of the base register (BL)
the immediate operand 5H and the carry flag, which is 0H.

 This gives,

(BL)  (BL) + imm + (CF) = AB16 + 516+016 = B016

 Since no carry is generated, the CF remains 0.


ADD, ADC, INC INSTRUCTION
Solution:

 3rd instruction – Increments the contents of memory location


SUM by one.

 That is,
(SUM)  (SUM) + 116 = 00CD16 + 116 = 00CE16
 CF remains 0.
INSTRUCTION (AX) (BL) (SUM) (CF)
Initial State 1234H ABH 00CDH 0H
ADD AX, [SUM] 1301H ABH 00CDH 0H
ADC BL, 05H 1301H B0H 00CDH 0H
INC [SUM] 1301H B0H 00CEH 0H
SUBTRACTION
SUBTRACTION OF BINARY NUMBERS
 Recall Chapter 1: Rules of subtraction

Minuend(B) Subtrahend(A) Difference Borrow out

Rule 1 0 - 0 = 0
Rule 2 0 - 1 = 1 with a borrow of 1
Rule 3 1 - 0 = 1
Rule 4 1 - 1 = 0

To perform Rule 2 you have to borrow 1 from the next column. The
weight of the binary you borrow will be 2.
SUBTRACTION OF BINARY NUMBERS
 The binary subtraction operation is described in general as:
A – B = D + Bri
 Where Bri = borrow-in.There will also be a borrow-out (Bro).

 In 8086/88 assembly language, binary subtractions are


performed in byte (8-bit) or word (16-bit) data format.

 Example of subtraction of two byte-wide numbers A and B is


represented as:
A7 A6 A5 A4 A3 A2 A1 A0
- B7 B6 B5 B4 B3 B2 B1 B0
D7 D6 D5D4 D3 D2D1D0
SUBTRACTION OF BINARY NUMBERS
Example:

N1 = 23810 = 111011102 and N2 = 17110 = 101010112

N1 - N2 = 23810 - 17110 = 6710

Binary subtraction gives:


11 borrow
111011102 N1
- 101010112 N2
010000112 Difference
SUBTRACTION SUBGROUPS
 Subtraction subgroups are:

 Form of each instructions:


SUBTRACTION SUBGROUPS
 Allowed operands for instruction SUB and SBB:

 Allowed operands for instruction DEC:


SUBTRACTION SUBGROUPS
 Allowed operands for instruction NEG:
SUB INSTRUCTION
 SUB = Subtract byte or word.
 In general, the result of executing SUB is expressed as:

(D) - (S)  (D)

 That is, the contents of the source operand are being subtracted
by those of the destination operand and the sum that results is
put into the location of the destination operand.

 The borrow-in (Bri) and borrow-out (Bro) that may occur from
the subtraction of the MSB of the destination is reflected in the
carry flag (CF).
SUB INSTRUCTION
Example:
Assume that the AL and BX registers contain AF16 and A316,
respectively.What is the result of executing the instruction:
SUB AL, BX
SUB INSTRUCTION
Solution:

(AL) - (BX)= AF16 – A316 = C16

The difference ends up in destination register AL.


That is:

XX 0C
AX
SBB INSTRUCTION
 SBB = Subtract byte or word with borrow.
 Works similarly to SUB, except, it also subtracts the contents of
the carry flag from the destination.
 SBB is generally expressed as:

(D) - (S) - (CF)  (D)

 Used for multiword subtract operations.


SBB INSTRUCTION
Example:
Assuming that the contents of register BX and CX are 123416 and
012316, respectively, and the carry flag is 016, what is the result of
executing the following instruction?
SBB BX, CX
SBB INSTRUCTION
Solution:

Since the instruction implements the operation of:

(BX) – (CX) – (CF)  (BX)

Therefore,
(BX) = 123416 – 012316 – 016
(BX) = 111116

Since, no borrow was needed, the carry flag remains 0.


DEC INSTRUCTION
 DEC = Decrement.
 Execution of the DEC instruction subtracts 1 from its specified
operand.
 This instruction is typically used to decrement the values of a
count or address.
 Example: DEC AH
 This instruction executes decrement of the high byte of AX.
 (AH)  (AH) - 116
NEG INSTRUCTION
 NEG = Negate (negative)
 Execution of the NEG instruction causes the value of its operand
to be replaced by its negative.
 This is actually done thru subtraction – that is, the contents of
the specified operand are subtracted from zero and the result is
returned to the operand location.
 The subtraction is actually performed using 2’s complement
arithmetic.
 To obtain the correct value of the carry flag that results from a
NEG operation, the carry generated by the add operation used
in the 2’s complement subtraction calculation must be
complemented.
NEG INSTRUCTION
Example:
Assuming that the register BX contains 003A16, what is the result
of executing the following instruction?
NEG BX
NEG INSTRUCTION
Solution:
The instruction causes the 2’s complement subtraction that
follows:
(BX) = 000016 - (BX) = 000016 + 2’s complement of 003A16
= 000016 + FFC616
= FFC616

Since no carry is generated in this add operation, the carry flag is


complemented to give (CF) = 1
MULTIPLICATION & DIVISION
MULTIPLICATION OF BINARY NUMBERS
 Recall Chapter 1: Rules of Multiplication

 The four basic rules for multiplying bits are as follows:

0x0=0
0x1=0
1x0=0
1x1=1
MULTIPLICATION & DIVISION
SUBGROUPS
 Multiplication subgroups are:

 Division subgroups are:


MULTIPLICATION & DIVISION
INSTRUCTIONS
MULTIPLICATION INSTRUCTIONS
 Allowed operands:

 A byte-wide or word-wide source operand is specified in a


multiplication instruction.
 The result of multiplication will be stored in the destination
operand in terms of:
to be in AX for multiplication of AL x byte (8-bit).
to be in DX AX for multiplication of AX x word (16-bit).
MULTIPLICATION INSTRUCTIONS
 The result of executing an MUL or IMUL instruction byte and
word can be represented as:

(AL) x (8-bit operand)  (AX)


(AX) x (16-bit operand)  (DX:AX)
MULTIPLICATION INSTRUCTIONS
 Example 1: What will be the result execute by the following
instructions?
MOV AL, 5H
MOV BL, 10H
MUL BL
MULTIPLICATION INSTRUCTIONS
 Solution:
 MOV AL, 5H means AL will be loaded by 516 = 0000 01012
 MOV BL, 10H means BL will be loaded by 1016 = 0000 10102
 MUL BL executes multiplication of AL and BL
where (AL) x (8-bit operand)  (AX)
5H x 10H = 0050H  (AX)
0000 0101 x 0000 1010  (AX)
MULTIPLICATION INSTRUCTIONS
 Example 2: The contents of AL = -116 and the contents of CL =
-216. What result is produced in AX by executing the following
instructions?
MUL CL
IMUL CL
MULTIPLICATION INSTRUCTIONS
 Solution:
As binary data, the contents of AL and CL are:
(AL) = -116 = 0000 00012 = 1111 11102 = 1111 11112 = FF16
(CL) = -216 = 0000 00102 = 1111 11012 = 1111 11102 = FE16

Executing the instruction MUL CL gives:


(AX) = 1111 11112 x 1111 11102 = 11111101000000102 = FD0216

Executing second instruction IMUL CL will execute multiplication


of the two signed numbers to generate the result, that is:

(AX) = -116 x -216 = 216 = 000216


DIVISION INSTRUCTIONS
 For the division operation, again just the source operand is
specified. The other operand is either the contents of AX for 16-
bit dividends or the contents of both DX and AX for 32-bit
dividends.
 The result of a DIV or IDIV instruction for an 8-bit divisor is
represented by
(AH), (AL)  (AX)/(8-bit operand)
Where AH contains the remainder and AL the quotient.
 For 16-bit division, we get
(DX), (AX)  (DX, AX)/(16-bit operand)
 Here AX contains the quotient and DX contains the remainder.
LOGIC INSTRUCTIONS
LOGIC INSTRUCTIONS
 Logic instructions include: AND, OR, X-OR and NOT.
 Logic symbol for each of these functions and their basic
operation is described in the truth table.
LOGIC INSTRUCTIONS
 Microprocessor performs these basic logic operations bit-wise
on byte-wide and word-wide data. For example, for the byte-
wide AND operation:
LOGIC INSTRUCTIONS
 Logic instructions
LOGIC INSTRUCTIONS
 Allowed operands for the AND, OR, and X-OR.

 Allowed operands for NOT instruction.


LOGIC INSTRUCTIONS
 Example 1: AND AX, BX
 This instruction causes the contents of BX to be bit-wise ANDed
with the contents of AX.
 The result is reflected by the new contents of AX.
 Assuming that AX contains 123416 and BX contains 000F16, the
result produced by the instruction is:
123416 . 000F16 = 0001 0010 0011 01002 . 0000 0000 0000 11112
= 0000 0000 0000 01002
= 000416

This result is stored in the destination operand and gives


(AX) = 000416
LOGIC INSTRUCTIONS
 Example 2: Describe the result of executing the following
sequence of instructions:
MOV AL, 010101012
AND AL, 000111112
OR AL, 110000002
XOR AL, 000011112
NOT AL
LOGIC INSTRUCTIONS
 Solution:
LOGIC INSTRUCTIONS
SHIFT INSTRUCTIONS
SHIFT INSTRUCTIONS
 4 shift instructions: SHL, SHR, SAL, SAR
 2 types of shift operations: logical shift & arithmetic shift.
 These shift operations can be performed to the right or to the
left.
 SHL: Shift logical left
 SHR: Shift logical right
 SAL: Shift arithmetic left
 SAR: Shift arithmetic right
 All these instructions are used to align data, to isolate bits of a
byte/word so that it can be tested, and to perform simple
multiply and divide computations.
SHIFT INSTRUCTIONS
 Shift instructions:
SHIFT INSTRUCTIONS
 Allowed operands:

 Source operand can be specified in 2 ways:


1. Assign value of 1  1-bit shift will take place
2. Assign count CL, ex: if CL = 2  shift 2 bit positions
SHIFT INSTRUCTIONS
 Example 1: SHL AX, 1

 This instruction causes the 16-bit contents of the AX register to


be shifted 1 bit position to the left.
 Vacated LSB location is filled with 0.
 The bit shifted out of the MSB is saved in CF.
SHIFT INSTRUCTIONS
 Example 2: SHR AX, CL (if CL = 2)
 Shift the AX to the right by the count number of CL (here = two
bit locations) and fill the vacated bits positions on the left with
zeros.
 The logical shift right that occurs is as shown in the figure.
 The two MSBs have been filled with 0 and last bit shifted out at
the LSB, which is 0, is placed in the CF.
SHIFT INSTRUCTIONS
 Example 3: If AX = 091A16, CL = 2, what will be the result of
this instruction? What will be the content of the CF?

SHR AX, CL
ROTATE INSTRUCTIONS
ROTATE INSTRUCTIONS
 ROL – Rotate left
 ROR – Rotate right
 RCL – Rotate left through carry
 RCR – Rotate right through carry

 They perform many of the same programming functions as the


shift instructions, such as alignment of data and isolation of a bit
of an element of data.
 In several ways, rotate instructions work similarly as shift
instructions.
 They have the ability to rotate the contents of either an internal
register or a storage location in memory.
ROTATE INSTRUCTIONS
 The rotation that takes place can be from 1 to 255 bit positions
to the left or to the right.

 In the case of multibit rotate, the number of bit positions to be


rotated is specified by the value in CL.

 Their difference from the shift instructions lies in the fact that
the bits moved out at either the MSB or LSB end are not lost;
instead, they are reloaded at the other end.
ROTATE INSTRUCTIONS
 Rotate instructions:
ROTATE INSTRUCTIONS
 Allowed operands:
ROTATE INSTRUCTIONS
 ROL – Execution of this instruction causes the contents of the
selected operand to be rotated left the specified number of bit
positions. Each bit shifted out at the MSB end is reloaded at the
LSB end. Moreover, the content of CF reflects the state of the
last bit that was shifted out.
ROTATE INSTRUCTIONS
 Example 1: ROL AX, 1 (take AX = 123416)
 This instruction causes a 1-bit rotate to the left.
 Note: The original value of bit 15 is 0.
 This value has been rotated into both CF and bit 0 of AX.
 All other bits have been rotated one bit position to the left.
ROTATE INSTRUCTIONS
 ROR – Operates the same way as ROL, except that it causes
data to be rotated to the right, instead of to the left.

 Example 2: ROR AX, CL


 Take AX = 123416 and count CL = 4
 This instruction causes the contents of AX to be rotated to the
right by the number of bit positions specified in CL.
ROTATE INSTRUCTIONS
 Example 4: What will be executed by the following instruction
given that AX = 234516 and CL = 3. What will be the content of
the CF?

ROL AX, CL
Thank You

Potrebbero piacerti anche