You are on page 1of 24

CSE302MicroProcessorNotes

UNIT-II SOFTWARE ASPECTS TABLE OF CONTENTS Linking & Relocation Stacks Procedures Macros Interrupt & Interrupt routines Byte and String Manipulation. Introduction: This chapter considers the formulation of complex programs from small sequences called modules. Each module performs a well defined task. Such complex computer code is referred to as modular programming. Assembly language programs are developed by essentially the same procedure as high level language. 1) Define the overall concept of the program. 2) Breaking overall program into number of task. 3) Define the work for each task and communication with other tasks. 4) Putting the task into assembler language modules together to form the program. 5) Debugging & Testing 6) Documentation.

CSE302MicroProcessorNotes

The typical hierarchical diagram which contains task & subtasks.


MainModule

ModuleA

ModuleB

ModuleC

ModuleE

ModuleB

ModuleD

ModuleE

ModuleG ModuleH

ModuleD

The Reasons for breaking a program into small parts are that: Modules are easier to comprehend. Different modules can be assigned to different programmers. Debugging & Testing can be order. Documentation can be easy. Modifications can be localized. When designing the modules of a program one must concerned two circumstances 1) How 2) what circumstances the modules are entered. 2) 1) Control Coupling: How information is communicated between the modules. 3) 2) Data Coupling: Data is assembled together (or) separately. Most assembler language are designed for modularization process in three ways:1) The data to be structured for easily access 2) To provide the procedures

CSE302MicroProcessorNotes

3) To permit the sections of code, known as macros, which contain a set of arguments. Linking and Relocation: Some program modules may be in same source and assembled together; others may be in different source modules and assembled separately. If they are assembled separately, the main module must be terminated by an END statement and other modules must be terminated by an END statement with no operand. In any event the object modules are grouped into libraries, must be linked together to form a load module before execution. In addition to outputting load module the linker prints a memory map that indicates where the linked objects modules will be loaded into memory. After the load module has been created it is loaded into the memory by loader and execution begins. Also I/O modules are available in program; it is driven by I/O drivers. The arrows indicate that corrections may be made after any one of the major stages. The linker/Loader combination must make all of the segment & adders assignments needed to allow the program to work properly.

CSE302MicroProcessorNotes

Operatingsystem ResidentMonitor

Object module
Assembleror other

load module

Linker
. .

Loader

Execution Program

Listing

..

Memory map

Library

More specifically, the loader and linker combination must 1) Find the object modules to be linked. 2) Construct the load module 3) Fill in all aspects 4) Fill in all segment addresses. 5) Load the program for execution.

CSE302MicroProcessorNotes

Object modules are linked by naming them in the command to the linker & by operating system. The format of a linker command depends upon the system. It contains a command followed by a list of modules and libraries. The order in which object modules are determined in the linker may appear in a same order in load module. Segment Combination: In addition to the linker, the assembler provides the way to align the segments by the linker. Sometimes segments with the same name are concatenated and sometimes they are overlaid. The segments with the same name are joined together is decremented by the modifiers attached to the SEGMENT directives. A SEGMENT directive may have the form Segment Name SEGMENT Combine-type

Where the combine type indicates how the segment is to be located within the load module. Segments that have different names cannot be combined. If segments with the same name but no combine type will cause linker error. The possible combine types are: PUBLIC:If segments in different object modules have the same name and combine type PUBLIC, then they are concatenated into a single segment in the load module.

CSE302MicroProcessorNotes

The order of concatenation is specified by linker command. COMMON: If the segments in different object modules have the same name and the combine type is COMMON, then they are overlaid. The length of the common segment is that of the longest segment is being overlaid. STACK: If segments in different object modules have the same name & combine type STACK, then they become one segment whose length is the sum of the lengths of the individually specified segments. AT: AT combine-type is followed by an expression that evaluates to a constant which is to be the segment address. It allows the user to specify the exact location of the segment in memory. MEMORY: This combine-type causes the segment to be placed at the last of the load module. If more than one segment with the MEMORY combine-type is being linked. The first one will be treated as a MEMORY combine-type. The others will be overlaid as a COMMON combine-type. The example fig. shows how the public & common combine-types affect the construction of a load module.

CSE302MicroProcessorNotes

