You are on page 1of 43

CSPC 36

Microprocessors and
Microcontrollers

1st Unit
THE 8086 Microprocessor:
• Introduction to 8086
• Microprocessor architecture
• Assembly language programming – Memory Segmentation & Addressing modes
• Assembly language programming – Instruction set & Assembler directives
• Modular Programming - Linking and Relocation - Stacks - Procedures
• Macros
• Interrupts and interrupt service routines
• Byte and String Manipulation.

2
Assembly language : low-level programming language for a computer or other programmable
device specific to a particular computer architecture

• Assembly language is converted into executable machine code by a utility program referred to as
an assembler.

• (.obj)

Modular programming :

• software design technique

• that emphasizes separating the functionality of a program into independent modules,

• each module contains everything necessary to execute only one aspect of the desired functionality.

3
Modular Programming
Characteristics of module:
• Each module is independent of other modules.

• Programming a single function per module with one input and one output is a goal

Modularization process is achieved in three ways:


• allow data to be structured so that they can be accessed by the various modules,

• another is to provide for procedures (or subroutines),

• and the third is to permit macros, to be inserted by the appearance of single statements

Library of Object Modules :


• Frequently used procedures are placed in a Library file.
• Only the required object modules are pulled from the Library.
4
When designing the modules of a program one must be concerned with,
• Control coupling : how and under what circumstances the modules are entered and exited

• Data coupling: how information is communicated between the modules.

Coupling between two modules depends on :


• assembled together or separately and the organization of the data

Advantages of Modular Programming:


• It is easy to write, test and debug a module.

• Code can be reused.

DRAWBACKS: Modular programming requires extra time and memory

5
Linking and Relocation
Assembling: Program modules may be put in
• The same source module and assembled together

• In different source modules and assembled separately.

Linker :
• Object modules  a load module

• Prints a memory map : where the linked object modules will be loaded into memory.

Loader : Load module is loaded into the memory of the computer

• The linker/loader combination must make all of the segment and address assignments needed to
allow the program to work properly.
6
Function of linker/loader:
1. Find the object modules to be linked.
2. Construct the load module by
assigning the positions of all of the
segments in all of the object modules
being linked.
3. Fill in all offsets that could not be
determined by the assembler.
4. Fill in all segment addresses.
5. Load the program for execution.

Figure 1. Creation and execution of a program. 7


Segment Combination
• The way segments in different object modules are organized by the linker.

• A SEGMENT directive may have the form : Segment name SEGMENT Combine-type

combine-type indicates how the segment is to be located within the load module.

• The possible combine-types are: Segments with same name in different object modules
• PUBLIC—Concatenate into a single segment in the load module.

• COMMON— they are overlaid so that they have the same beginning address.

• STACK—modules are combined to form one large stack, length is the sum of the lengths of
the individually specified segments.
• AT—It 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
8
Figure 2 : Segment combinations resulting from the PUBLIC and COMMON combine types.

Figure 3 Formation of a
stack from two segments.

9
Access to External Identifiers
• Object modules that are being linked together must be able to refer to each other.

• Local identifier : If an identifier is defined in an object module

• External/global identifier : if it is not defined in the module but is defined in one of the other
modules being linked.

• Given module must include two list of the identifiers to permit other object modules to reference
some of the identifiers.
• List 1 : the external identifiers it references

• EXTRN Identifier:Type, . . . , Identifier:Type

• List 2 : the locally defined identifiers that can be referred to by other modules.

• PUBLIC Identifier, . . . , Identifier


10
• For a variable : the type BYTE, WORD, or DWORD and for a label it may be NEAR or FAR.
• INC VAR1
• the module in which VAR1 is defined must contain Ex: PUBLIC . . . ,VAR1, . . .
• the module containing the statement EX: EXTRN . . .,VAR1:WORD,. .

11
• Assembler inserts the offset address for the local identifiers

