Chapter 3
Chapter 3
4. DX: This is the data register. It is of 16 bits and is divided into two 8-bit registers
DH and DL to also perform 8-bit instructions. It is used in the multiplication and
input/output port addressing.
Example:
MUL BX (DX, AX = AX * BX)
5. SP: This is the stack pointer. It is of 16 bits. It points to the topmost item of the
stack. If the stack is empty the stack pointer will be (FFFE)H. Its offset address is
relative to the stack segment.
6
SP (stack pointer):
• Contains an assumed offset value of the top of the stack
• Combined with the SS register form the complete logical address of the top of the
stack
• The stack is maintained as a LIFO with its bottom at the start of the stack segment
(specified by the SS segment register)
• The top of the stack (the location of the last data in the stack) is specified by the
offset stored in the SP register
11
• BP (base pointer):
• As it is with the stack pointer, this register can hold an offset from the SS register
• It is usually used by subroutines to locate variables that were passed on the stack
by a calling program
• Unlike the SP register, the BP can be used to specify the offset of other program
segments
12
• SI (source index):
• Used in conjunction with the DS register to point to data locations in the data
segment.
• Used with string movement instructions. It would point to the source string.
• DI (destination index):
• Similar function to the SI
• Used in conjunction with the ES register in string operations. In string movement
instructions it points to the destination string
13
• IP (Instruction Pointer):
• This is the register used for accessing instructions
• While the CS register contains the segment number of the next instruction, the IP
contains the offset of that instruction in the code segment
• As was shown in the section on the fetch-execute cycle in the first unit, the IP
register gets updated by the control unit every time an instruction is executed
such that it will always point to the next instruction
• Unlike other registers, the IP can not be manipulated by instructions (i.e. it
cannot appear as an operand in any instruction)
14
Condition Code Registers
• A status register, flag register, or condition code register is a collection of status
flag bits for a processor. An example is the FLAGS register of the x86
architecture. The flags might be part of a larger register, such as a program
status word register.
• Condition codes are extra bits kept by a processor that summarize the results
of an operation and that affect the execution of later instructions. These bits
are often collected together in a single condition or indicator register (CR/IR) or
grouped with other status bits into a status register (PSW/PSR).
15
Flags are discussed in previous Chapter
• To revisit flags here is a short description.
1. Zero Flag (ZF): is set to 1 when result is zero. For non-zero result this flag is set
to 0.
2. Sign Flag (SF): Sign Flag is set to 1 when result is negative. When result is
positive it is set to 0. This flag takes the value of the most significant bit.
3. Carry Flag (CF): Carry Flag is set to 1 when there is an unsigned overflow. For
example when you add bytes 255 + 1 (result is not in range 0…255). When
there is no overflow this flag is set to 0.
16
4. Overflow Flag (OF): Overflow Flag is set to 1 when there is a signed overflow.
For example, when you add bytes 100 + 50 (result is not in range -128…127).
5. Parity Flag (PF): Parity Flag is set to 1 when there is even number of one bits in
result, and to 0 when there is odd number of one bits.
6. Auxiliary Flag (AF): Auxiliary Flag is set to 1 when there is an unsigned
overflow for low nibble (4 bits).
17
Stack operation in 8086 microprocessor
The stack is a block of memory that may be used for temporarily storing the
contents of registers inside CPU.
• Stack is accessed by using SP and SS.
• Stack is a Top Down Data Structure whose elements are accessed by using a
pointer (SP,SS).
• The stack is required when CALL instruction is used.
• Push
• Pop
• Top of stack
• Stack pointer
18
Cont..
The Stack pointer keeps track of the position of the last item placed on the stack
(i.e. the Top of Stack)
The Stack is organized in words, (i.e. two bytes at a time). Thus the stack pointer is
incremented or decremented by 2.
The Stack Pointer points to the last occupied locations on the stack
19
Cont..
20
Stack instructions
Flags: Only affected by the popf instruction Addressing mode: src and dst
should be words and cannot be immediate. dst cannot be the IP or CS
register
21
PUSH & POP
• • The two set of instructions which explicitly modify the stack are the PUSH
(which places items on the stack) and the POP (which retrieves items from the
stack).
• • In both cases, the stack pointer is adjusted accordingly to point always to the
top of stack.
• • Thus PUSH AX means SP=SP-2 and AX -> [SP]
• • POP AX means [SP] -> AX and SP=SP+2.
22 Cont..
23 Example:
• Using the stack, swap the values of the ax and bx registers, so that ax now contains what
bx contained and bx contains what ax contained. (This is not the most efficient way to
exchange the contents of two variables). To carry out this operation, we need at least one
temporary variable:
push ax ; Store ax on stack push
bx ; Store bx on stack
pop ax ; Copy last value on stack to ax
pop bx ; Copy first value to bx
Push ax
Mov ax, bx
Pop bx
24
Introduction to Assembly Language programming
• It teaches you about the way the computer's hardware and operating system work
together and how, the application programs communicate with the operating
system. Assembly language, unlike high level languages, is machine dependent.
Each microprocessor has its own set of instructions, that it can support.
You must learn assembly language for various reasons:
• 1. It helps you understand the computer architecture and operating system.
• 2. Certain programs, requiring close interaction with computer hardware, are
sometimes difficult or impossible to do in high level languages.
25
Assembly Language Syntax
An assembly language program consists of statements. The syntax of an assembly
language program statement obeys the following rules:
- Only one statement is written per line.
- Each statement is either an instruction or an assembler directive.
- Each instruction has an opcode and possibly one or more operands.
- An opcode is known as a mnemonic.
- Each mnemonic represents a single machine instruction.
- Operands provide the data to work with.
26
Assembler Directives
• Pseudo instructions or assembler directives are instructions that are directed to the
assembler. Assembler directives affect the generated machine code, but are not translated
directly into machine code. Directives can be used to declare variables, constants,
segments, macros, and procedures as well as supporting conditional assembly.
• Segment directives Segments are declared using directives.
The following directives are used to specify the following segments:
.stack
.data
.code
27
Instructions
• An instruction in assembly language is a symbolic representation of a single
machine instruction. In its simplest form, an instruction consists of a mnemonic
and a list of operands.
• A mnemonic is a short alphabetic code that assists the CPU in remembering an
instruction. This mnemonic can be followed by a list of operands. Each
instruction in assembly language is coded into one or more bytes.
28 Instruction Semantics:
The following rules have to be strictly followed in order to write correct code.
1. Both operands have to be of the same size:
Instruction Correct Reason
MOV AX, BL No Operands of different sizes
MOV AL, BL Yes Operands of same sizes
MOV AH, BL Yes Operands of same sizes
MOV BL, CX No Operands of different sizes
29