You are on page 1of 61

Lecture 12

Revision
Introduction
• Microprocessor is a digital device on a chip which
can fetch instructions from a memory, decode and
execute them i.e. performs certain arithmetic and
logical operations, accept data from input device,
and send results to output devices.
• Therefore, a microprocessor interfaced with
memory and Input/ Output devices forms a
Microcomputer.
Von-Neumann Architecture
Control Unit

• The control unit performs the most important


function in a computer.
• It controls all other units and controls the flow of
data from one unit to another for performing
computations.
• It also sequences the operations.
• It instructs all the units to perform the task in a
particular sequence with the help of clock pulses.
ALU
• This unit is used for performing arithmetic
operations such as Addition, Subtraction,
Multiplications, division and other logical
operations on the data.
• The control unit guides ALU which of the operations
are to be performed.
• The sequence of the instructions is controlled by
the control unit.
Address Bus

• The address bus is unidirectional and is to be used by the


CPU to send out address of the memory location to be
accessed.
• It is also used by the CPU to select a particular input or
output port.
• It may consist of 8, 12, 16, 20 or even more number of
parallel lines.
• Number of bits in the address bus determines the minimum
number of bytes of data in the memory that can be
accessed.
• A 16-bit address bus for instance can access bytes of data.
Data Bus

• Data bus is bidirectional, that is, data flow occurs both to and
from CPU and peripherals.
• A microprocessor is characterized by the width of its data
bus.
• The size of the internal data bus determines the largest
number that can be processed by a microprocessor, for
instance, having a 16-bit internal data bus is 65536 (64K).
• A microprocessor is specified by its ‘Word Size’, e.g. 4-bit, 8-
bit, 16-bit etc.
• By the term ‘word size” means the number of bits of data
that is processed by the microprocessor as a unit.
• It also specifies the width of the data bus.
Control Bus

• Control bus contains a number of individual lines carrying


synchronizing signals.
• The control bus sends out control signal to memory, I/O
ports and other peripheral devices to ensure proper
operation.
• For instance, if it is desired to read the contents of a
particular memory location, the CPU first sends out address
of that very location on the address bus and a ‘Memory
Read’ control signal on the control bus.
• The memory responds by outputting data stored in the
addressed memory location on the data bus.
16-Bit 8086 Processor Architecture
 16-bit Arithmetic Logic Unit (16-bit data bus)
 20-bit address bus – 1,048,576 = 1 Mega
 16 I/O lines so it can access 64K I/O ports
 16-bit flag Register
 It has 14, all of 16-bit registers
 Clock frequency range is 5-10 MHZ
 Rich set of instructions
40 Pin DIP (Dual in-line Package), Operates in two modes
Bus Interface Unit (BIU):

• In simple words, the BIU handles all transfers of


data and addresses on the buses for the execution
unit.
• The BIU sends out addresses, fetches instructions
from memory, reads data from ports and memory,
and writes data to ports and memory.
The Execution Unit (EU):

• The execution unit of the 8086 tells the BIU where to fetch
instructions or data from, decodes instructions, and
executes instructions.
• The EU has a 16-bit arithmetic logic unit (ALU) which can
add, subtract, AND, OR, XOR, increment, decrement,
complement or shift binary numbers.
• The EU contains control circuitry, which directs internal
operations.
• The main functions of EU are:
o Decoding of Instructions
o Execution of instructions
Steps:

 EU extracts instructions from top of queue in BIU


 Decode the instructions
 Generates operands if necessary
 Passes operands to BIU & requests it to perform read or
write bus cycles to memory or I/O
 Perform the operation specified by the instruction on
operands
Memory Segmentation
• The complete 1MB of memory can be divided into 16
segments, each of 64KB size and is addressed by one
of the segment registers.
• The 16-bit contents of the segment register actually
point to the starting location of a particular segment.
• To address a specific memory location within a
segment, we need an offset address.
• The offset address values are from 0000 to FFFF so
that the physical addresses range from 00000 to
FFFFF.
Cont.,
• 16-bit segmented value are placed in one of the segment
registers
• CPU automatically converts segment-offset address into 20-
bit physical address using the following equation:
Physical address = Segment address * 10H + Offset address.
Where:
* 10H means you shift the segment address 4-bit to the left
Addressing modes for sequential control transfer
instructions

• We can classify the 8086 addressing modes into:


