1 of 66

CPE 408330� Assembly Language and�Microprocessors��Chapter 5: 8088/8086 Microprocessor �Programming – Integer Instructions �and Computations����

[Computer Engineering Department,

Hashemite University, © 2008]

2 of 66

Lecture Outline

  • 5.1 Data-Transfer Instructions
  • 5.2 Arithmetic Instructions
  • 5.3 Logic Instructions
  • 5.4 Shift Instructions
  • 5.5 Rotate Instructions

2

CPE 0408330 @ 2008

S. Abed - HU, Jordan

3 of 66

5.1 Data-Transfer Instructions

  • The data-transfer functions 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
    • MOV (Move byte or word)
    • XCHG (Exchange byte or word)
    • XLAT (Translate byte)
    • LEA (Load effective address)
    • LDS (Load data segment)
    • LES (Load extra segment)

3

CPE 0408330 @ 2008

S. Abed - HU, Jordan

4 of 66

5.1 Data-Transfer Instructions Move Instruction

  • The MOVE Instruction

4

CPE 0408330 @ 2008

S. Abed - HU, Jordan

• Used to move (copy) data between:

• Registers

• Register and memory

• Immediate operand to a register or memory

• General format:

MOV D,S

• Operation: Copies the content of the source to the destination

(S) 🡪 (D)

• Source contents unchanged

• Flags unaffected

• Allowed operands

Register

Memory

Accumulator (AH,AL,AX)

Immediate operand (Source only)

Segment register (Seg-reg)

• Examples:

MOV [SUM],AX

(AL) 🡪 (address SUM)

(AH) 🡪 (address SUM+1)

Allowed operands for MOV instruction

5 of 66

5.1 Data-Transfer Instructions Move Instruction

  • The MOVE Instruction

e.g. MOV DX, CS

MOV [SUM], AX

  • Note that the MOV instruction cannot transfer data directly between external memory.

5

CPE 0408330 @ 2008

S. Abed - HU, Jordan

6 of 66

5.1 Data-Transfer Instructions Move Instruction

6

CPE 0408330 @ 2008

S. Abed - HU, Jordan

MOV DX, CS

Before execution

Source = CS 🡪 word data

Destination = DX 🡪 word data

Operation: (CS) 🡪 (DX)

• State before fetch and execution

CS:IP = 0100:0100 = 01100H

Move instruction code = 8CCAH

(01100H) = 8CH

(01101H) = CAH

(CS) = 0100H

(DX) = XXXX 🡪 don’t care state

7 of 66

5.1 Data-Transfer Instructions Move Instruction

7

CPE 0408330 @ 2008

S. Abed - HU, Jordan

MOV DX, CS

After execution

• State after execution

CS:IP = 0100:0102 = 01102H

01002H 🡪 points to next

sequential instruction

(CS) = 0100H

(DX) = 0100H 🡪 Value in CS

copied into DX

Value in CS unchanged

8 of 66

5.1 Data-Transfer Instructions Move Instruction

  • EXAMPLE

What is the effect of executing the instruction

MOV CX, [SOURCE_MEM]

Where SOURCE_MEM equal to 2016 is a memory location offset relative to the current data segment starting at 1A0016.

  • Solution:

((DS)0+2016) → (CL)

((DS)0+2016+116) → (CH)

Therefore CL is loaded with the contents held at memory address

1A00016 + 2016 = 1A02016

and CH is loaded with the contents of memory address

1A00016 + 2016 +116 = 1A02116

8

CPE 0408330 @ 2008

S. Abed - HU, Jordan

9 of 66

5.1 Data-Transfer Instructions Move Instruction

  • EXAMPLE

Use the DEBUG to verify

MOV CX,[20]

DS = 1A00 (DS:20) = AA55H

(1A00:20) 🡪 (CX)

  • Solution:

9

CPE 0408330 @ 2008

S. Abed - HU, Jordan

10 of 66

5.1 Data-Transfer Instructions Move Instruction

10

CPE 0408330 @ 2008

S. Abed - HU, Jordan

• Example—Initialization of internal registers with immediate data and address information

• DS, ES, and SS registers initialized from immediate data via AX

IMM16 🡪 (AX)

(AX) 🡪 (DS) & (ES) = 2000H

IMM16 🡪 (AX)

(AX) 🡪 (SS) = 3000H

• Data registers initialized

IMM16 🡪 (AX) =0000H

(AX) 🡪 (BX) =0000H

IMM16 🡪 (CX) = 000AH and (DX) =

0100H

• Index register initialized from immediate operations

IMM16 🡪 (SI) = 0200H and (DI) = 0300H

DS,ES to 2000H

SS to 3000H

AX, BX to 0H

CX to 0A

SI to 200

DI to 300

11 of 66

5.1 Data-Transfer Instructions - Exchange Instruction

