Sunteți pe pagina 1din 32

Dr.S.

Sivaprakash Prof,IT/CMREC
Dr.S.Sivaprakash Prof,IT/CMREC

At the conclusion of this chapter you will be able to:


•Use 8086 string manipulation instructions to perform a variety of operations on a sequence of data words in the memory.

•Describe how a stack is initialize and used in 8086 assembly language program which call procedures.

•Write an 8086 assembly language program which calls a near procedure.

•Write an 8086 assembly language program which calls a far procedure.

•Write, assemble, link and run a program which consists of more than one module.

•Write and use an assembler macro.


Dr.S.Sivaprakash Prof,IT/CMREC

• Writing and using procedures


• The CALL and RET instructions

• The 8086 Stack

• Using PUSH and POP

• Passing parameters to and from procedures

• Writing and debugging program containing procedures

• Reentrant and Recursive procedures

• Writing and Calling Far procedures

• Accessing a procedure

• Writing and using Assembler Macros


• Comparison Macros and Procedures

• Defining and calling a Macro without parameters

• Passing parameters to Macros


Dr.S.Sivaprakash Prof,IT/CMREC

Procedure :
A procedure is group of instructions that usually performs one task. It is a reusable section of a software program
which is stored in memory once but can be used as often as necessary.
A procedure can be of two types.
1) Near Procedure
2) Far Procedure
Near Procedure: A procedure is known as NEAR procedure if is written(defined) in the same code segment which is
calling that procedure.
Only Instruction Pointer(IP register) contents will be changed in NEAR procedure.
FAR procedure : A procedure is known as FAR procedure if it is written (defined) in the different code segment than the
calling segment.
In this case both Instruction Pointer(IP) and the Code Segment(CS) register content will be changed.
Dr.S.Sivaprakash Prof,IT/CMREC

Directives used for procedure :


PROC directive:
•The PROC directive is used to identify the start of a procedure.
•The PROC directive follows a name given to the procedure.
•After that the term FAR and NEAR is used to specify the type of the procedure.

ENDP Directive:
•This directive is used along with the name of the procedure to indicate the end of a procedure to the assembler.
•The PROC and ENDP directive are used to bracket a procedure.
Dr.S.Sivaprakash Prof,IT/CMREC
CALL instruction and RET instruction :
CALL instruction :
The CALL instruction is used to transfer execution to a procedure.
It performs two operation.
When it executes, first it stores the address of instruction after the CALL instruction on the stack.
Second it changes the content of IP register in case of Near call and changes the content of IP register and CS register in case
of FAR call.
There are two types of calls.
1)Near Call or Intra segment call.
2) Far call or Inter Segment call
Operation for Near Call : When 8086 executes a near CALL instruction, it decrements the stack pointer by 2 and copies
the IP register contents on to the stack.
Then it copies address of first instruction of called procedure.
SP SP-2
IP stores onto stack
IP starting address of a procedure.
Dr.S.Sivaprakash Prof,IT/CMREC

Difference between FAR CALL and NEAR CALL

Near Call Far Call


A near call refers a procedure A Far call refers a procedure
which is in the same code which is in different code
segment. segment
It is also called Intra-segment It is also called Inter-segment
call call
A Near Call replaces the old IP A FAR replaces CS & IP with
with new IP new CS & IP.
It uses keyword near for calling It uses keyword far for calling
procedure. procedure
Less stack locations are More stack locations are
required required.
Dr.S.Sivaprakash Prof,IT/CMREC

Re-entrant procedure :
In some situation, it may happen that procedure 1 is called from main program and procedure 2 is called from procedure
1.And again procedure 1 is called from procedure 2.
In this situation , program execution flow re-enters in the procedure 1 ( first time when procedure 1 was called from main
program and second time when procedure 1 was called from procedure 2) .
Hence this type of procedure is called as Reentrant procedure.
Dr.S.Sivaprakash Prof,IT/CMREC

• Avoid writing the same sequence of instruction again and again.

• Write it in a separate subprogram and call that subprogram whenever necessary.