- Register Addressing Modes
- Memory Addressing Modes
- Immediate Addressing Mode.
• Where Register & Memory addressing transfers a copy of a
byte or word from the source addressing register or
contents of a memory location to the destination register or
memory location.
• Immediate addressing transfers the source, an immediate
byte, word, into the destination register or memory
location.
1. Immediate
• In this type of addressing, immediate data is a part of
instruction and appears in the form of successive byte or
bytes.
• Ex:
MOV AX, 0005H
• In the above example, 0005H is the immediate data.
• The immediate data may be 8-bit or 16-bit in size.
• Ex:
2. Direct Addressing Mode
• Direct addressing moves a byte or word between a memory
location addressing and a register.
• In the direct addressing mode a 16-bit memory address
(offset) is directly specified in the instruction as a part of it.
• Ex: MOV AX, [5000H]
• Here, data resides in a memory location in the data
segment, whose effective address may be completed using
5000H as the offset address and content of DS as segment
address. The effective address here, is 10H * DS + 5000H.
• Ex:
3. Register:
• In register addressing mode, the data is stored in a register
and is referred using the register.
• All the registers, except IP, may be used in this mode.
• Ex:
MOV AX, BX
4. Register Indirect
• Register indirect addressing transfers a byte or word
between an addressing register and a memory location
addressed by an index or base register (offset).
• Sometimes, the address of the memory location, which
contains data or operand, is determined in an indirect way,
using the offset register.
• This mode of addressing is known as register indirect mode.
• In this addressing mode, the offset address of data is in
either BX or SI or DI register.
• The default segment is either DS or ES.
Cont.,
• The data is supposed to be available at the address pointed
to by the content of any of the above registers in the default
data segment.
• Ex: MOV AX, [BX]
• Here, data is present in a memory location in DS whose
offset address is in BX. The effective address of the data is
given as 10H * DS+[BX].
• Ex:
5. Indexed:
• In this addressing mode, offset of the operand is stored in
one of the index registers.
• DS and ES are the default segments for index registers, SI
and DI respectively.
• This is a special case of register indirect addressing mode.
• Ex:
MOV AX, [SI]
• Here, data is available at an offset address stored in SI in DS.
• The effective address, in this case, is computed as:
10 H*DS+[SI].
6. Register Relative
• Register relative addressing moves a byte or word
between a register addressing and the memory
location addressed by an index or base register plus
a displacement.
• In this addressing mode, the data is available at an
effective address formed by adding an 8-bit or 16-
bit displacement with the content of any one of the
registers BX, BP, SI and DI in the default (either DS
or ES) segment.
Cont.,
• Ex:
MOV AX, 50H[BX]
Here, the effective address is given as 10H *DS+50H+[BX]
Ex:
7. Based Indexed
• The effective address of data is formed, in this addressing
mode, by adding content of a base register (any one of BX or
BP) to the content of an index register (any one of SI or DI).
• The default segment register may be ES or DS.
• Ex:
MOV AX, [BX][SI]
• Here, BX is the base register and SI is the index register the
effective address is computed as 10H * DS + [BX] + [SI].
8. Relative Based Indexed
• The effective address is formed by adding an 8 or 16-bit
displacement with the sum of the contents of any one of
the base register (BX or BP) and any one of the index
register, in a default segment.
• Ex:
MOV AX, 50H [BX] [SI]
• Here, 50H is an immediate displacement, BX is base register
and SI is an index register the effective address of data is
computed as:
10H * DS + [BX] + [SI] + 50H
Part II
Assembly Language
• Program Statement:
• Programs consist of statements, one per line.
• Each statement is either an instruction, which the assembler translates
Statement
into machine code, or an assembler directive, which instructs the
assembler to perform some specific task, such as allocating memory
s space for a variable or creating a procedure .
• Both instructions and directives have up to four fields:
name operation operand(s) comment
Where, At least one blank or tab character must separate the fields.
Example
• An example of an Instruction is:
START: MOV CX, 5 ;initialize counter
• Here, the name field consists of the label START:, the
operation is MOV, the operands are CX and 5, and the
comment is ;initialize counter.
• An example of a directive is:
MAIN PROC
• MAIN is the name, and the operation field contains PROC.
• This particular directive creates a procedure called MAIN.
Arrays
• In assembly language, an array is just a sequence of memory
bytes or words.
• for example, to define a three-byte array called B_ARRAY, whose
initial values are : l0h, 20h, and 30h, we can write,
B _ARRAY DB 10H,20H,30H
Where, the name of the array is associated to the first address ,
B_ARRAY + 1 with the address of the second byte, and so on.
• In the same way, an array of words may be defined.
• For example,
W_ ARRAY DW 1000,40,29887,329
Where, the name of the array is associated to the first address ,
W_ARRAY + 2 with the address of the second word, and so on.
Putting It Together

