Professional Documents
Culture Documents
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
• 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
• and the third is to permit macros, to be inserted by the appearance of single statements
5
Linking and Relocation
Assembling: Program modules may be put in
• The same source module and assembled together
Linker :
• Object modules a load module
• Prints a memory map : where the linked object modules will be loaded into memory.
• 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.
• 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.
• 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
• List 2 : the locally defined identifiers that can be referred to by other modules.
11
• Assembler inserts the offset address for the local identifiers
• Linking process inserts the offset address for the external identifiers and all 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 ES,AX ;
ADD DX,ES:VAR
• 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.
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.
• The return is always made to the instruction immediately following the call regardless of where
the call is located.
• Disadvantage : Extra code is needed to join them together in such a way that they can
communicate with each other.
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
• 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
• 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.
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 = 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,
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
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.
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.
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
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.
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.
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