Lecture 12

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Push, Pop and Operation of the Stack

• PUSH source
POP destination

CS is not a valid destination for POP

• Example Instructions

PUSH BX
PUSH [BX]
PUSH DS
PUSH COSTP
POP CX
POP [SI]

• PUSH and POP are relevant only for stack segment


Operation of the Stack
• The base address is in SS register.

• The highest offset address is in SP register, i.e, SP will contain the offset of the highest address
with respect to the base address.

• This is why it is called the TOP of STACK.

• For example, if the stack is defined from addresses 20000H to 203E8H, SS = 2000H and SP =
03E8H, this stack then has a size of 3E8H bytes (1000 bytes).

• In the 8086, only a word (16 bits) can be pushed on to stack. A push thus causes two memory
locations to be accessed.
Operation of the Stack
Defining a Stack
• We have to define elements including the stack size, base address of segment and the
stack top.

• If we are using the tiny model, this is done by DOS – but in other memory models, we
will have to do it ourselves.

• .MODEL SMALL
.STACK 400H

• STACKSEG SEGMENT STACK


DW 200H (?)
STACKSEG ENDS
Stack Example
.MODEL SMALL
.STACK 300H

.CODE
.STARTUP

MOV AX, 4567H


MOV BX, 0ACEH
PUSH AX
PUSH BX
POP AX
POP BX

.EXIT
END
Instructions to enter through keyboard and display on
the screen
Addressing Modes
• The way in which operands are specified in an assembly language
instruction is called its addressing mode.

• We will use the MOV instruction for understanding these modes. This
has the format:
MOV destination, source
Addressing Modes
• The basic assumptions in this context are:
• the operands can be in registers, in memory, or may be in the
instruction itself. However the 8086 does not have an addressing
mode in which both operands are in memory locations
• In the case of two operands, one of them can be in memory, but the
other will have to be placed in a register
• Data types should match – i.e., the source and destination should
both be either bytes or words.
Addressing Modes
• Register Addressing
Here both the source and destination are registers. No memory access is involved.
MOV AL, AH

• Immediate Addressing
In this mode, the source will be a constant data
MOV AL, 45H
MOV PRICE, 40

• Direct Addressing
Here either the source or the destination will be a memory address.
MOV AX, [2345H]
MOV [1089H], AL
MOV AX, PRICE
MOV COST, AL
Addressing Modes

• Register Indirect Addressing


In this mode, the address of the data is held in a register.
We also use the term effective address for the address of the operand. For this mode of
addressing, the address registers allowed are BX, SI and DI.
EA = {[BX]/[DI]/[SI]}
MOV AL, [BX]
MOV [SI], CL

• Register Relative Addressing


In relative addressing mode, a number or displacement is part of the effective address.
EA = {[BX]/[DI]/[SI]/[BP]} + 8-bit or 16-bit displacement
MOV CL, 10[BX]
MOV CL, [BX + 10]
MOV CL, [BX] + 10 or
MOV CL, [BX][10] or
MOV CL, PRICE [BX]
Addressing Modes
• Based Indexed Mode
In this mode, an index register and a base register together carry the effective address. The content
of these two registers are added and called the effective address.
MOV AL, [BX][SI]
MOV [BX][DI], CX

• Relative Based Indexed Mode


This is the case when the ‘effective address’ is specified with a base register, an index register as
well as a displacement. The ‘effective address’ is the sum of the two registers and the displacement.

MOV DL, 5[BX][DI]


MOV 5[BP][SI], AX
MOV CL, COST[BX][SI]

• Note that BP cannot be used without a displacement. See it being used in ‘register relative’ mode
but not in register indirect mode.
Segment Override

• We can use segments other than those specified as default segments.

MOV AL, ES : [BX]


MOV DS : [BP + 7], BL
MOV AX, CS : [BX ]
Near and Far Jump
• The jump location may be in the same code segment, in which case it
is near jump. For a near jump, the value of CS remains the same, but
the value of IP will change. This is also called an intra-segment (within
segment) jump.
• The jump location may be in a different code segment – then it is
called a far jump. In this case, both CS and IP will have to take on new
values. This is an inter-segment (between segments) jump.
Loop Instruction
• LOOP label
The register CX is assigned to decrement every time LOOP executes.
When CX = 0, the looping is exited.
String Instructions

• The 8086 has a set of instructions for handling blocks of data in the form of
bytes or words.

• They are called ‘string’ instructions. A string is an array of data of the same
type – for example, a character string or a byte string.

• String instructions can be seen to be useful when in the memory, data has to
be moved, searched or compared in blocks (arrays).
Pre-requisites for using String Instructions

• Two segments are to be defined i.e., the data segment and the extra
segment. This means that the corresponding segment registers DS and ES
have to be initialized and used. The data segment is the source segment
and the extra segment is the destination segment.

• The DI registers and SI registers should act as pointers to the data


segment and extra segment respectively. This means that, initially, SI
should contain the address (offset) of the first location in the data
segment. Similarly, DI should contain the address (offset) of the first
location in the extra segment.
Pre-requisites for using String Instructions

• There is a control flag called the direction flag which is used


exclusively for string operations. Its purpose is that in string
operations, if the flag is set, the pointer registers get automatically
decremented and if reset, the reverse occurs. So whenever string
instructions are being used, the direction flag (DF) should be set or
reset depending on the direction the addresses are to be modified
after each operation.

• The counter CX should be loaded with the count of the number of


operations required.
The MOVS Instruction
Small Model Program
The CMPS Instruction -- CMPSB/CMPSW

This instruction is for string comparison, byte by byte or word by word


as the case may be.
String Instructions Summary
Procedures
• Procedure is a program or a set of
instructions that does a specific task.

• When this task is to be done repeatedly,


the procedure is used again and again.

• When a ‘main’ program considers this as


a subsidiary task, the procedure (or
subroutine) is ‘called’ whenever its
service is required.

• A procedure also helps in making a


program modular.
Sequence of Actions taken by a Processor when using a
Procedure
Writing a Procedure
Intrasegment or Near Call
• Direct CALL
Usage: CALL label

• The direct call is like a direct jump instruction, and is three bytes long.

• It is relative and the destination can be -32,768 bytes to +32,767 bytes from the address of the
instruction following the call.

• This means that the offset can be a 16-bit signed number.


Intrasegment or Near Call

• Indirect Call

• Usage: CALL reg16; CALL [reg16]

• In this case, the destination is


specified in a 16-bit register or in
a memory location pointed by a
register.
Intersegment or Far Call

• Direct Far Call


• A far call is an intersegment call, which means that the destination
address is in a different code segment.
• This will be a 5-byte instruction, the first byte being the opcode, the
second and third bytes being the new value of IP, and the fourth and
fifth, the new values of CS.
Intersegment or Far Call
• Indirect Far Call
• For an indirect call, the destination address is not in the instruction – rather, it
is in a register or memory.

• For a far call, four bytes are needed to specify a destination. But a register cannot
specify it.

• Therefore, the four bytes needed to specify a destination are stored in memory and
pointed by a register.

• As an example CALL DWORD PTR [SI] can be a far call instruction. [SI] and [SI + 1] gives
the new value of IP and [SI + 2] and [SI + 3] gives the new
value of CS.

You might also like