• Linking process inserts the offset address for the external identifiers and all segment addresses.

• Relocation: It is the assignment of the segment addresses.

• Informing the assembler which variables are external is necessary, this alone is not sufficient.

• If VAR is an external variable, the segment address of VAR must be put in the appropriate segment
register.

MOV AX,SEG VAR ; PUT THE SEGMENT ADDR OF VAR IN ES

MOV ES,AX ;

ADD DX,ES:VAR

• ASSUME ES: SEG VAR ADD DX,ES:VAR


12
Stacks
• CX register and the loop instructions : counting, testing, and branching needed in a loop,
but because the loop instructions are designed to use only the CX register a problem arises
when loops are nested.

• Solution: designing last-in/first-out (LIFO) stack (or push down stack), itself is simply a
part of memory.

• Stack uses indirect addressing through a special register (SP), that is automatically
decremented as items are put on the stack (PUSH) and incremented as they are retrieved
(POP).

• Address of the last element pushed onto the stack is known as the top of the stack (TOS).

• Only the POPF instruction, which pops the top of the stack into the PSW, affects the flags.
13
Figure 4 Storing and retrieving counters for nested loops.

• 8086 : Push down stack


• physical stack address is obtained from
both (SP) and (SS) or (BP) and (SS).

Figure 5 Stack instructions.

14
Using the stack for nesting loops.

8086 stack 15
Procedures
• A procedure (or subroutine) is a set of code that can be branched to and returned from in such a
way that the code is as if it were inserted at the point from which it is branched to.

• CALL : The branch to a procedure RETURN : The corresponding branch back

• The return is always made to the instruction immediately following the call regardless of where
the call is located.

• They can also be stored in libraries and used by a variety of programs.

• Disadvantage : Extra code is needed to join them together in such a way that they can
communicate with each other.

• This extra code is referred to as linkage.

16
Requirements for calling a procedure:
1. Procedure call must save the address of the next instruction.

2. Since both the calling program and procedure share the same set of registers, it is
necessary to save the registers when entering a procedure, and to restore them before
returning to the calling program.

3. A procedure must have a means of communicating or sharing data with the routine that
calls it and other procedures.

17
• Several calls are made to the same procedure
• return after each call is made to the
instruction following that call.
• only one copy of the procedure needs to be
stored in memory

• each return is made to the corresponding


calling procedure,
• not to a procedure higher in the hierarchy.

Figure 4 Use of procedures 18


• A CALL may be direct or indirect and intrasegment or intersegment.

• If a call statement is a forward reference,

• NEAR PTR may be added before the operand to indicate an intrasegment direct call

• FAR PTR may be added before the operand to indicate an intersegment direct call

• If the CALL is intrasegment (intersegment), then the return must be intrasegment


(intersegment).

• Intrasegment call : pushes only (IP), the offset of the return address, onto the stack

• Intersegment call : must push both (IP) and (CS) onto the stack

• The return must correspondingly pop one or two words from the stack.

• The RET instruction may optionally include a 16-bit immediate operand. If it does, then this
operand is added to (SP) after the return address has been popped. 19
• 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.

Intrasegment CALL DST (SP) <----- ( SP ) -2 ((SP) + 1 : ( SP ) ) <----- (IP)


direct call (IP) <----- ( IP ) +D 16 *
Intra segment CALL DST (SP) <----- ( S P ) -2 ((SP) + 1 : (SP)) <----- (IP)
indirect call (IP) <----- (EA)
Inter segment CALL DST (SP) <----- ( S P ) -2 ((SP) + 1 : (SP)) <----- (CS)
direct call (SP) <----- ( SP ) -2 ((SP) + 1 : (SP)) <----- (IP)
(IP) <----- D 16
(CS) <----- Segment address (Last word of instruction)
Inter segment CALL DST (SP) <----- ( SP ) -2 ((SP) + 1 : ( SP) ) <----- (CS)
indirect call (SP) <----- ( SP )-2 ((SP) + 1 : (SP)) <----- (IP)
(IP) <----- (EA) (CS) <----- (EA+2)