11

CPE 0408330 @ 2008

S. Abed - HU, Jordan

• Used to exchange the data between two data registers or a data register and memory

• General format:

XCHG D,S

• Operation: Swaps the content of the

source and destination

• Both source and destination change

(S) 🡪 (D)

(D) 🡪 (S)

• Flags unaffected

• Special accumulator destination version executes faster

• Examples:

XCHG AX,DX

(Original value in AX) 🡪 (DX)

(Original value in DX) 🡪 (AX)

Allowed operands for XCHG instruction

12 of 66

5.1 Data-Transfer Instructions - Exchange Instruction

12

CPE 0408330 @ 2008

S. Abed - HU, Jordan

XCHG [SUM],BX

Note: SUM = 1234

Before execution

Source = BX 🡪 word data

Destination = memory offset

SUM 🡪 word data

Operation: (SUM) 🡪 (BX)

(BX) 🡪 (SUM)

What is the general logical address

of the destination operand?

• State before fetch and execution

CS:IP = 1100:0101 = 11101H

Move instruction code = 871E3412H

(01104H,01103H) = 1234H = SUM

(DS) = 1200H

(BX) = 11AA

(DS:SUM) = (1200:1234) = 00FFH

13 of 66

5.1 Data-Transfer Instructions - Exchange Instruction

13

CPE 0408330 @ 2008

S. Abed - HU, Jordan

XCHG [SUM],BX

After execution

• State after execution

CS:IP = 1100:0105 = 11105H

11005H 🡪 points to next sequential instruction

• Register updated

(BX) = 00FFH

• Memory updated

(1200:1234) = AAH

(1200:1235) = 11H

14 of 66

5.1 Data-Transfer Instructions - Exchange Instruction

  • EXAMPLE

Use the DEBUG to verify the previous example.

  • Solution:

14

CPE 0408330 @ 2008

S. Abed - HU, Jordan

15 of 66

5.1 Data-Transfer Instructions - Exchange Instruction

  • Solution (cont’d):

15

CPE 0408330 @ 2008

S. Abed - HU, Jordan

16 of 66

5.1 Data-Transfer Instructions Translate Instruction

  • The XLAT Instruction

16

CPE 0408330 @ 2008

S. Abed - HU, Jordan

• Translate instruction

• Used to look up a byte-wide value in a table in memory and copy that

value in the AL register

• General format:

XLAT

• Operation: Copies the content of the element pointed to in the source

table in memory to the AL register

((AL)+(BX) +(DS)0) 🡪 (AL)

Where:

(DS)0 = Points to the active data segment

(BX) = Offset to the first element in the table

(AL) = Displacement to the element of the table that is to be accessed*

*8-bit value limits table size to 256 elements

17 of 66

5.1 Data-Transfer Instructions Translate Instruction

17

CPE 0408330 @ 2008

S. Abed - HU, Jordan

• Application: ASCII to EBCDIC Translation

• Fixed EBCDIC table coded into

memory starting at offset in BX

• Individual EBCDIC codes placed in

table at displacement (AL) equal to the

value of their equivalent ASCII character

• A = 41H in ASCII, A = C1H in EBCDIC

• Place the value C1H in memory at

address (A1H+(BX) +(DS)0), etc.

• Example

XLAT

(DS) = 0300H

(BX) = 0100H

(AL) = 3FH 🡪 6FH = ? (Question mark)

18 of 66

5.1 Data-Transfer Instructions Load Effective Address and Load Full Pointer�Instructions

18

CPE 0408330 @ 2008

S. Abed - HU, Jordan

  • The LEA, LDS, and LES Instructions

• Load effective address instruction

• Used to load an address pointer offset from memory into a register.

• General format:

LEA Reg16,EA

• Operation:

(EA) 🡪 (Reg16)

• Source unaffected:

• Flags unaffected

19 of 66

5.1 Data-Transfer Instructions Load Effective Address and Load Full Pointer�Instructions

19

CPE 0408330 @ 2008

S. Abed - HU, Jordan

• Load full pointer

• Used to load a full address pointer from memory into a segment register and a register

• Segment base address

• Offset

• General format and operation for LDS

LDS Reg16,EA

(EA) 🡪 (Reg16)

(EA+2) 🡪 (DS)

• LES operates the same, except initializes ES

20 of 66

5.1 Data-Transfer Instructions Load Effective Address and Load Full Pointer�Instructions

20

CPE 0408330 @ 2008

S. Abed - HU, Jordan

• Example

LDS SI,[200H]

Source = pointer to DS:200H🡪 32 bits

Destination = SI 🡪 word pointer offset

DS 🡪 word pointer SBA

Operation: (DS:200H) 🡪 (SI)

(DS:202H) 🡪 (DS)

• State before fetch and execution

CS:IP = 1100:0100 = 11100H