Two (or) more code segments to be put in a single segment with the use of PUBLIC. It allows intersegment branches to be replaced by intra segment branches. Example for STACK combine type Source Module 1 DATA SEGMENT COMMON . . DATA ENDS CODE SEGMETN PUBLIC . . CODE ENDS

SOURCE MODULE 2
DataforSource Module1 Code in Source Module 1 Code in Source Module 2 DataforSource Module2

DATA SEGMENT COMMON . DATA ENDS CODE SEGMETN PUBLIC . CODE ENDS

Code Segment

Fig: Load Module

CSE302MicroProcessorNotes

Example for STACK combine type SOURCE MODULE 1 STACK_SEG SEGMENT STACK DW 20 DUP(?) Topof STACK LABEL WORD Stack_SEG ENDS . . END SOURCE MODULE 2
STACK SEG

STACK_SEG SEGMENT STACK 50 Words DW 30 DUP (?) STACK_SEG ENDS . . END STACK provides a means of obtained one stack shared by several modules. The AT combine-type permits the user to specify the exact beginning location of a segment of code (or) data. The variables and labels are defined in segments using AT combine type is called absolute variables and labels. If it does not having AT combine_type it is called relocatable variables and labels.
TOP OF STACK

CSE302MicroProcessorNotes

4.1.2 Access to External Identifiers:Object modules are linked together & refer to each other. That is, there must be a way to refer the variables (or) labels in other modules. If an identifier is defined in an object module it is called local identifier. But it is not defined in the module, but is defined in one of the other modules being linked, that is called external (global) identifier. For single object module all identifiers are referenced must be locally. But in multiple module programs, the assembler must be informed in advance of any externally defined identifiers that appear in a module so that it will not treat them as being undefined. Each module contain two lists, 1) External identifiers 2) Local identifiers. It must be implemented by EXTRN & PUBLIC directives. EXTRN And PUBLIC Identifier,, Identifier Identifier: Type,,identifier:Type

For a variable the type may be BYTE, BYTE, WORD, (or) DWORD & for a label the type may be NEAR (or) FAR. EX: INC VAR1

If VAR1 is external and is associated with a word, then the module containing the statement must also contain a directive such as EXTRN ,VAR1: WORD,..

CSE302MicroProcessorNotes

And the module in which VAR1 is defined must contain a statement PUBLIC ,VAR1, o The linker will verify every identifier appearing in an EXTERN statement is matched by one in a PUBLIC statement. o If this is not the case the linker error will occur. o The figure shows three modules show the matching is done by the linker while joining them together. Source Module1 EXTRN VAR2:WORD, LAB2:FAR PUBLIC VAR1, LAB1 DATA1 SEGMENT VAR1 DB 2 VAR3 DW ? VAR4 DW ? DATA1 ENDS . . Source Module2 EXTRN VAR1: BYTE, VAR4: WORD PUBLIC VAR2 DATA2 SEGMENT VAR2 DW VAR3 DB DATA2 ENDS 0 5 DUP (?)
A Linker error will occur because there is no matching PUBLIC declaration
10

CSE302MicroProcessorNotes

Source Module3 EXTRN LAB1:FAR PUBLIC LAB2, LAB3 :


Does not need a match

1) In module1 VAR1 & LAB1 are local & declared PUBLIC, but VAR3 & VAR4 are local but not declared, so it is ignored by the linker. 2) In module 1, VAR2, LAB2 are not locally defined but are available in module1, because they have been declared PUBLIC in any other modules. 3) Module 2 contains VAR2 & Module3 contains LAB2. 4) Module 2 references VAR1 & VAR4 which are defined in module1 but VAR4 is not defined in a PUBLIC, so linker error will create. 5) LAB3 in module 3, is made PUBLIC but it is not used by any other module. So does not require a match.

4.2.STACKS
A program needs to temporarily store information & retrieve it in reverse order. One example of such a situation is saving and restoring the counters in reverse order. When a loop instruction is designed, it uses CX register.

Loop n counter Order of Retrieving


:

Order of Storing

Loop 2 counter Loop 1 counter

Loop 1 counter
11

CSE302MicroProcessorNotes