• Now that you have seen all the program segments, we can construct the
general form of a .SMALL model program.
.MODEL SMALL
.STACK 100H
.DATA
;data definitions go here
.CODE
MAIN PROC
;instructions go here
MAIN ENDP
; other procedures go here
END MAIN
INT 21h

• INT 21h may be used to invoke a large number of DOS


functions.
• A particular function is requested by placing a function
number in the AH register and invoking INT 2lh.
• Here we are interested in the following functions:
Function number Routine
1 single-key input
2 single-character output
9 character string output
4CH DOS exit function
Overflow
• The phenomenon of overflow is associated with the fact
that the range of numbers that can be represented in a
computer is limited.
• The (decimal) range of signed numbers that can be
represented by a 16-bit word is -32768 to 32767; and for
unsigned from 0 to 65535.
• If the result of an operation falls outside these ranges,
overflow occurs and the truncated result that is saved will
be incorrect.
• The processor sets OF= 1 for signed overflow and CF= 1 for
unsigned overflow.
Unsigned Overflow
• On addition, unsigned overflow occurs when there is a carry
out of the MSB. This means that the correct answer is larger
than the biggest unsigned number; that is, FFFFh for a word
and FFh for a byte.
• On subtraction, unsigned overflow occurs when there is a
borrow into the MSB. This means that the correct answer is
smaller than 0.
Signed Overflow
1. On addition of numbers with the same sign,
signed overflow occurs when the sum has a
different sign. For example: ADD 7FFFh + 7FFFh,
(two positive numbers), but got FFFEh (a negative
result). 0111 1111 1111 1111
+0111 1111 1111 1111
-----------------------------
1111 1111 1111 1110
2. On addition of numbers with different signs,
overflow is impossible, because a sum like A + ( -
B) is really A – B. OF = 0
Cont.,
3. Subtraction of numbers with different signs is like
adding numbers of the same sign.
For example, A - ( -B) = A + B and -A -(+B) = -A + -B.
Signed overflow occurs if the result has a different
sign than expected.
4- Subtraction of numbers with the same sign cannot
give overflow.
Flow Control Instructions
• For assembly language programs to carry out useful
tasks, there must be a way to make decisions and
repeat sections of code.
• The jump and loop instructions transfer control to
another part of the program.
• This transfer can be conditional based on the status
flag settings or unconditional.
A- Conditional Jumps
• The syntax is: Jxxx destination label
• For example, JNZ (jump if the result of the previous
operation is not equal zero) is an example of a
conditional jump instruction.
• Where, if the condition is true, the next instruction
to be executed is the one at destination label, which
may precede or follow the jump instruction itself.
• IP register adjusted to point to the destination label
• If the condition is false, the instruction immediately
following the jump is done next.
CMP Instruction
• Syntax:
CMP destination, source
• Where, it compares destination and source by
computing destination contents minus source
contents. (as SUB, except that the destination is not
changed)
• The result is not stored, but the flags are affected.
• The jump condition is often provided by the CMP
(compare) instruction.
B- Unconditional Jump (JMP)
• Syntax:
JMP destination_label
For example:
TOP:
; body of the loop
DEC CX
JNZ BOTTOM
JMP EXIT
BOTTOM:
JMP TOP
EXIT:
MOV Ax, BX
Branching Structures
• In high-level languages, branching structures enable
a program to take different paths, depending on
conditions.
• In this section, we'll look at three structures:
1- IF-THEN
2- IF-THEN-ELSE
3- CASE
IF-THEN
• The IF-THEN structure may be expressed in
pseudocode as follows:
IF condition is true
THEN
execute true-branch statements
END_IF
Where, the condition is an expression that is true or
false.
CASE
• A CASE is a multiway branch structure that tests a register,
variable, or expression for particular values or a range of
values.
• The general form is as follows:
CASE expression
values_1 : statements_1
values_2 : statements_2
.
.
values_n: statements_n
END_CASE
Looping Structures
• A loop is a sequence of instructions that is repeated.
• The number of times to repeat may be known in advance, or
it may depend on conditions.
• The LOOP instruction can be used to implement a FOR
loop.
• It has the form:
LOOP destination_label
• The counter for the loop is the register CX which is
initialized to loop_count.
Multiplication and Division Instructions