LDS instruction code = C5360002H

(11102H,11103H) = (EA) = 0200H

(DS) = 1200H

(SI) = XXXX 🡪 don’t care state

(DS:EA) = 12200H = 0020H = Offset

(DS:EA+2) = 12202H = 1300H = SBA

Before execution

21 of 66

5.1 Data-Transfer Instructions Load Effective Address and Load Full Pointer�Instructions

21

CPE 0408330 @ 2008

S. Abed - HU, Jordan

• Example

• State after execution

CS:IP = 1100:0104 = 11104H

01004H 🡪 points to next sequential instruction

(DS) = 1300H 🡪 defines a new data segment

(SI) = 0020H 🡪 defines new offset into DS

After execution

22 of 66

5.1 Data-Transfer Instructions Load Effective Address and Load Full Pointer�Instructions

  • EXAMPLE

Verify the following instruction using DEBUG program.

LDS SI, [200H]

  • Solution:

22

CPE 0408330 @ 2008

S. Abed - HU, Jordan

23 of 66

5.1 Data-Transfer Instructions Load Effective Address and Load Full Pointer�Instructions

  • EXAMPLE

Initializing the internal registers of the 8088 from a table in memory.

  • Solution:

23

CPE 0408330 @ 2008

S. Abed - HU, Jordan

MOV AX, [INIT_TABLE]

MOV SS, AX

LDS SI, [INIT_TABLE+02H]

LES DI, [INIT_TABLE+06H]

MOV AX, [INIT_TABLE+0AH]

MOV BX, [INIT_TABLE+0CH]

MOV CX, [INIT_TABLE+0EH]

MOV DX, [INIT_TABLE+10H]

• DS loaded via AX with immediate value using move instructions

DATA_SEG_ADDR 🡪 (AX) 🡪 (DS)

• Index register SI loaded with move from table

(INIT_TABLE,INIT_TABLE+1) 🡪 SI

• DI and ES are loaded with load full pointer instruction

(INIT_TABLE+2,INIT_TABLE+3) 🡪 DI

(INIT_TABLE+4,INIT_TABLE+5) 🡪 ES

• SS loaded from table via AX using move instructions

(INIT_TABLE+6,INIT_TABLE+7) 🡪 AX 🡪 (SS)

• Data registers loaded from table with move instructions

(INIT_TABLE+8,INIT_TABLE+9) 🡪 AX

(INIT_TABLE+A,INIT_TABLE+B) 🡪 BX

(INIT_TABLE+C,INIT_TABLE+D) 🡪 CX

(INIT_TABLE+E,INIT_TABLE+F) 🡪 DX

LES DI,[INIT_TABLE+2]

24 of 66

5.2 Arithmetic Instructions

  • The arithmetic instructions include
    • Addition
    • Subtraction
    • Multiplication
    • Division
  • Data formats
    • Unsigned binary bytes
    • Signed binary bytes
    • Unsigned binary words
    • Signed binary words
    • Unpacked decimal bytes
    • Packed decimal bytes
    • ASCII numbers

24

CPE 0408330 @ 2008

S. Abed - HU, Jordan

25 of 66

5.2 Arithmetic Instructions - Addition�Instructions

  • Variety of arithmetic instruction provided to support integer addition—core instructions are
    • ADD 🡪 Addition
    • ADC 🡪 Add with carry
    • INC 🡪 Increment
  • Addition Instruction—ADD
    • ADD format and operation:

ADD D,S

(S) +(D) 🡪 (D)

    • Add values in two registers

ADD AX,BX

(AX) + (BX) 🡪 (AX)

    • Add a value in memory and a value in a register

ADD [DI],AX

(DS:DI) + (AX) 🡪 (DS:DI)

    • Add an immediate operand to a value in a register or memory

ADD AX,100H

(AX) + IMM16 🡪 (AX)

  • Flags updated based on result
    • CF, OF, SF, ZF, AF, PF

25

CPE 0408330 @ 2008

S. Abed - HU, Jordan

(a) Addition Instructions. (b) Allowed operands for ADD and ADC (c) Allowed operands for INC

26 of 66

5.2 Arithmetic Instructions - Addition�Instructions

  • EXAMPLE

Assume that the AX and BX registers contain 110016 and 0ABC16, respectively. What is the result of executing the instruction ADD AX, BX?

  • Solution:

26

CPE 0408330 @ 2008

S. Abed - HU, Jordan

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

The sum ends up in destination register AX. That is

(AX) = 1BBC16

CF = 0

27 of 66

5.2 Arithmetic Instructions - Addition�Instructions

27

CPE 0408330 @ 2008

S. Abed - HU, Jordan

• State before fetch and execution

CS:IP = 1100:0100 = 11100H

ADD machine code = 03C3H

(AX) = 1100H

(BX) = 0ABCH