Because the loop instructions are designed to use only the CX register a problem arises when loops are nested. For efficient saving and retrieving the loop counters in LIFO basis, Stack can be used. Stack is designed LIFO basis. Most important use of stacks is related to procedures. Stack pointer is automatically decremented in PUSH operation & incremented in POP operation. The address of the last element pushed onto the stack is known as the top of the stack (TOS). Only words can be pushed (or) popped and the data cannot be immediate. But the Push & pop instructions can use all of the other addressing modes. POPF instruction pops the top of the stack into the PSW, affects the flag. When a word is pushed onto the stack by decrementing SP by 2, so that it points next lower word in memory. A POP consists of loading the top of the stack into the destination incrementing SP by 2. On the 8086, the physical address is obtained from both SP and SS (or) BP and SS. For all PUSH & POP operations SS being the stack segment register. The SS are the lowest address of the stack, which is referred to as the base of the stack. The stack occupies the memory locations from 16 times SS to 16 times SS plus the original SP. The stack must be initially set by the software. This is done by loading SP & SS.
12

CSE302MicroProcessorNotes

Stack_seg segment DW 3DUP(?) TOS Label word Stack_seg ends. Code_seg segment Assume CS: Code_seg, SS: Stack_seg Start: MOV AX, Stack_seg MOV SS, AX MOV SP, offset TOS : Code_seg ends.

13

CSE302MicroProcessorNotes

FOR EXAMPLE MOV AX, [BP][SI] Load Ax from stack segment MOV AX, [BP][SI] Load AX from Data segment EX: CX, & DX could be stored in memory beginning at SAVE. MOV SAVE, CX MOV SAVE+2, DX If the offset of SAVE is in SI. MOV [SI], CX INC SI INC SI MOV [SI], DX PUSH CX PUSH DX POP DX POP CX

4.3. PROCEDURES
A procedure is a set of code that can be branched to and returned from the point from which it is branched to. The branch to a procedure is referred to as the call. Corresponding branch back is known return.
14

CSE302MicroProcessorNotes

FIG: MULTIPLE CALLS Calling program


: CALL PROC_A : CALL PROC_A : RET

Procedure
PROC_A : :

Several calls are made to the same procedure. One copy of procedure needs to be stored in memory even though the procedure may be called several times. Main Program
: :

Procedure A
PROC_A : CALL PROC_B : : RET

Procedure B
PROC_B : : RET

CALL PROC_A :

When procedures are nested each return is made to the corresponding calling procedure. Procedures can also be stored in libraries & used by variety of programs. Procedures have one major disadvantage is that extra code is needed to join them together. The extra code is called linkage.

15

CSE302MicroProcessorNotes

The following three requirements must be satisfied when calling a procedure 1) Unlike other branch instructions, a procedure must save the address of the next instruction so that the return will be able to branch back to the proper place in the calling program. 2) The registers used by the procedure need to be stored their contents are changed, and then restored before the procedure is exited. 3) A procedure must have the communication with the calling program & other procedures. 4.3.1. Calls, Returns, and procedure definitions The CALL instruction not only branches to the indicated address, but also pushes the return address onto the stack. The RET instruction simply POPS the return address from the stack. The addressing modes for the CALL instruction are the same as the JMP instruction except short CALL. A CALL may be direct (or) indirect and intra segment (or) inter segment. A FAR PTR & NEAR PTR is added before the operand to indicate an inter segment (or) intra segment call. Intra segment call pushes only IP, the offset of the return address on to the stack. But an inter segment call must push both IP and CS on to the stack. The following figure shows the stack action:Main program calls FAR procedure PRO_A. PRO_A calls NEAR procedure PRO_B. PRO_B calls NEAR procedure PRO_C.

16

CSE302MicroProcessorNotes

Return is made to PRO_B. Return is made to PRO_A. PRO_A calls NEAR procedure PRO_C. Return is made to PRO_A. Return is made to main program.

17

CSE302MicroProcessorNotes

Format: Procedure-name Procedure-name PROC ENDP Attribute End statement

A Procedure may be in:1) Same code segment as the statement that calls it. 2) different segment and source module from the calling statement. The attribute may be NEAR & FAR. EXAMPLE:
SEGX SUBT SEGMENT : PROC FAR : RET ENDP : FAR PTR SUBT : ENDS
18

SUBT CALL SEGX

CSE302MicroProcessorNotes

SEGY SEGMENT : CALL FAR PTR SUBT : SEGY ENDS

4.3.2. Saving and restoring registers Both the calling program & procedure share the same set of registers. It is necessary to save the registers when entering a procedure & restore them before returning to the calling program. The stack is used for storing these register contents. Storing & restoring the registers
SUBT PROC NEAR PUSH AX PUSH BX PUSH CX PUSH DX : : POP DX POP CX POP BX POP AX RET SUBT ENDP
19