• For that CALL instruction is used.


Dr.S.Sivaprakash Prof,IT/CMREC

The CALL Instruction:


•Stores the address of the next instruction to be executed after the CALL instruction to stack. This address is called as
the return address.
•Then it changes the content of the instruction pointer register and in some cases the content of the code segment
register to contain the starting address of the procedure.
Dr.S.Sivaprakash Prof,IT/CMREC

Chart for CALL and


RET instruction 
Dr.S.Sivaprakash Prof,IT/CMREC

Types of CALL instructions:


•DIRECT WITHIN-SEGMENT NEAR CALL:

produce the starting address of the procedure by adding a 16-bit signed displacement to the contents of the instruction pointer.
•INDIRECT WITHIN-SEGMENT NEAR CALL:

the instruction pointer is replaced with the 16-bit value stored in the register or memory location.
•THE DIRECT INTERSEGMENT FAR CALL:

Used when the called procedure is in different segment. The new value of the instruction pointer is written as bytes 2 and 3 of
the instruction code. The low byte of the new IP value is written before the high byte.
•THE INDIRECT INTERSEGMENT FAR CALL:

Replaces the instruction pointer and the contents of the segment register with the two 16-bit values from the memory.
Dr.S.Sivaprakash Prof,IT/CMREC

The 8086 RET instruction:


•When 8086 does near call it saves the instruction pointer value after the CALL instruction on to the stack.
•RET at the end of the procedure copies this value from stack back to the instruction pointer (IP).
Dr.S.Sivaprakash Prof,IT/CMREC

 Section of memory you set aside for storing


return addresses.
 Also used to store the contents of the
registers for the calling program while a
procedure executes.
 Hold data or address that will be acted upon
by procedures.
Dr.S.Sivaprakash Prof,IT/CMREC

 The PUSH register/memory instruction decrements the stack pointer by 2 and copies he contents of the
specified 16-bit register or memory location to memory at the new top-of-stack location.
 The POP register/memory instruction copies the word on the top-of-stack to the specified 16-bit register or
memory location and increments the stack pointer by 2.
Dr.S.Sivaprakash Prof,IT/CMREC

Major ways of passing parameters to and from a procedure:


•In register

•In dedicated memory locations accessed by name

•With pointers passed in registers

•With the stack


Dr.S.Sivaprakash Prof,IT/CMREC

Passing parameters in registers :


The main program can pass upto 6 parameters to the procedure through the registers AX,BX,CX,DX,SI & DI before
executing the call instruction. e.g. consider the program to calculate a square of given number.
Dr.S.Sivaprakash Prof,IT/CMREC

Passing parameters in dedicated memory locations accessed by name :


when large number of parameters is to be passed to the procedure, then these parameters can be placed in an argument list
as dedicated memory locations in one of the data segment from memory. e.g. consider the program to calculate a square of
given number.
Dr.S.Sivaprakash Prof,IT/CMREC

Passing parameters with Pointer :


In the main program, before we call the procedure we can set up SI as a pointer to pass values to procedures and set DI as
pointer to receive values from procedures. e.g. consider the program to calculate a square of given number.
LEA − Used to load the address of operand into the provided register
Dr.S.Sivaprakash Prof,IT/CMREC

Passing parameters with stack :


Alternate method of passing large number of parameters is to push the parameters on the stack in the main program before
we call the procedure. e.g. consider the program to calculate a square of given number.
Dr.S.Sivaprakash Prof,IT/CMREC

Advantages and Disadvantages of using procedure :


Advantages :
1)Allows to save memory space.
2)Program development becomes easier.
3)Debugging of errors in program become easy.
4)Reduced size of program
5)Reusability of procedure.
Disadvantages :
1)CALL and RET instructions are always required to integrate with procedures.
2) Requires the extra time to link procedure and return from it.
3) For small group of instructions, linking and returning back time more than the execution time, hence for small group of
instructions procedures cannot be preffered.
Dr.S.Sivaprakash Prof,IT/CMREC

 Carefully workout the overall structure of the program and break it down into modules which can easily be