(DS) = 1200H

(1200:0000) = 12000H = XXXX

Before execution

28 of 66

5.2 Arithmetic Instructions - Addition�Instructions

28

CPE 0408330 @ 2008

S. Abed - HU, Jordan

• State after execution

CS:IP = 1100:0102 = 11102H

11102H 🡪 points to next sequential instruction

• Operation performed

(AX) + (BX) 🡪 (AX)

(1100H) + (0ABCH) 🡪 1BBCH

(AX) = 1BBCH

= 00011011101111002

(BX) = unchanged

• Impact on flags

• CF = 0 (no carry resulted)

• ZF = 0 (not zero)

• SF = 0 (positive)

• PF = 0 (odd parity)—parity flag is only based on the bits of the least significant byte

After execution

29 of 66

5.2 Arithmetic Instructions - Addition�Instructions

  • EXAMPLE

Verify the previous example using DEBUG program. Solution:

29

CPE 0408330 @ 2008

S. Abed - HU, Jordan

30 of 66

5.2 Arithmetic Instructions - Addition�Instructions

  • 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?

ADD AX, [SUM]

ADC BL, 05H

INC WORD PTR [SUM]

  • Solution:

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

(BL)←(BL)+imm8+(CF) = AB16 + 516+016 = B016

(SUM)←(SUM)+ 116 = 00CD16 + 116 = 00CE16

30

CPE 0408330 @ 2008

S. Abed - HU, Jordan

31 of 66

5.2 Arithmetic Instructions - Addition�Instructions

  • EXAMPLE

What is the result of executing the following instruction

sequence?

ADD AL, BL

AAA

Assuming that AL contains 3216 (ASCII code for 2) and BL contains 3416 (ASCII code 4), and that AH has been cleared.

  • Solution:

(AL)←(AL)+(BL)= 3216 + 3416=6616

The result after the AAA instruction is

(AL) = 0616

(AH) = 0016

with both AF and CF remain cleared

31

CPE 0408330 @ 2008

S. Abed - HU, Jordan

Important: Any adjustment operation will be performed on AL therefore the result must be always placed in AL before executing the adjustment operation

32 of 66

5.2 Arithmetic Instructions - Addition�Instructions

  • EXAMPLE

Perform a 32-bit binary add operation on the contents of the processor’s register.

  • Solution:

(DX,CX) ← (DX,CX)+(BX,AX)

(DX,CX) = FEDCBA9816

(BX,AX) = 0123456716

MOV DX, 0FEDCH

MOV CX, 0BA98H

MOV BX, 01234H

MOV AX, 04567H

ADD CX, AX

ADC DX, BX ; Add with carry

32

CPE 0408330 @ 2008

S. Abed - HU, Jordan

33 of 66

5.2 Arithmetic Instructions – Subtraction Instructions

  • Variety of arithmetic instruction provided to support integer subtraction—core instructions are

• SUB 🡪 Subtract

• SBB 🡪 Subtract with borrow

• DEC 🡪 Decrement

• NEG 🡪 Negative

33

CPE 0408330 @ 2008

S. Abed - HU, Jordan

(a) Subtraction Instructions. (b) Allowed operands for SUB and SBB (c) Allowed operands for DEC (d) Allowed operands for NEG

34 of 66

5.2 Arithmetic Instructions – Subtraction Instructions

  • Subtract Instruction—SUB
    • SUB format and operation:

SUB D,S

(D) - (S) 🡪 (D)

    • Subtract values in two registers

SUB AX,BX

(AX) - (BX) 🡪 (AX)

    • Subtract a value in memory and a value in a register

SUB [DI],AX

(DS:DI) - (AX) 🡪 (DS:DI)

    • Subtract an immediate operand from a value in a register or memory

SUB AX,100H

(AX) - IMM16 🡪 (AX)

  • Flags updated based on result
    • CF, OF, SF, ZF, AF, PF

34

CPE 0408330 @ 2008

S. Abed - HU, Jordan

(a) Subtraction Instructions. (b) Allowed operands for SUB and SBB (c) Allowed operands for DEC (d) Allowed operands for NEG

35 of 66

5.2 Arithmetic Instructions – Subtraction Instructions

  • Subtract with borrow instruction—SBB
    • SBB format and operation:

SBB D,S

(D) - (S) - (CF) 🡪 (D)

    • Used for extended subtractions
    • Subtracts two registers and carry (borrow)

SBB AX,BX

    • Example:

SBB BX,CX

(BX) = 1234H

(CX) = 0123H

(CF) = 0

(BX) - (CX) - (CF) 🡪 (BX)

1234H - 0123H - 0H = 1111H

(BX) = 1111H

    • What about CF?

35

CPE 0408330 @ 2008

S. Abed - HU, Jordan

36 of 66

