+ All Categories
Home > Documents > AR Chapter5 v2

AR Chapter5 v2

Date post: 03-Jun-2018
Category:
Upload: mohamed-nabil
View: 214 times
Download: 0 times
Share this document with a friend

of 53

Transcript
  • 8/11/2019 AR Chapter5 v2

    1/53

    ECE 2211

    Microprocessor and Interfacing

    Br. Athaur Rahman Bin Najeeb

    Room 2.105

    Email: [email protected]

    Website: http://eng.iiu.edu.my/~athaur

    Consultation : Tuesday 10.00 am ( appointment)

  • 8/11/2019 AR Chapter5 v2

    2/53

    Announcement

  • 8/11/2019 AR Chapter5 v2

    3/53

    Chapter 5:P 8088/8086 Programming

    Recall of debugging

    Assembler

    Instructions

    Data transfer instructions

    Arithmetic instructions

    Shift instructions

    Rotate instructions

    Useful Link: http://oopweb.com/Assembly/Documents/Art

    OfAssembly/Volume/Chapter_6/CH06-1.html

  • 8/11/2019 AR Chapter5 v2

    4/53

    Debug Debug is a little tool that comes with windows

    Part of PCs Disk Operating System ( DOS)

    Invoking debug

    Start -> Run-> type cmd -> typedebug

    Start -> Run ->type debug

    Debug Commands / Monitor Commands

    A

    I

    T

    G H

    M

    E

    F

    R

    C

  • 8/11/2019 AR Chapter5 v2

    5/53

    Structure of ASM file

  • 8/11/2019 AR Chapter5 v2

    6/53

  • 8/11/2019 AR Chapter5 v2

    7/53

    Mnemonic Meaning Format Operation Flagsaffected

    Mov Move Mov D,S (s) (D) None

    Data Transfer Instructions - MOV

    Destination Source

    Memory Accumulator

    Register Register

    Register Memory

    Memory Register

    Register Immediate

    Memory Immediate

    Seg reg Reg 16

    Seg reg Mem 16

    Reg 16 Seg reg

    Memory Seg reg

    NO MOV

    Mem

    ImmSeg Reg

    Mem

    Seg RegSeg reg

    EX: MOV AL, BL

  • 8/11/2019 AR Chapter5 v2

    8/53

    Data Transfer Instruction -MOV

    The MOV instruction Move data from source to destination; e.g. MOV [DI+100H], AH It does not modify flags Cannot transfer data between 2 external memory address

    Eg: MOV [SI],[1234] Cannot move (load) directly to Segment Registers

    Eg: MOV DS, 1000

    Byte Operation / Word Operation Question [Example 5.1 , text book ? ]

    What is the effect of executing MOV CX, [ SOURCE_MEMORY ] .. Where

    SOURCE_MEMORY equal 20(H) and DS equal 1A00[H]

  • 8/11/2019 AR Chapter5 v2

    9/53

    Ex: Mov dx,cs

  • 8/11/2019 AR Chapter5 v2

    10/53

    Data Transfer Instructions - XCHG

    Mnemonic Meaning Format Operation Flags affected

    XCHG Exchange XCHG D,S (s) (D) None

    Destination Source

    Accumulator Reg 16

    Memory Register

    Register Register

    Register Memory

    Example: XCHG [1234h], BX

    NO XCHGMEMsSEG REGs

  • 8/11/2019 AR Chapter5 v2

    11/53

    Example XCHG

    Example:

    For the data shown, what is the result of executing XCHG [SUM], Bx ?

    [SUM] = 1234(16)

    Solution:Execution of this instruction performs the operation

    ((DS)0 + SUM)(BX)

    The PA is

    = + =

    Then,

    13234(16)(BL)

    13235(16)(BH)

    Result: (BX) = 00FF(16)

  • 8/11/2019 AR Chapter5 v2

    12/53

    Your First Program Write a ASM program to load values 0011(H), 00AB(H) into regiter AX, BX and swap

    them

    Read from memory location 0200(H) and swap with register AX

    Step 1: Flow Chart (? )

    Step 2 Translate to Assembly Language

    Step 3: write, test, debug and execute the code

    Answer

  • 8/11/2019 AR Chapter5 v2

    13/53

    Data Transfer Instructions LEA, LDS, LES

    Loading GP registers and segment registers with an address directly from memory

    Ex: LEA SI,[EA]

    LEA SI, [DI+BX+5H] ; DI=1000(H), Bx=20(H), EA ? PA = DS[0] +EA LEA SI, [200h] , if DS=1200 , calculate the PA

    LEA Destination, Source

    Transfers the offset address of source (must be a memory location) to the destination register

    It does not modify flags

    LES Destination Source It is identical to LDS except that the second two bytes are copied to ES It does not modify flags

    Load 4-byte data (pointer) in memory to two 16-bit registers Source operand gives the memory location The first two bytes are copied to the register specified in the destination operand; the second

    two bytes are copied to register DS It does not modify flags

  • 8/11/2019 AR Chapter5 v2

    14/53

    Data Transfer Instructions LEA, LDS, LES

    Mnemonic

    Meaning Format Operation Flagsaffected

    LEA Load Effective

    Address

    LEA Reg16,EA EA (Reg16) None

    LDS Load RegisterAnd DS

    LDSReg16,MEM32

    (MEM32)(Reg16)

    None

    (Mem32+2) (DS)

    LES Load Registerand ES

    LESReg16,MEM32

    (MEM32)(Reg16)

    (Mem32+2) (DS)

    None

    LEA SI DATA or MOV SI Offset DATA

  • 8/11/2019 AR Chapter5 v2

    15/53

    Examples for LEA, LDS, LES

    DATAX DW 1000H DATAY DW 5000H .CODE LEA SI, DATAX MOVE DI, OFFSET DATAY; THIS IS MORE EFFICIENT LEA BX,[DI]; IS THE SAME AS MOV BX,DI; THIS JUST TAKES LESS CYCLES. LEA BX,DI; INVALID!

    LES similar to LDS except that it loads ES

    LDS BX, [DI];

    127ABX

    1000DI

    3000DS

    7A

    12

    00

    30

    11000

    11001

    11002

    11003

  • 8/11/2019 AR Chapter5 v2

    16/53

    Data Transfer Instructions SAHF

    LAHF

    Store data in AH to the low 8 bits of the flag register

    It modifies flags: AF, CF, PF, SF, ZF

    Copies bits 0-7 of the flags register into AH

    It does not modify flags

    LDS Destination Source

    Load 4-byte data (pointer) in memory to two 16-bit registers

    Source operand gives the memory location

    The first two bytes are copied to the register specified in the destination operand;

    the second two bytes are copied to register DS

    It does not modify flags

    LES Destination Source

    It is identical to LDS except that the second two bytes are copied to ES

    It does not modify flags

  • 8/11/2019 AR Chapter5 v2

    17/53

    Arithmetric Instruction

    Addition

    Subtraction

    Multiplication Division

  • 8/11/2019 AR Chapter5 v2

    18/53

    Arithmetic Instructions ADDDestination, Source

    Destination + Source Destination

    Destination and Source operands can not be memory locations at the same time

    It modifies flags AF CF OF PF SF ZF

    ADCDestination, Source

    estination + ource + arry ag estination

    Destination and Source operands can not be memory locations at the same time

    It modifies flags AF CF OF PF SF ZF

    INCDestination

    Destination + 1 Destination

    It modifies flags AF OF PF SF ZF (Note CF will not be changed)

    DECDestination

    Destination - 1 Destination

    It modifies flags AF OF PF SF ZF (Note CF will not be changed)

  • 8/11/2019 AR Chapter5 v2

    19/53

    Arithmetic Instructions

    ADD, ADC, INC, AAA, DAA

    Mnemonic Meaning Format Operation Flagsaffected

    ADD Addition ADD D,S (S)+(D) (D)

    carry (CF)

    ALL

    ADC Add withcarr

    ADC D,S (S)+(D)+(CF) (D)

    ALL

    INC Incrementby one

    INC D (D)+1 (D) ALL but CY

    AAA ASCIIadjust foraddition

    AAA If the sum is >9, AH

    is incremented by 1

    AF,CF

    DAA Decimaladjust foraddition

    DAA Adjust AL for decimalPacked BCD

    ALL

  • 8/11/2019 AR Chapter5 v2

    20/53

    Arithmetic Instructions SUB, SBB, DEC, AAS, DAS,

    NEG

    Mnemonic Meaning Format Operation Flagsaffected

    SUB Subtract SUB D,S (D)-(S) (D)Borrow (CF)

    All

    SBB Subtractwith borrow

    SBB D,S (D)-(S)-(CF) (D) All

    DEC Decrementby one

    DEC D (D)-1 (D) All but CF

    NEG Negate NEG D All

    DAS Decimaladjust for

    subtraction

    DAS Convert the result in AL topacked decimal format

    All

    AAS ASCIIadjust for

    subtraction

    AAS (AL) difference

    (AH) dec by 1 if borrow

    CY,AC

  • 8/11/2019 AR Chapter5 v2

    21/53

    Arithmetic Instructions

    SUBDestination, Source Destination - Source Destination

    Destination and Source operands can not be memory locations at the same time

    It modifies flags AF CF OF PF SF ZF

    SBBDestination, Source

    estination - ource - arry ag estination

    Destination and Source operands can not be memory locations at the same time

    It modifies flags AF CF OF PF SF ZF

    CMPDestination, Source

    Destination Source (the result is not stored anywhere)

    Destination and Source operands can not be memory locations at the same time

    It modifies flags AF CF OF PF SF ZF (if ZF is set, destination = source)

  • 8/11/2019 AR Chapter5 v2

    22/53

    Example 1

    Write a program to add a data byte located at offset 0500(h) in 2000h data segmentWITH an other byte which is available in 0700h in the same segment

  • 8/11/2019 AR Chapter5 v2

    23/53

    Example 2

    Write a program to move the contects of the ML 0500h to register BX and to registerCX. Add immediate byte 05h to the data residing in ML whose address computedusing ds-2000h and offset 0600h. Store the result of addition in 0700h. Assume alldata is located in ds=2000h

    start

    Move content to BX

    Move content to CX

    Move data , 05h to [0600h]

    Store result

    End

  • 8/11/2019 AR Chapter5 v2

    24/53

    Arithmetic Instructions

    MUL Source

    Perform unsigned multiply operation

    If source operand is a byte, AX = AL * Source

    If source operand is a word, (DX AX) = AX * Source

    Source operands can not be an immediate data

    It modifies CF and OF (AF,PF,SF,ZF undefined)

    IMUL Source

    Perform signed binary multiply operation If source operand is a byte, AX = AL * Source

    If source operand is a word, (DX AX) = AX * Source

    Source operands can not be an immediate data

    It modifies CF and OF (AF,PF,SF,ZF undefined)

    Examples:MOV AL, 20H

    MOV CL, 80H

    MUL CL

    MOV AL, 20H

    MOV CL, 80H

    IMUL CL

  • 8/11/2019 AR Chapter5 v2

    25/53

    DIV Source

    Perform unsigned division operation

    If source operand is a byte, AL = AX / Source; AH = Remainder of AX / Source

    If source operand is a word, AX=(DX AX)/Source; DX=Remainder of (DX AX)/Source

    Source operands can not be an immediate data

    IDIV Source

    Arithmetic Instructions

    Examples:MOV AX, 5

    MOV BL, 2

    DIV BL

    MOV AL, -5

    MOV BL, 2

    IDIV BL

    Perform signed division operation

    If source operand is a byte, AL = AX / Source; AH = Remainder of AX / Source

    If source operand is a word, AX=(DX AX)/Source; DX=Remainder of (DX AX)/Source

    Source operands can not be an immediate data

  • 8/11/2019 AR Chapter5 v2

    26/53

    Summary so far

  • 8/11/2019 AR Chapter5 v2

    27/53

    Arithmetic Instructions

    NEG Destination

    0 Destination Destination (the result is represented in 2s complement)

    Destination can be a register or a memory location

    It modifies flags AF CF OF PF SF ZF

    CBW Extends a signed 8-bit number in AL to a signed 16-bit data and stores it into AX

    CWD

    Extends a signed 16-bit number in AX to a signed 32-bit data and stores it into DX

    and AX. DX contains the most significant word

    It does not modify flags

    Other arithmetic instructions:

    DAA, DAS, AAA, AAS, AAM, AAD

  • 8/11/2019 AR Chapter5 v2

    28/53

    Example

    What is the result of executing the following sequence of instruction ?

    MOV AL, 0A1H

    CBW

    CWD

  • 8/11/2019 AR Chapter5 v2

    29/53

    Logical Instructions ( 3)

    NOT Destination

    Inverts each bit of the destination operand

    Destination can be a register or a memory location

    It does not modify flags

    AND Destination, Source

    Performs logic AND operation for each bit of the destination and source; stores the

    resu n o es na on

    Destination and source can not be both memory locations at the same time It modifies flags: CF OF PF SF ZF

    OR Destination, Source

    Performs logic OR operation for each bit of the destination and source; stores the

    result into destination Destination and source can not be both memory locations at the same time

    It modifies flags: CF OF PF SF ZF

  • 8/11/2019 AR Chapter5 v2

    30/53

  • 8/11/2019 AR Chapter5 v2

    31/53

    Logic operation: a review

  • 8/11/2019 AR Chapter5 v2

    32/53

  • 8/11/2019 AR Chapter5 v2

    33/53

    Clearing Setting, Toggling bit of an Operand

    Mask : clear a bit to zero

    To clear a bit, we AND it with zero

    AND with 1 : unchanged

    Example: AND AX, 000F; if AX = FFFF Setting a bit to 1: OR with logic 1

    XOR to reverse the login level

    AND AL, 0FH (given AL=FF)

    Application example:

    A microprocessor is connected to read 8 LEDs into AL , if LED number 0 is on, thenturn on the alarm

  • 8/11/2019 AR Chapter5 v2

    34/53

    SHIFT INSTRUCTIONS

  • 8/11/2019 AR Chapter5 v2

    35/53

    A logical shift fills the newly created bit position with zero:

    Logical VS Arithmetic Shifts

    CF

    0

    An arithmetic shift fills the newly created bit position with a copy of the numberssign bit:

    CF

  • 8/11/2019 AR Chapter5 v2

    36/53

    SHIFT

    ARITHMETIC LOGICAL

    Shift Instructions : 4 types

    LEFT LEFTRIGHT RIGHT

    SAL SAR SHL SHR

  • 8/11/2019 AR Chapter5 v2

    37/53

  • 8/11/2019 AR Chapter5 v2

    38/53

    Bit Manipulation Instructions

    SHL(SAL) Destination, Count Left shift destination bits; the number of bits shifted is given by operand Count

    During the shift operation, the MSB of the destination is shifted into CF and

    zero is shifted into the LSB of the destination Operand Count can be either an immediate data or register CL

    Destination can be a register or a memory location

    It modifies flags: CF OF PF SF ZF

    CF 0

    SHR Destination, Count Right shift destination bits; the number of bits shifted is given by operand Count

    During the shift operation, the LSB of the destination is shifted into CF and

    zero is shifted into the MSB of the destination

    Operand Count can be either an immediate data or register CL Destination can be a register or a memory location

    It modifies flags: CF OF PF SF ZF

    CF0 Destination

    Destination

    LSBMSB

    LSBMSB

  • 8/11/2019 AR Chapter5 v2

    39/53

    Bit Manipulation Instructions

    SAR Destination, Count

    Right shift destination bits; the number of bits shifted is given by operand Count

    The LSB of the destination is shifted into CF and the MSB of the destination remians

    the same

    Operand Count can be either an immediate data or register CL

    Destination can be a register or a memory location

    It modifies flags: CF PF SF ZF

    CFDestination

    LSBMSB

  • 8/11/2019 AR Chapter5 v2

    40/53

  • 8/11/2019 AR Chapter5 v2

    41/53

    Fast Multipication

  • 8/11/2019 AR Chapter5 v2

    42/53

  • 8/11/2019 AR Chapter5 v2

    43/53

    ROTATE INSTRUCTIONS

  • 8/11/2019 AR Chapter5 v2

    44/53

  • 8/11/2019 AR Chapter5 v2

    45/53

    Bit Manipulation Instructions

    ROL Destination, Count

    Left shift destination bits; the number of bits shifted is given by operand Count

    The MSB of the destination is shifted into CF, it also goes to the LSB of the destination

    Operand Count can be either an immediate data or register CL Destination can be a register or a memory location

    It modifies flags: CF OF

    CF Destination

    LSBMSB

    ROR Destination, Count

    Right shift destination bits; the number of bits shifted is given by operand Count

    The LSB of the destination is shifted into CF, it also goes to the MSB of the destination

    Operand Count can be either an immediate data or register CL

    Destination can be a register or a memory location

    It modifies flags: CF OF

    CFDestination

    LSBMSB

  • 8/11/2019 AR Chapter5 v2

    46/53

    Bit Manipulation Instructions

    RCL Destination, Count

    Left shift destination bits; the number of bits shifted is given by operand Count

    The MSB of the destination is shifted into CF; the old CF value goes to the LSB

    of the destination Operand Count can be either an immediate data or register CL

    Destination can be a register or a memory location

    It modifies flags: CF OF PF SF ZFCF Destination

    LSBMSB

    RCR Destination, Count

    Right shift destination bits; the number of bits shifted is given by operand Count

    The LSB of the destination is shifted into CF, the old CF value goes to the MSB

    of the destination

    Operand Count can be either an immediate data or register CL

    Destination can be a register or a memory location It modifies flags: CF OF PF SF ZF

    CFDestination

    LSBMSB

  • 8/11/2019 AR Chapter5 v2

    47/53

  • 8/11/2019 AR Chapter5 v2

    48/53

    Example: Using ROL to Exchange Values.

    .data

    byteval db 0Fh

    wordval dw 1234h

    .code

    byte_values:

    ,

    ROL AL,4 ; AL = 62hROL byteval,4 ; byteval = F0h

    word_values:

    MOV AX,0203h

    ROL AX,8 ; AX = 0302h

    ROL wordval,8 ; wordval = 3412h

  • 8/11/2019 AR Chapter5 v2

    49/53

  • 8/11/2019 AR Chapter5 v2

    50/53

  • 8/11/2019 AR Chapter5 v2

    51/53

    Example:

    What is the result in BX and CF after execution of

    the following instruction?

    RCR BX,CL

    ,

  • 8/11/2019 AR Chapter5 v2

    52/53

    Example:

    Indicate the hexadecimal value of AL after each rotation

    MOV AL,6BH

    ROR AL,1

    RCL AL,3

  • 8/11/2019 AR Chapter5 v2

    53/53

    ANSWER:

    A B5H

    B AEH


Recommended