20
Intrasegment return RET (IP) <----- ( (SP) + 1 : (SP)) (SP) <----- ( SP ) + 2
Intrasegment return RET EXP** Same as above except, also (SP) <----- ( S P ) +D16*
with immediate
data
Intersegment RET (IP) <----- ( (SP) + 1 : (SP)) (SP) <----- ( SP ) + 2
return (CS) <----- ( (SP) + 1 : (SP)) (SP) <----- ( S P ) + 2
Intersegment RET EXP** Same as above except, also
return with (SP) <----- ( SP ) + D 16*
immediate data

*Displacement between the destination and the instruction following the CALL instruction.
**EXP is an expression that evaluates to a constant and becomes the D16 portion of the instruction.
21
• Procedures are delimited within the source code by placing a statement of the form at the
beginning of the procedure ,

Procedure name PROC Attribute

• Terminate the procedure with a statement

Procedure name ENDP

• procedure name = identifier used for calling the procedure attribute = NEAR or FAR.

• The attribute is needed to determine the type of RET statement that is to be assembled.

• NEAR = the instruction will only pop a word into the IP register,

• FAR = it will also pop a word into the CS register.

22
assumed that the only stack action is due to the calls and returns and
1. Main program calls FAR procedure PRO_A. the return addresses are
2. PRO_A calls NEAR procedure PRO_B. Main program return address for PRO_A call (CS) = 0500 (IP) = 1000
PRO_A return address for PRO_B call (IP) = 2500
PRO_B return address for PRO_C call (IP) = 4000
Note that when the stack is popped the PRO_A return address for PRO_C call (IP) = 3700
information is not destroyed but it is erased by
subsequent pushes, as is the case when PRO_A
calls PRO_C.

23
3. PRO_B calls NEAR procedure PRO_C. Main program return address for PRO_A call (CS) = 0500 (IP) = 1000
4. Return is made to PRO_B. PRO_A return address for PRO_B call (IP) = 2500
5. Return is made to PRO_A. PRO_B return address for PRO_C call (IP) = 4000
PRO_A return address for PRO_C call (IP) = 3700

24
Main program return address for PRO_A call (CS) = 0500 (IP) = 1000
6. PRO_A calls NEAR procedure PRO_C.
PRO_A return address for PRO_B call (IP) = 2500
7. Return is made to PRO_A.
PRO_B return address for PRO_C call (IP) = 4000
8. Return is made to main program.
PRO_A return address for PRO_C call (IP) = 3700

25
A procedure may be in:

1. The same code segment as the statement that calls it. (NEAR )

2. A code segment that is different from the one containing the statement that calls it, but in the
same source module as the calling statement. (FAR)

3. A different source module and segment from the calling statement. (FAR)

• FAR attribute, all calls to it must be intersegment calls even if the call is from the same code
segment.

• Otherwise, the return would pop two words from the stack even though only one word was
pushed onto the stack by the call.

• In addition, for the third case, the procedure name must be declared in EXTRN and PUBLIC
statements.
26
• Procedure SUBT is defined in code segment
SEGX

• Called from both code segments SEGX and


SEGY.

• Because SUBT is called from both SEGX and


SEGY it must be given the FAR attribute.

• Since SUBT has been given the FAR attribute,


the calls from within SEGX must push both (IP)
and (CS) onto the stack as well as the call from
SEGY.

Figure : Procedure declaration.


27
Saving and Restoring Registers
• Since both the calling program and procedure share the
same set of registers, it is necessary to save the registers
when entering a procedure, and to restore them before
returning to the calling program.

• Various means could be used for saving and retrieving the