5.2 Arithmetic Instructions – Subtraction Instructions

  • Negate instruction—NEG
    • NEG format and operation

NEG D

(0) - (D) 🡪 (D)

(1) 🡪 (CF)

    • Example:

NEG BX

(BX) =003AH

(0) - (BX) 🡪 (BX)

0000H – 003AH=

0000H + FFC6H (2’s complement) = FFC6H

(BX) =FFC6H ; CF =1

  • Since no carry is generated in this add operation, the carry flag is complemented to give CF =1.

36

CPE 0408330 @ 2008

S. Abed - HU, Jordan

37 of 66

5.2 Arithmetic Instructions – Subtraction Instructions

  • Decrement instruction—DEC
    • DEC format and operation

DEC D

(D) - 1 🡪 (D)

    • Used to decrement pointer—addresses
      • Example

DEC SI

(SI) = 0FFFH

(SI) - 1 🡪 SI

0FFFH - 1 = 0FFEH

(SI) = 0FFEH

37

CPE 0408330 @ 2008

S. Abed - HU, Jordan

38 of 66

5.2 Arithmetic Instructions – Subtraction Instructions

  • EXAMPLE

Perform a 32-bit binary subtraction for variable X and Y.

  • Solution:

MOV SI, 200H ; Initialize pointer for X

MOV DI, 100H ; Initialize pointer for Y

MOV AX, [SI] ; Subtract LS words

SUB AX, [DI]

MOV [SI],AX ; Save the LS word of result

MOV AX, [SI]+2 ; Subtract MS words

SBB AX, [DI]+2

MOV [SI]+2, AX ; Save the MS word of result

38

CPE 0408330 @ 2008

S. Abed - HU, Jordan

39 of 66

5.2 Arithmetic Instructions – Multiplication Instructions

  • Integer multiply instructions—MUL and IMUL
    • Multiply two unsigned or signed byte or word operands
  • General format and operation
    • MUL S = Unsigned integer multiply
    • IMUL S = Signed integer multiply

(AL) X (S8)🡪 (AX) 8-bit

product gives 16 bit result

(AX) X (S16) 🡪 (DX), (AX)

16- bit product gives 32 bit result

    • Source operand (S) can be an 8-bit or 16-bit value in a register or memory
    • AX assumed to be destination for 16 bit result
    • DX,AX assumed destination for 32 bit result
    • Only CF and OF flags updated; other undefined

39

CPE 0408330 @ 2008

S. Abed - HU, Jordan

(a) Multiplication and Division Instructions. (b) Allowed operands

40 of 66

5.2 Arithmetic Instructions – Multiplication Instructions

  • EXAMPLE

The 2’s-complement signed data contents of AL are –1 and that of CL are –2. What result is produced in AX by executing the following instruction?

MUL CL and IMUL CL

  • Solution:

(AL) = -1 (as 2’s complement) = 111111112 = FF16

(CL) = -2 (as 2’s complement) = 111111102 = FE16

Executing the MUL instruction gives

(AX) = 111111112x111111102=11111101000000102=FD0216

Executing the IMUL instruction gives

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

40

CPE 0408330 @ 2008

S. Abed - HU, Jordan

If the operation is MUL CX 🡺 multiply CX by AX and store the higher order word of the result in DX and the low order word of the result in AX

41 of 66

5.2 Arithmetic Instructions – Multiplication Instructions

  • In general:

1- The multiplication may take one of two forms

  • Multiply AL by 8-bit operand 🡺 result will be 16-bit saved in AX.
  • Multiply AX by 16-bit operand 🡺 result will be 32 but saved in DX,AX.

2- To perform unsigned multiplication convert the two numbers into binary and perform the multiplication.

3- To perform signed multiplication

  • If both operands are positive or both are negative 🡺 ignore the sign and multiply the numbers normally
  • If one operand is positive and the other is negative 🡺 multiply the numbers and perform 2’s complement for the result

41

CPE 0408330 @ 2008

S. Abed - HU, Jordan

42 of 66

5.2 Arithmetic Instructions – Multiplication Instructions

42

CPE 0408330 @ 2008

S. Abed - HU, Jordan

  • EXAMPLE

Verify the previous example using DEBUG program. Solution:

43 of 66

5.2 Arithmetic Instructions – Division Instructions

  • Integer divide instructions—DIV and IDIV
    • Divide unsigned– DIV S
    • Operations:

(AX) / (S8) 🡪 (AL) =quotient

(AH) = remainder

      • 16 bit dividend in AX divided by 8-bit divisor in a register or memory,
      • Quotient of result produced in AL
      • Remainder of result produced in AH

(DX,AX) / (S16) 🡪 (AX) =quotient

(DX) = remainder

      • 32 bit dividend in DX,AX divided by 16-bit divisor in a register or memory
      • Quotient of result produced in AX
      • Remainder of result produced in DX
    • Divide error (Type 0) interrupt may occur.