CSE302MicroProcessorNotes

After Storing
SP 0200

After Restoring:
SP 0200

TOS

DX CX BX

DX CX BX AX

AX Return Address

TOS

Return Address

Storing could be done before the procedure is called and the restoring just after the return. 4.3.3. Procedure Communication:Two types of procedures are there Same set of data Different set of data The procedure can refer to the variables directly as indicated in the same source module. Otherwise it can be referred by PUBLIC & EXTRN directive. 4.3.4. Recursive procedures A single application consists of multiplication and addition and the overall result could be obtained by nesting the multiply-add algorithm. Such algorithms are said to be recursive. Recursive means a procedure call itself.
20

CSE302MicroProcessorNotes

But each call must not destroy the parameters & the previous result. This means each call it will store the set of parameters, registers in different place in memory.
Temporary results Frame for 2nd call Save area for register parameters Temporary results Frame for 1st call Save area for register

parameters

4.4. Interrupts and Interrupt Routines Whenever certain conditions exist, computer automatically executes one of a collection of special routines. These special routines called interrupt. That interrupt execution is called interrupt routine. There are two classes of interrupt routines o Internal interrupts o External interrupts. Internal interrupts are initiated by the CPU (or) by an instruction.

21

CSE302MicroProcessorNotes

External Interrupts are caused by a signal sent to the CPU from elsewhere in the computer. Internal interrupts are caused by a division by zero (or) special. External interrupts are caused by the I/O device.

Difference between Procedure & Interrupt Routine:[Interrupt routine is similar to a procedure, in that it may be branched from any other program & return is made after interrupt routine has executed. The PSW & register are saved before interrupt has executed. But in procedure we will not save PSW. Interrupt is unlike a procedure in that the programs may be put in a fixed place in a memory]. Some kinds of interrupts are controlled by IF & TF flags. These flags must be set properly (or) the interrupt action is blocked. If the conditions for an interrupt are met it will push the current contents of the PSW, CS & IP onto the stack & inputting the new contents of IP & CS. After finishes the interrupt routine pops the old IP, CS & PSW from the stack. The double word containing the new contents of IP & CS is called interrupt pointer (or) vector. Each interrupt type is given a number between 0 and 255. The address of the interrupt vector is found by multiplying the type by 4. The interrupt pointer may occupy first 1024 bytes of memory & it will not be used by other purposes. Some of the 256 interrupt types may be reserved by the OS & may be initialized when the computer system is first brought up.
22

CSE302MicroProcessorNotes

Some other interrupt types may be used according to the applications. New (IP) for Type 0
Pointer for Type 0 00000 Reserved for device error 00004 Reserved for Single step trap- TF must be set 00008 Pointer for Type 2 Reserved for Non maskable Interrupt 0000C Pointer for Type 3 Reserved for 1 byte Interrupt instruction 00010 Pointer for Type 4 Reserved for Overflow INTO 00014 Pointer for Type 5

New (CS) for Type 0 New (IP) for Type 1


Pointer for Type 1

New (CS) for Type 1

00018 Pointer for Type 6 Reserved for 2 byte INT & maskable external interrupts

New (IP) for Type 255


Pointer for Type 255

003FC

New (CS) for Type 255


00400

Only first 5 types have explicit definitions Other types may be used by external interrupts.

23

CSE302MicroProcessorNotes

The types are reserved for division by zero error. If this error is attempted, the computer will push the current contents of the PSW, CS & IP onto the stack & fill the IP & CS from addresses 00000 & 00002 & continue the new execution. Type1 is the single-step trap. This interrupt is enabled by TF flag. This interrupt is used for debugging each & every instructions. Type2 interrupt is the external interrupt is set by the IF flag. All other interrupts are external interrupt set by the IF flag is 1. IRET is used for return from interrupt routine. If the type does not appear, then the instruction is 1 byte long, otherwise 2 bytes long. Routine for printing info set 1

00014 INT5 INT6 INT7s 00018 0001C routine for printing info set 3 routine for pronting info set 2

INTO is a type 4 interrupt. It is set by OF flag is set to 1. Program : ADD AX, BX INTO :
24

INT_ROUT: If OF flag is set 1 : :

You might also like