contents of the registers that are changed by a procedure,
but if there are stack facilities the stack is almost always
used for this purpose.
Figure 5(a) gives the code needed to store and
restore the contents of the AX, BX, CX,
and DX registers

28
• The storing could be done just before the procedure is called and the restoring just after the return, but
this is not the method normally employed.
• By including these tasks in the procedure the code required to perform them needs to be written only
once.
• If this code were part of the calling program, it would have to be repeated for each call.
• Not all registers need to be saved, only those that are altered by the procedure.

Fig. 4-18(b) shows how the stack


and stack pointer are affected.
Note that the register contents
are popped in reverse order.

29
• However, unless the procedure is short and not likely to be changed, it is normally easier and
less error prone to automatically save at least all of the registers in the data group, and
possibly the pointer, index, and segment registers.

• If, say, the DX register is not saved because it is not accessed, but later the procedure is
modified so that DX is used, then PUSH DX and POP DX would have to be properly
inserted in the procedure.

• Otherwise, upon the return to the calling program (DX) may not be correct.

• An obvious exception would be the case in which DX is intentionally used to return a result.

• In this case, one would not want (DX) to be changed by a POP.

30
• There are two general types of procedures, those that always operate on the same set of data
and those that may process a different set of data each time they are called.

• For example, a procedure may be written so that it always adds the first COUNT elements in an
array ARY and returns the result in a variable SUM, or it may be written so that it can add the
elements in an arbitrary array and return the results to an arbitrary variable.

• When the procedure is in a separate source module it can still refer to the variables directly,
provided that the calling program contains the directive

PUBLIC ARY, COUNT,SUM

• The procedure source module contains the directive

EXTRN ARY:WORD, COUNT:WORD, SUM:WORD


31
Alternatively, ARY, COUNT, and SUM could be If the calling program uses different variable
put in a common area by placing the code at the names, say NUM for ARY, N for COUNT, and
beginning of both source modules. This would TOTAL for SUM, then the DATA segment in its
cause both source modules to share the segment source module
DATA.
DATA SEGMENT COMMON DATA SEGMENT COMMON
ARY DW 100 DUP(?) NUM DW 100 DU P(?)
COUNT DW ? N DW ?
SUM DW ? TOTAL DW ?
DATA ENDS DATA ENDS

In either case the segments DATA would be overlaid by the linking process and the first 100
words of DATA would contain the numbers being summed, the next word would contain the
count, and the last word would hold the result.

32
• Whether the variables are referred to directly or a common area is employed, both of the
above approaches to communicating with the procedure PROADD are restricted to summing
the numbers in the same area of memory whenever the procedure is called.

• The only way the numbers in a different array WEIGHTS could be added would be to transfer
them to ARY (or NUM) and the count to COUNT (or N) before calling the procedure, and
then transfer the result from SUM (or TOTAL) to its desired location after the procedure has
been executed.

• This could require a considerable amount of code and execution time since there could be as
many as 102 numbers to be moved.

33
• The solution is to pass the addresses of the array, the count, and the result to the procedure,
thus enabling the procedure to work directly with different memory locations each time it is
called.

• Even though 102 words may be involved, only three addresses would need to be passed
since the array elements are in sequence.

• The variables whose addresses are passed are called parameters .

• There are two principal ways of passing parameter addresses:


(1) to construct a parameter address table (or array) and pass the address of the table via a register,

(2) to push the parameter addresses onto the stack.

34
Recursive Procedures
• Sometimes an algorithm is defined in terms of itself. That is, the algorithm is nested within
itself in such a way that its computational procedure calls upon itself. As an example,
Horner’s rule for evaluating the polynomial
((. . . ((anx + an _ 1 )x + an _ 2 )x + . . ,)x + a^)x + a0
• involves multiplying x and an , adding an _ 1 to the result, multiplying x times this result,
adding an _ 2 1° this result, and so on.
• The next result is obtained by multiplying the previous result by x and adding the next lower
coefficient.
• A single application of the algorithm consists of a multiplication and an addition and the
overall result could be obtained by nesting the simple multiply-add algorithm within itself.
• Such algorithms are said to be recursive and are sometimes needed in systems programming
for performing searches, decoding the syntax of a high-level language, and executing other
tasks.