43

CPE 0408330 @ 2008

S. Abed - HU, Jordan

(a) Multiplication and Division Instructions. (b) Allowed operands

44 of 66

5.2 Arithmetic Instructions – Convert Instructions

  • Used to sign extension signed numbers for division
  • Operations
    • CBW = convert byte to word

(MSB of AL) 🡪 (all bits of AH)

    • CWD = convert word to double word

(MSB of AX) 🡪 (all bits of DX)

  • Application:
    • To divide two signed 8-bit numbers, the value of the dividend must be sign extended in AX
      • Load into AL
      • Use CBW to sign extend to 16 bits

44

CPE 0408330 @ 2008

S. Abed - HU, Jordan

45 of 66

  • In general:

1- The division may take one of two forms

  • Divide AX by 8-bit operand 🡺 The division is performed between AX/ 8-bit operand. AL will contain the quotient of the result and AH will contain the remainder of the result. IF quotient is FF then interrupt occurs.
  • Divide DX,AX by 16-bit operand 🡺 The division is performed between DX,AX/ 16-bit operand. AX will contain the quotient of the result and DX will contain the remainder of the result. IF quotient is FFFF then interrupt occurs.

2- The way in which you perform either a singed or unsigned division is similar to the mechanism used in the multiplication instruction

3- The sign for the remainder is always similar to the sign of the dividend ex. -26 / 8 🡺 Quotient=-3 and Remainder = -2

45

CPE 0408330 @ 2008

S. Abed - HU, Jordan

46 of 66

5.2 Arithmetic Instructions – Convert Instructions

  • EXAMPLE

What is the result of executing the following instructions?

MOV AL, 0A1H

CBW

CWD

  • Solution:

(AL) = A116 = 101000012

Executing the CBW instruction extends the MSB of AL

(AH) = 111111112 = FF16 or (AX) = 11111111101000012

Executing CWD instruction, we get

(DX) = 11111111111111112 = FFFF16

That is,

(AX) = FFA116 (DX) = FFFF16

46

CPE 0408330 @ 2008

S. Abed - HU, Jordan

47 of 66

5.3 Logic Instructions

  • Variety of logic instructions provided to support logical computations
    • AND 🡪 Logical AND
    • OR 🡪 Logical inclusive-OR
    • XOR 🡪 Logical exclusive-OR
    • NOT 🡪 Logical NOT
  • Logical AND Instruction—AND
    • AND format and operation:

AND D,S

(S) AND (D) 🡪 (D)

    • Logical AND of values in two registers

AND AX,BX

(AX) AND (BX) 🡪 (AX)

    • Logical AND of a value in memory and a value in a register

AND [DI],AX

(DS:DI) AND (AX) 🡪 (DS:DI)

    • Logical AND of an immediate operand with a value in a register or memory

AND AX,100H

(AX) AND IMM16  (AX)

    • Flags updated based on result
      • CF, OF, SF, ZF, PF
      • AF undefined

47

CPE 0408330 @ 2008

S. Abed - HU, Jordan

(a) Logic Instructions. (b) Allowed operands for AND, OR, and XOR (c) Allowed operands for NOT

48 of 66

5.3 Logic Instructions

48

CPE 0408330 @ 2008

S. Abed - HU, Jordan

  • EXAMPLE

Describe the results of executing the following instructions?

MOV AL, 01010101B

AND AL, 00011111B

OR AL, 11000000B

XOR AL, 00001111B

NOT AL

  • Solution:

(AL)=010101012 ⋅ 000111112= 000101012=1516

Executing the OR instruction, we get

(AL)= 000101012 +110000002= 110101012=D516

Executing the XOR instruction, we get

(AL)= 110101012 ⊕ 000011112= 110110102=DA16

Executing the NOT instruction, we get

(AL)= (NOT)110110102 = 001001012=2516

49 of 66

5.3 Logic Instructions

49

CPE 0408330 @ 2008

S. Abed - HU, Jordan

  • EXAMPLE

Verify the previous example using DEBUG program.

  • Solution:

50 of 66

5.3 Logic Instructions - Mask Application

50

CPE 0408330 @ 2008

S. Abed - HU, Jordan

  • Application– Masking bits with the logic instructions

• Mask—to clear a bit or bits of a byte or word to 0

• AND operation can be used to perform the mask operation

• 1 AND 0 🡪 0; 0 and 0 🡪 0

• A bit or bits are masked by ANDing with 0

• 1 AND 1 🡪 1; 0 AND 1 🡪 0

• ANDing a bit or bits with 1 results in no change

• Example: Masking the upper 12 bits of a value in a register

AND AX,000FH

(AX) =FFFF

IMM16 AND (AX) 🡪 (AX)

