06 stackler ve makrolar1

Upload: carolyn-fields

Post on 04-Jun-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 06 Stackler Ve Makrolar1

    1/23

    Programming 8086 Part IV

    Stacks, Macros

    1

    Microprocessors

  • 8/13/2019 06 Stackler Ve Makrolar1

    2/23

    Contents

    2

    Stacks

    Macros

  • 8/13/2019 06 Stackler Ve Makrolar1

    3/23

    Stack

    3

    Stack is an area of memory for keepingtemporary data

    Stack is used by CALLinstruction to keep return

    address for procedure, RETinstruction gets this

    value from the stack and returns to that offset

    The stack can also be used to keep any other

    data

    There are two instructions that work with thestack:

    PUSH- stores 16 bit value in the stack.

    POP- gets 16 bit value from the stack.

  • 8/13/2019 06 Stackler Ve Makrolar1

    4/23

    PUSH

    4

    Syntax for PUSHinstruction:PUSH REG

    PUSH SREG

    PUSH memory

    PUSH immediate (works only on 80186 and later)

    REG: AX, BX, CX, DX, DI, SI, BP, SP

    SREG: DS, ES, SS, CS

    memory: [BX], [BX+SI+7], 16 bit variable, etc. immediate: 5, -24, 3Fh, 10001101b, etc.

  • 8/13/2019 06 Stackler Ve Makrolar1

    5/23

    POP

    5

    Syntax for POPinstruction:POP REG

    POP SREG

    POP memory

    REG: AX, BX, CX, DX, DI, SI, BP, SP

    SREG: DS, ES, SS, (except CS)

    memory: [BX], [BX+SI+7], 16 bit variable, etc.

  • 8/13/2019 06 Stackler Ve Makrolar1

    6/23

    LIFO

    6

    The stack uses LIFO(Last In First Out)

    algorithm, this means

    that if we push these

    values one by oneinto the stack:

    1, 2, 3, 4, 5

    the first value that we

    will get on pop willbe 5, then 4, 3, 2, and

    only then 1.

  • 8/13/2019 06 Stackler Ve Makrolar1

    7/23

    Using the Stack

    7

    It is very important to do equal number of PUSHs

    and POPs, otherwise the stack maybe corrupted and

    it will be impossible to return to operating system.

    PUSHand POPinstruction are especially usefulbecause we don't have too much registers to operate

    with, so here is a trick:

    Store original value of the register in stack

    (using PUSH). Use the register for any purpose.

    Restore the original value of the register from stack

    (using POP).

  • 8/13/2019 06 Stackler Ve Makrolar1

    8/23

    A Simple Stack Example

    8

    ORG 100h

    MOV AX, 1234h

    PUSH AX ; store value of AX in stack.

    MOV AX, 5678h ; modify the AX value.POP AX ; restore the original value of AX.

    RET

    END

  • 8/13/2019 06 Stackler Ve Makrolar1

    9/23

    Exchanging Values Using Stack

    9

    ORG 100hMOV AX, 1234h ; store 1234h in AX.

    MOV BX, 5678h ; store 5678h in BX

    PUSH AX ; store value of AX in stack.

    PUSH BX ; store value of BX in stack.

    POP AX ; set AX to original value of BX.

    POP BX ; set BX to original value of AX.

    RET

    END

  • 8/13/2019 06 Stackler Ve Makrolar1

    10/23

    SS and SP

    10

    The stack memory area is set by SS(Stack Segment) register,and SP(Stack Pointer) register.

    Generally operating system sets values of these registers onprogram start.

    The current address pointed by SS:SPis called the top of thestack.

    For COMfiles stack segment is generally the code segment, andstack pointer is set to value of 0FFFEh. At the addressSS:0FFFEh stored a return address for RETinstruction that isexecuted in the end of the program.

    You can visually see the stack operation by clicking on [Stack]button on emulator window. The top of the stack is marked with

    "

  • 8/13/2019 06 Stackler Ve Makrolar1

    11/23

    PUSH and POP

    11

    "PUSH source " instruction does the following: Subtract 2from SPregister.

    Write the value of source to the address SS:SP.

    "POP dest inat ion " instruction does the following:

    Write the value at the

    address SS:SPto dest inat ion .

    Add 2to SPregister.

  • 8/13/2019 06 Stackler Ve Makrolar1

    12/23

    Initial State of the Stack

    12

    SS

    FFFF

    FFFE

    FFFD

    FFFC

    FFFB

    FFFA

    0000

    (TOS) SP

    1234H

    5678H

    AX

    BX

  • 8/13/2019 06 Stackler Ve Makrolar1

    13/23

    After PUSH AX

    13

    12

    34

    SS

    FFFF

    FFFE

    FFFD

    FFFC

    FFFB

    FFFA

    0000

    (TOS) SP

    1234H

    5678H

    AX

    BX

  • 8/13/2019 06 Stackler Ve Makrolar1

    14/23

    After PUSH BX

    14

    12

    34

    56

    78

    SS

    FFFF

    FFFE

    FFFD

    FFFC

    FFFB

    FFFA

    0000

    (TOS) SP

    1234H

    5678H

    AX

    BX

  • 8/13/2019 06 Stackler Ve Makrolar1

    15/23

    After POP AX

    15

    12

    34

    56

    78

    SS

    FFFF

    FFFE

    FFFD

    FFFC

    FFFB

    FFFA

    0000

    (TOS) SP

    5678H

    5678H

    AX

    BX

  • 8/13/2019 06 Stackler Ve Makrolar1

    16/23

    After POP BX

    16

    12

    34

    56

    78

    SS

    FFFF

    FFFE

    FFFD

    FFFC

    FFFB

    FFFA

    0000

    (TOS) SP

    5678H

    1234H

    AX

    BX

  • 8/13/2019 06 Stackler Ve Makrolar1

    17/23

    Macros

    17

    Macros are just like procedures, but not really

    Macros look like procedures, but they exist only

    until your code is compiled

    After compilation all macros are replaced with real

    instructions

    If you declared a macro and never used it in your

    code, compiler will simply ignore it.

  • 8/13/2019 06 Stackler Ve Makrolar1

    18/23

    Macro Definition

    18

    name MACRO [parameters,...]

    ENDM

  • 8/13/2019 06 Stackler Ve Makrolar1

    19/23

    19

    Unlike procedures, macros should be defined above

    the code that uses it.

    MyMacro MACRO p1, p2, p3

    MOV AX, p1

    MOV BX, p2

    MOV CX, p3ENDM

    ORG 100h

    MyMacro 1, 2, 3

    MyMacro 4, 5, DXRET

    The code on the left is

    expanded into:

    MOV AX, 00001hMOV BX, 00002h

    MOV CX, 00003h

    MOV AX, 00004h

    MOV BX, 00005h

    MOV CX, DX

  • 8/13/2019 06 Stackler Ve Makrolar1

    20/23

    Macros and Procedures

    20

    1. When you want to use a procedure you shoulduse CALL instruction, for example:

    CALL MyProc

    When you want to use a macro, you can just type itsname. For example:

    MyMacro2. You should use stack or any general purpose

    registers to pass parameters to procedure.

    To pass parameters to macro, you can just type themafter the macro name. For example: MyMacro 1, 2, 3

  • 8/13/2019 06 Stackler Ve Makrolar1

    21/23

    Macros and Procedures

    21

    3. Macros end with ENDMdirective andprocedures end with ENDPdirective.

    4. Procedures are located at some specific

    address in memory but macros are expanded

    directly in program's code.

  • 8/13/2019 06 Stackler Ve Makrolar1

    22/23

    Labels in Macros

    22

    Macros are expanded directly in code, therefore ifthere are labels inside the macro definition you

    may get "Duplicate declaration" error when macro

    is used for twice or more.

    To avoid such problem, use LOCALdirectivefollowed by names of variables, labels or

    procedure names.

  • 8/13/2019 06 Stackler Ve Makrolar1

    23/23

    Example

    23

    MyMacro2 MACRO

    LOCAL label1, label2

    CMP AX, 2

    JE label1

    CMP AX, 3

    JE label2

    label1:

    INC AX

    label2:

    ADD AX, 2

    ENDM

    ORG 100h

    MyMacro2

    MyMacro2

    RET