35
• Recursive algorithms may be implemented by having a procedure call itself, but care must be taken to assure that each
successive call does not destroy the parameters and results generated by the previous call and to make sure that the
procedure does not modify itself.

• This means that each call must store its set of parameters, registers, and all temporary results in a different place in memory.

• To guarantee that separate areas of memory are used a stack is normally employed.

• By pushing the data onto the stack as shown in Fig. 4-23, it can be conveniently retrieved in reverse order as the process
emerges from its nesting.

• The data stored by an application of the procedure is called a frame, and the 8086’s BP register can be used to permit ready
access to the individual items within a frame.

• Normally, a frame contains the saved register contents, the parameter addresses, and a temporary storage area.

• Properly storing the information on the stack provides for an orderly exit.

• To prevent indefinite nesting of a recursive algorithm, clearly the algorithm must include a conditional branch that will
eventually allow the nest to be exited.

36
• A trivial example of a recursive procedure is one for evaluating factorials.
• Computing a factorial can be done using a recursive procedure FACT as follows:
• BEGIN FACT(N,RESULT)
• SAVE registers on stack
• IF N = 0 THEN
RESULT <- 1
• ELSE
PUSH address of RESULT onto stack
PUSH N-1 onto stack
CALL FACT (N-1 RESULT)
RESULT N*RESULT
• ENDIF
RESTORE registers from stack
DELETE parameters from stack
• RETURN
37
Figure 4-23 Use of a stack to
dynamically provide storage during
• The parameters N and RESULT represent the factorial recursive calls.
to be found and the result, respectively.

• The program for implementing this algorithm is given


in Fig. 4-24.

• Although the above description indicates only the


parameters as being pushed onto and popped from the
stack, the program includes the other necessary stack
activity.

• A structure is used to define the current stack frame and


the BP register is used to access the frame.

38
• Even though using a
recursive procedure is an
inefficient and
unreasonable way of
making a simple factorial
calculation, the example
does demonstrate the
essentials of recursive
programming.

39
• VISPERA.CO
• Fortunately for retailers, this is where computer vision excels.
• Integrated, end-to-end smart shelving solution that delivers a digitized
view of store inventory with over 99% recognition accuracy.
• Powered by
• Cisco Hyperflex™ Edge
• the Intel® Distribution of OpenVINO™ toolkit,
• Vispera ShelfSight*

40
Role at Intel
• As a performance optimization engineer, you’ll conduct experiments and analysis to identify
performance challenges and uncover optimization opportunities for every major software stack that
runs on Intel® architecture. Our internal engineering and marketing teams, as well as external
independent software vendors and original equipment manufacturers will count on your results to
influence software and hardware products and market to end users.
• Application and middleware optimization engineers use expertise in the software stack,
performance characterization, and hardware architecture to analyze performance of
software/hardware combinations and help advance our product roadmap.
• Software performance engineers characterize, analyze and fine-tune application software to help
deliver optimal performance on Intel architecture
• You’ll have the advantage of working in a leading software environment and the kind of flexibility
that only a cross-platform company like Intel can offer.
• So, if you want the opportunity to influence hardware designs to enable new software capabilities
and maximize performance, this is the role for you.

41
• What We Make Possible
• Software—Intel's Best-Kept Secret?
• Intel is more than a silicon company.
• In fact, we’re one of the largest and most influential software
companies in the world.
• We’re committed to enabling great computing experiences and that
demands the ideal interaction between hardware and software.
• To help make that happen, we've focused on working internally and by
helping the external developer community deliver an Intel—optimized
software stack for everything from smartphones to the cloud and more!

42
43

You might also like