000FH AND FFFFH = 00000000000011112 AND 11111111111111112

= 00000000000011112

= 000FH

51 of 66

5.3 Logic Instructions - Mask Application

51

CPE 0408330 @ 2008

S. Abed - HU, Jordan

OR operation can be used to set a bit or bits of a byte or word to 1

X OR 0 🡪 X; result is unchanged

X or 1 🡪 1; result is always 1

Example: Setting a control flag in a byte memory location to 1

MOV AL,[CONTROL_FLAGS]

OR AL, 10H ; 00010000 sets fifth bit –b4

MOV [CONTROL_FLAGS],AL

  • Executing the above instructions, we get

(AL) = XXXXXXXX2 OR 000100002 = XXX1XXXX2

Setting

52 of 66

5.4 Shift Instructions

  • Variety of shift instructions provided
    • SAL/SHL 🡪 Shift arithmetic left/shift logical left
    • SHR 🡪 Shift logical right
    • SAR 🡪 Shift arithmetic right
  • Perform a variety of shift left and shift right operations on the bits of a destination data operand
  • Basic shift instructions—SAL/SHL, SHR, SAR
    • Destination may be in either a register or a storage location in memory
    • Shift count may be:

1= one bit shift

CL = 1 to 255 bit shift

  • Flags updated based on result
    • CF, SF, ZF, PF
    • AF undefined
    • OF undefined if Count ≠ 1

52

CPE 0408330 @ 2008

S. Abed - HU, Jordan

(a) Shift Instructions. (b) Allowed operands

Every shift operation is equivalent to :

* multiplication by 2 for Shift left

* dividing by 2 for Logical Shift right

53 of 66

5.4 Shift Instructions - Operation of the SAL/SHL Instruction

  • Typical instruction—count of 1

SHL AX,1

  • Before execution

Dest = (AX) = 1234H

= 00010010001101002

Count = 1

CF = X

  • Operation:
    • The value in all bits of AX are shifted left one bit position
    • Emptied LSB is filled with 0
    • Value shifted out of MSB goes to carry flag
  • After execution

Dest = (AX) = 2468H

= 00100100011010002

CF = 0

  • Conclusion:
    • MSB has been isolated in CF and can be acted upon by control flow instruction– conditional jump
    • Result has been multiplied by 2

53

CPE 0408330 @ 2008

S. Abed - HU, Jordan

54 of 66

5.4 Shift Instructions - Operation of the SHR Instruction

  • Typical instruction—count in CL

SHR AX,CL

  • Before execution

Dest = (AX) = 1234H = 466010

= 00010010001101002

Count = (CL) = 02H

CF = X

  • Operation:
    • The value in all bits of AX are shifted right two bit positions
    • Emptied MSBs are filled with 0s
    • Value shifted out of LSB goes to carry flag
  • After execution

Dest = (AX) = 048DH = 116510

= 00000100100011012

CF = 0

  • Conclusion
    • Bit 1 has been isolated in CF and can be acted upon by control flow instruction– conditional jump
    • Result has been divided

54

CPE 0408330 @ 2008

S. Abed - HU, Jordan

55 of 66

5.4 Shift Instructions - Operation of the SAR Instruction

  • Typical instruction—count in CL

SAR AX,CL

  • Before execution—arithmetic implies signed numbers

Dest = (AX) = 091AH

= 00001001000110102 = +2330

Count = CL = 02H

CF = X

  • Operation:
    • The value in all bits of AX are shifted right two bit positions
    • Emptied MSB is filled with the value of the sign bit
    • Values shifted out of LSB go to carry flag
  • After execution

Dest = (AX) = 0246H

= 00000010010001102 = +582

CF = 1

  • Conclusion
    • Bit 1 has been isolated in CF and can be acted upon by control flow instruction– conditional jump
    • Result has been signed extended
    • Result value has been divided by 4 and rounded to integer: 4 X +582 = +2328

55

CPE 0408330 @ 2008

S. Abed - HU, Jordan

56 of 66

5.4 Shift Instructions - Operation of the SAR Instruction

56

CPE 0408330 @ 2008

S. Abed - HU, Jordan

  • EXAMPLE

Assume that CL contains 0216 and AX contains 091A16. Determine the new contents of AX and the carry flag after the instruction SAR AX, CL is executed.

  • Solution:

Initial (AX)=00001001000110102

shift AX right twice: (AX)=00000010010001102=024616

and the carry flag is (CF)=12

57 of 66

5.4 Shift Instructions - Operation of the SAR Instruction

57

CPE 0408330 @ 2008

S. Abed - HU, Jordan

  • EXAMPLE

Verify the previous example using DEBUG program.

  • Solution:

(AX)=00000010010001102=024616

and the carry flag is (CF)=12

58 of 66

5.4 Shift Instructions - Operation of the SAR Instruction