written as procedures.
 Simulate each procedure with few instructions which simply pass test values to the mainline program. This
is called as dummy or stubs.
 Check that number of PUSH and POP operations are same.
 Use breakpoints before CALL, RET and start of the program or any key points in the program.
Dr.S.Sivaprakash Prof,IT/CMREC

 Reentrant procedures: The procedure which can be interrupted,


used and “reentered” without losing or writing over anything.
 Recursive procedure: It is the procedure which call itself.
Dr.S.Sivaprakash Prof,IT/CMREC

 It is the procedure that is located in a segment which has different


name from the segment containing the CALL instruction.
Dr.S.Sivaprakash Prof,IT/CMREC

Accessing a procedure in another segment


•Put mainline program in one segment and all the procedures in different segment.
•Using FAR calls the procedures can accessed as discuss above.
Accessing procedure and data in separate assembly module
•Divide the program in the series of module.
•The object code files of each module can be linked together.
•In the module where variables or procedures are declared, you must use PUBLIC
directive to let the linker know that it can be accessed from other modules.
•In a module which calls procedure or accesses a variable in another module, you must
use the EXTERN directive.
Dr.S.Sivaprakash Prof,IT/CMREC
Dr.S.Sivaprakash Prof,IT/CMREC

Macro:
A MACRO can be defined anywhere in a program using the directives MACRO and ENDM. The label prior
to MACRO is the macro name which should be used in the actual program. The ENDM directive marks the end of the
instructions or statements sequence assigned with the macro name.

A MACRO is group of small instructions that usually performs one task. It is a reusable section of a software
program.A macro can be defined anywhere in a program using directive MACRO &ENDM.
Dr.S.Sivaprakash Prof,IT/CMREC

• A big advantage of using procedures is that the machine codes for the
group of instruction in the procedures needs to be loaded in to main
memory only once.
• Disadvantage using the procedures is the need for the stack.
• A macro is the group of instruction we bracket and give a name to at the
start of the program.
• Using macro avoids the overhead time involved in calling and returning
from a procedures.
• Disadvantage is that this will make the program take up more memory
than using a procedure.
Dr.S.Sivaprakash Prof,IT/CMREC
Dr.S.Sivaprakash Prof,IT/CMREC

 The words NUMBER,


SOURCE and
DESTINATION are
called as the dummy
variables. When we call
the macro, values from
the calling statements
will be put in the
instruction in place of
the dummies.
Dr.S.Sivaprakash Prof,IT/CMREC

Timings and Delays


•™ That every instruction requires a definite number of clock cycles for its execution.
•Thus every instruction requires a fixed amount of time, i.e. multiplication of the number of clock cycles required
for the execution of the instruction period of the clock at which the microprocessor is running.
•The duration required for the execution of an instruction can be used to derive the required delays. ™
•A sequence of instructions, if executed by a microprocessor, will require a time duration that is the sum of all the
individual time durations required for execution of each instruction.
Dr.S.Sivaprakash Prof,IT/CMREC

The procedure of generating delays using a microprocessor based system can be stepwise described as follows:
1.Determine the exact required delay.
2. a) Select the instructions for delay loop. While selecting the instructions, care should be taken that the execution location
or register used by the main program must note be modified by the delay routine.
b) The instructions executed for the delay loop are dummy instructions in the sense that the result of those
instructions is useless but the time required for their execution is an elemental part of the required delay.
3. Find out the number of clock states required for execution of each of the selected delay loop instructions. Further find out
the number of clock states required (n) to execute the loop once by adding all the clock states required to execute the
instructions individually.
4. Find out the period of the clock frequency at which microprocessor is running i.e. duration of a clock state (T).
5. Find out the time required for the execution of the loop once by multiplying the period T with the number of clock states
required (n) to execute the delay loop once.
6. Find out the count (N) by dividing the required time delay Td by the duration for execution of the loop once (n*T).

Count N = Required Delay (Td) n*T

S-ar putea să vă placă și