• In this part, we introduce instructions for


multiplying and dividing any numbers.
• The process of multiplication and division is
different for signed and unsigned numbers, so there
are different instructions for signed and unsigned
multiplication and division.
• Because signed and unsigned multiplication lead to
different results.
MUL and IMUL

• Because signed and unsigned multiplication lead to different


results there are two multiplication instructions:
MUL for unsigned and
IMUL (integer multiply) for signed multiplication.
• These instructions multiply bytes or words.
• If two bytes are multiplied, the product is a word (16 bits).
• If two words are multiplied, the product is a doubleword
(32 bits).
• The syntax of these instructions is:
MUL source
Or
IMUL source
Cont.,
• Byte form
• For byte multiplication, one number is contained in the
source and the other is assumed to be in AL.
• The 16-bit product will be in AX.
• The source may be a byte register or memory byte, but not
a constant.
• Word Form
• For word multiplication, one number is contained in the
source and the other is assumed to be in AX.
• The most significant 16 bits of the doubleword product will
be in DX, and the least significant 16 bits will be in AX.
DIV and /IDIV

• When division is performed, we obtain two results, the


quotient and the remainder.
• As with multiplication, there are separate instructions for
unsigned and signed division; DIV (divide) is used for unsigned
division and IDIV-(integer divide)-for signed division.
• The syntax is:
DIV divisor
Or
IDIV divisor
• The quotient and remainder have the same size as the divisor
Cont.,
• Byte Form
• In this form, the divisor is an 8-bit register or memory byte.
• The 16-bit dividend is assumed to be in AX.
• After division, the 8-bit quotient is in AL and the 8-bit remainder is in
AH.
• The divisor may not be a constant.
• Word Form
• Here the divisor is a 16-bit register or memory word.
• The 32-bit - dividend is assumed to be in DX : AX, After division, the 16-
bit quotient is in AX and the 16-bit remainder is in DX.
• The divisor may not he a constant.
Stack, Procedure , and
Recursion
When to use stacks
• Temporary save area for registers
• To save return address for CALL
• To pass arguments
• Local variables
• Applications which have LIFO nature such as
reversing a string
PUSH

• To add a new word to the stack we PUSH it on.


• The syntax:
PUSH source
• where source is a 16-bit register or memory word.
• For example,
PUSH AX
• Execution of PUSH cause the following to happen:
1. SP is decreased by 2.
2. A copy of the source content is moved to the
address specified by SS:SP. The source is unchanged.
POP

• To remove the top item from the stack, we POP It.


• The syntax Is
POP destination
• Where destination is a 16-bit register or memory word.
• For example,
POP BX
• Executing POP causes this to happen:
1. The content of SS:SP (the top of the stack) is moved to
the destination.
2. SP is increased by 2
Procedure

• The idea of top-down program design is to take the original


problem and decompose it into a series of subproblems that
are easier to solve than the original problem.
• High-level languages usually employ procedures to solve
these subproblems, and we can do the same thing in
assembly language.
• Thus an assembly language program can be structured as a
collection of procedures.
• It is also possible for these procedures to call each other, or
for a procedure to call itself.
Procedure Declaration

• The syntax of procedure declaration is the following:


name PROC type
;body of the procedure
RET
name ENDP
Name is the user-defined name of the procedure.
• The optional operand type is NEAR or FAR (if type is omitted,
NEAR is assumed).
• NEAR means that the statement that calls the procedure is in the
same segment as the procedure itself; FAR means that the calling
statement is in a different segment.
• In the following, we assume all procedures are NEAR.
CALL

• To invoke a procedure, the CALL instruction is used.


• There are two kinds of procedure calls, direct and indirect.
• The Syntax of a direct procedure call is:
CALL name
• Where name is the name of a procedure.
• The syntax of an indirect procedure call is :
CALL address_expression
• Where address_expression specifics a register or memory
location containing the address of a procedure.
RET
• The RET (return) instruction causes control to transfer back
to the calling procedure.
• Every procedure (except the main procedure) should have a
RET someplace; usually it's the last statement in the
procedure.
• A procedure must have a way to receive values from the
procedure that calls It, and a way to return' results.
Thanks

You might also like