58

CPE 0408330 @ 2008

S. Abed - HU, Jordan

  • EXAMPLE

Isolate the bit B3 of the byte at the offset address CONTROL_FLAGS.

  • Solution:

MOV AL, [CONTROL_FLAGS]

MOV CL, 04H

SHR AL, CL

Executing the instructions, we get

(AL)=0000B7B6B5B4

and

(CF)=B3

59 of 66

5.5 Rotate Instructions

  • Variety of rotate instructions provided:
    • ROL 🡪 Rotate left
    • ROR 🡪 Rotate right
    • RCL 🡪 Rotate left through carry
    • RCR 🡪 Rotate right through carry
  • Perform a variety of rotate left and rotate right operations on the bits of a destination data operand
    • Overview of function
    • Destination may be in either a register or a storage location in memory
    • Rotate count may be:

1= one bit rotate

CL = 1 to 255 bit rotate

    • Flags updated based on result
      • CF
      • OF undefined if Count ≠ 1
  • Used to rearrange information

59

CPE 0408330 @ 2008

S. Abed - HU, Jordan

(a) Rotate Instructions. (b) Allowed operands

60 of 66

5.5 Rotate Instructions - Operation of the ROL Instruction

  • Typical instruction—count of 1

ROL AX,1

  • Before execution

Dest = (AX) = 1234H

= 0001 0010 0011 01002

Count = 1

CF = 0

  • Operation
    • The value in all bits of AX are rotated left one bit position
    • Value rotated out of the MSB is reloaded at LSB
    • Value rotated out of MSB is copied to carry flag
  • After execution

Dest = (AX) = 2468H

= 0010 0100 0110 10002

CF = 0

60

CPE 0408330 @ 2008

S. Abed - HU, Jordan

61 of 66

5.5 Rotate Instructions - Operation of the ROR Instruction

  • Typical instruction—count in CL

ROR AX,CL

  • Before execution

Dest = (AX) = 1234H

= 00010010001101002

Count = 04H

CF = 0

  • Operation
    • The value in all bits of AX are rotated right four bit positions
    • Values rotated out of the LSB are reloaded at MSB
    • Values rotated out of MSB copied to carry flag
  • After execution

Dest = (AX) = 4123H

= 01000001001000112

CF = 0

  • Conclusion:
    • Note that the position of hex characters in AX have be rearranged

61

CPE 0408330 @ 2008

S. Abed - HU, Jordan

62 of 66

5.5 Rotate Instructions - Operation of the RCL Instruction

  • • Typical instruction—count in CL

RCL BX,CL

  • Before execution

Dest = (BX) = 1234H

= 00010010001101002

Count = (CL) = 04H

CF = 0

  • Operation
    • The value in all bits of AX are rotated left four bit positions
    • Emptied MSBs are rotated through the carry bit back into the LSB
    • First rotate loads prior value of CF at the LSB
    • Last value rotated out of MSB retained in carry flag
  • After execution

Dest = (BX) = 2340H

= 00100011010000002

CF = 1

62

CPE 0408330 @ 2008

S. Abed - HU, Jordan

63 of 66

5.5 Rotate Instructions - Operation of the RCR Instruction

63

CPE 0408330 @ 2008

S. Abed - HU, Jordan

  • EXAMPLE

What is the result in BX and CF after execution of the following

instructions?

RCR BX, CL

Assume that, prior to execution of the instruction, (CL)=0416,

(BX)=123416, and (CF)=0

  • Solution:

The original contents of BX are

(BX) = 00010010001101002 = 123416

Execution of the RCR command causes a 4-bit rotate right through carry to take place on the data in BX, the results are

(BX) = 10000001001000112 = 812316

(CF) = 02

64 of 66

5.5 Rotate Instructions - Operation of the RCR Instruction

64

CPE 0408330 @ 2008

S. Abed - HU, Jordan

  • EXAMPLE

Verify the previous example using DEBUG program.

  • Solution:

65 of 66

5.5 Rotate Instructions

65

CPE 0408330 @ 2008

S. Abed - HU, Jordan

  • EXAMPLE

Disassembly and addition of 2 hexadecimal digits stored as a

byte in memory.

  • Solution:

1st Instruction 🡪 Loads AL with byte

containing two hex digits

2nd Instruction 🡪 Copies byte to BL

3rd Instruction 🡪 Loads rotate count

4th instruction 🡪 Aligns upper hex digit of BL with lower digit in AL

5th Instruction 🡪 Masks off upper hex digit in AL

6th Instruction 🡪 Masks off upper four bits of BL

7th Instruction 🡪 Adds two hex digits

66 of 66

  • Solve the following problems from Chapter 5 from the course textbook:

1, 10, 26, 38, 47

66

CPE 0408330 @ 2008

S. Abed - HU, Jordan

H.W. #5