Module 2 Programming 8086 - swatitcet.files.wordpress.com · Module 2 Programming 8086 2.1...
Transcript of Module 2 Programming 8086 - swatitcet.files.wordpress.com · Module 2 Programming 8086 2.1...
Module 2
Programming 8086
2.1 Motivation
Motivation of this course is to provide the basic knowledge of addressing modes
of 8086 microprocessor, instruction set of 8086, Assembly language programming ,
mixed language programming using stacks, strings, procedures , macros, timers,
counters & delay.
2.2 Syllabus
Module Contents Duration Self study
2.1 Addressing modes 2 Hours 2 Hours
2.2 Instruction Set 3 Hours 4Hours
2.3 Assembly Language Programming, 1Hours 2 Hours
2.4 Mixed Language Programming 1 Hours 2 Hours
2.5 Programs based on Stacks, Strings, Procedures, Macros, Timers, Counters & delay
3 Hours 5 Hours
2.3: Weightage:
2.4 Learning Objective / Learning Outcome
2.4.1 Learning Objective
Students will try analyzing processor performance improvement using instruction level
parallelism.
2.4.2 Learning Outcome
Students will be able to write assembly language program for given task for 8086
microprocessor.
2.5 Prerequisite:
2.6: Key Definition:
2.7 Introduction
2.7.1 Addressing modes Of 8086 Microprocessor
The 8086 microprocessor accesses the data in different ways such as from different registers, from memory locations or from I/O ports are called its addressing modes. These addressing modes are categorized according to the accessing method. These are as follows.
1. Register Addressing Modes (Accessing data from registers) 2. Immediate Addressing Modes (Accessing immediate data and storing in the
register as an operand ) 3. Memory Addressing Modes (Accessing data from memory) 4. Direct Addressing Modes (Accessing direct data from I/O port) 5. Again some instruction are classified according to their behavior or condition,
these are as follows. 6. Relative addressing modes (Related with some condition) 7. Implied or Implicit addressing mode (No operands)
1. Register Addressing Modes
In register addressing mode, most 8086 instructions can operate the general purpose register to set as an operand to the instruction. This means a register is a source of an operand as well as the register is only the destination of an operand for an instruction.
MOV destination, source; This instruction copies the data from the source location to the destination location. The 8-bit or 16 bit registers are certainly valid operands for this instruction. But both operands should be in the same size. Now let's see some 8086 MOV instructions: MOV AX, BX; Copies the 16 bit value from BX into AX, Here the contents of AX is overlapping, but the contents of BX are not changed. (Both registers are in same size)
Example:
MOV DL, AL; Copies the value from AL into DL MOV SI, DX; Copies the value from DX into SI MOV SP, BP; Copies the value from BP into SP MOV CH, CL; Copies the value from CL into CH MOV AX, AX; yes, this is legal!
2. Immediate Addressing Modes
In immediate addressing mode, the hexadecimal number either 8-bit or 16-bit to be loaded immediately into the memory locations or consecutive memory locations respectively. i.e to load 8-bit immediate number into an 8-bit memory location or to load a 16-bit immediate number into two consecutive memory locations. Some examples of that instructions.
Example:
MOV CX, 437BH; copies 16-bit hexadecimal number 437BH in the 16-bit CX register MOV CL, 48H; Load the 8-bit immediate number 48H into the 8-bit CL register
3. Memory Addressing Mode:
The memory addressing modes are used to specify the location of an operand as memory. To access data in memory, the 8086 should be produce a 20-bit physical address. It does this by adding a 16-bit value called the effective address (EA). The effective address represents the displacement or offset of the desired operand from the segment base. The data segment is most often used as a segment base. MOV AL, [BX]; ADD the contents of DS (One of four segment bases) with BX and result will shift in AL register. MOV AL, [BP]; same as above The square bracket around the registers i.e. BX, BP are shorthand for “the contents of that registers at a displacement from the segment base of ."
Fig 2.1
4. Direct Addressing modes
This addressing mode is called direct because the displacement or offset of the operand from the segment base is specified directly in the instruction. MOV CL, [437AH]; copy the contents of the memory location , at a displacement of 437AH from the data segment base , into the CL register. MOV BX [437AH]; copies a word from memory into the BX register. Note: each memory address represents a byte of storage, the word must come from two memory locations. The byte at a displacement of 437AH from the data segment base will be copied into BL. The contents of the next higher address, displacement 437BH will be copied into the BH register. The 8086 will automatically access the required number of bytes in memory for a given instruction.
5. Relative addressing modes
This addressing mode is a relation base. The data is stored either in base pointer (BP) or in BX. Example: MOV AX, [BP +1] JMP [BX + 1] JNC START : if CF=0 or not carry then start the PC
6. Implicit or Implied addressing modes
In this addressing mode no operands are used to execute the instruction. Example: NOP : No operation CLC : Clear carry flag to 0 START : Start execution
2.7.2 Instruction Set of 8086
Instruction set of 8086 microprocessor is briefed in below table
AAA AAD AAM AAS ADC ADD AND CALL CBW CLC CLD CLI CMC CMP
CMPSB CMPSW CWD DAA DAS DEC DIV HLT IDIV IMUL IN INC INT INTO IRET JA
JAE JB JBE JC JCXZ JE JG JGE JL JLE JMP JNA JNAE JNB
JNBE JNC JNE JNG JNGE JNL JNLE JNO JNP JNS JNZ JO JP JPE
JPO JS JZ LAHF LDS LEA LES LODSB LODSW LOOP LOOPE LOOPNE LOOPNZ LOOPZ
MOV MOVSB MOVSW MUL NEG NOP NOT OR OUT POP POPA POPF PUSH PUSHA PUSHF RCL
RCR REP REPE REPNE REPNZ REPZ RET RETF ROL ROR SAHF SAL SAR SBB
SCASB SCASW SHL SHR STC STD STI STOSB STOSW SUB TEST XCHG XLATB XOR
The 8086 instructions are categorized into the following main types. i. Data Copy / Transfer Instructions ii. Arithmetic and Logical Instructions iii. Branch Instructions iv. Loop Instructions v. Machine Control Instructions vi. Flag Manipulation Instructions vii. Shift and Rotate Instructions viii. String Instructions
Data Copy / Transfer Instructions : MOV :
This instruction copies a word or a byte of data from some source to a destination. The destination can be a register or a memory location. The source can be a register, a memory location, or an immediate number. MOV AX,BX MOV AX,5000H MOV AX,[SI] MOV AX,[2000H] MOV AX,50H[BX]
MOV [734AH],BX MOV DS,CX MOV CL,[357AH] Direct loading of the segment registers with immediate data is not permitted.
PUSH : Push to Stack
This instruction pushes the contents of the specified register/memory location on to the stack. The stack pointer is decremented by 2, after each execution of the instruction. E.g. PUSH AX • PUSH DS • PUSH [5000H]
Fig 2.2
POP : Pop from Sack
This instruction when executed, loads the specified register/memory location with the contents of the memory location of which the address is formed using the current stack segment and stack pointer. The stack pointer is incremented by 2 Eg. POP AX POP DS POP [5000H]
Fig 2.3
XCHG : Exchange byte or word
This instruction exchange the contents of the specified source and destination operands Eg. XCHG [5000H], AX XCHG BX, AX
XLAT :
Translate byte using look-up table Eg. LEA BX, TABLE1 MOV AL, 04H XLAT Simple input and output port transfer Instructions:
IN:
Copy a byte or word from specified port to accumulator. Eg. IN AL,03H IN AX,DX
OUT:
Copy a byte or word from accumulator specified port. Eg. OUT 03H, AL OUT DX, AX
LEA :
Load effective address of operand in specified register.[reg] offset portion of address in DS Eg. LEA reg, offset
LDS:
Load DS register and other specified register from memory. [reg] [mem] [DS] [mem + 2] Eg. LDS reg, mem
LES:
Load ES register and other specified register from memory. [reg] [mem] [ES] [mem + 2] Eg. LES reg, mem
2.7.3 Assembly Language Programming
Assembly Language:
Each personal computer has a microprocessor that manages the computer's
arithmetical, logical, and control activities.
Each family of processors has its own set of instructions for handling various
operations such as getting input from keyboard, displaying information on screen and
performing various other jobs. These set of instructions are called 'machine language
instructions'.
A processor understands only machine language instructions, which are strings
of 1's and 0's. However, machine language is too obscure and complex for using in
software development. So, the low-level assembly language is designed for a specific
family of processors that represents various instructions in symbolic code and a more
understandable form.
Advantages of Assembly Language
Having an understanding of assembly language makes one aware of −
How programs interface with OS, processor, and BIOS;
How data is represented in memory and other external devices;
How the processor accesses and executes instruction;
How instructions access and process data;
How a program accesses external devices.
Other advantages of using assembly language are −
It requires less memory and execution time;
It allows hardware-specific complex jobs in an easier way;
It is suitable for time-critical jobs;
It is most suitable for writing interrupt service routines and other memory
resident programs.
Basic Features of PC Hardware
The main internal hardware of a PC consists of processor, memory, and
registers. Registers are processor components that hold data and address. To execute a
program, the system copies it from the external device into the internal memory. The
processor executes the program instructions.
The fundamental unit of computer storage is a bit; it could be ON (1) or OFF (0).
A group of nine related bits makes a byte, out of which eight bits are used for data and
the last one is used for parity. According to the rule of parity, the number of bits that
are ON (1) in each byte should always be odd.
So, the parity bit is used to make the number of bits in a byte odd. If the parity is
even, the system assumes that there had been a parity error (though rare), which might
have been caused due to hardware fault or electrical disturbance.
The processor supports the following data sizes −
Word: a 2-byte data item
Double word: a 4-byte (32 bit) data item
Quad word: an 8-byte (64 bit) data item
Paragraph: a 16-byte (128 bit) area
Kilobyte: 1024 bytes
Megabyte: 1,048,576 bytes
Addressing Data in Memory
The process through which the processor controls the execution of instructions is
referred as the fetch-decode-execute cycle or the execution cycle. It consists of three
continuous steps −
Fetching the instruction from memory
Decoding or identifying the instruction
Executing the instruction
The processor may access one or more bytes of memory at a time. Let us consider a
hexadecimal number 0725H. This number will require two bytes of memory. The high-
order byte or most significant byte is 07 and the low-order byte is 25.
The processor stores data in reverse-byte sequence, i.e., a low-order byte is stored in a
low memory address and a high-order byte in high memory address. So, if the
processor brings the value 0725H from register to memory, it will transfer 25 first to
the lower memory address and 07 to the next memory address.
Fig 2.4 memory address
When the processor gets the numeric data from memory to register, it again reverses
the bytes. There are two kinds of memory addresses −
Absolute address - a direct reference of specific location.
Segment address (or offset) - starting address of a memory segment with the
offset value.
An assembly program can be divided into three sections −
The data section,
The bss section, and
The text section.
The data Section
The data section is used for declaring initialized data or constants. This data does not
change at runtime. You can declare various constant values, file names, or buffer size,
etc., in this section.
The syntax for declaring data section is −
section.data
The bss Section
The bss section is used for declaring variables. The syntax for declaring bss section is −
section.bss
The text section
The text section is used for keeping the actual code. This section must begin with the
declaration global _start, which tells the kernel where the program execution begins.
The syntax for declaring text section is −
section.text global _start _start:
Comments
Assembly language comment begins with a semicolon (;). It may contain any printable
character including blank. It can appear on a line by itself, like −
; This program displays a message on screen
or, on the same line along with an instruction, like −
add eax, ebx ; adds ebx to eax
Assembly Language Statements
Assembly language programs consist of three types of statements −
Executable instructions or instructions,
Assembler directives or pseudo-ops, and
Macros.
The executable instructions or simply instructions tell the processor what to do. Each
instruction consists of an operation code (opcode). Each executable instruction
generates one machine language instruction.
The assembler directives or pseudo-ops tell the assembler about the various aspects of
the assembly process. These are non-executable and do not generate machine language
instructions.
Macros are basically a text substitution mechanism.
Syntax of Assembly Language Statements
Assembly language statements are entered one statement per line. Each statement
follows the following format −
[label] mnemonic [operands] [;comment]
The fields in the square brackets are optional. A basic instruction has two parts, the
first one is the name of the instruction (or the mnemonic), which is to be executed, and
the second are the operands or the parameters of the command.
Following are some examples of typical assembly language statements −
INC COUNT ; Increment the memory variable COUNT MOV TOTAL, 48 ; Transfer the value 48 in the ; memory variable TOTAL ADD AH, BH ; Add the content of the ; BH register into the AH register AND MASK1, 128 ; Perform AND operation on the ; variable MASK1 and 128 ADD MARKS, 10 ; Add 10 to the variable MARKS MOV AL, 10 ; Transfer the value 10 to the AL register
The Hello World Program in Assembly
The following assembly language code displays the string 'Hello World' on the screen
−
section .text
global _start ;must be declared for linker (ld)
_start: ;tells linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!', 0xa ;string to be printed
len equ $ - msg ;length of the string
When the above code is compiled and executed, it produces the following result −
Hello, world!
Compiling and Linking an Assembly Program in NASM
Make sure you have set the path of nasm and ld binaries in your PATH environment
variable. Now, take the following steps for compiling and linking the above program −
Type the above code using a text editor and save it as hello.asm.
Make sure that you are in the same directory as where you saved hello.asm.
To assemble the program, type nasm -f elf hello.asm
If there is any error, you will be prompted about that at this stage. Otherwise, an
object file of your program named hello.o will be created.
To link the object file and create an executable file named hello, type ld -m
elf_i386 -s -o hello hello.o
Execute the program by typing ./hello
If you have done everything correctly, it will display 'Hello, world!' on the screen.
2.7.4 Mixed Language Programming:
1. There are times when programs need to call programs written in other languages referred as mixed language programming. For example, when a particular subprogram is available in a language other than language you are using, or when algorithms are described more naturally in a different language, you need to use more than one language.
2. Mixed-language programming always involves a call to a function, procedure, or subroutine. Mixed-language calls involve calling functions in separate modules. Instead of compiling all source programs with same compiler, different compilers or assemblers are used as per the language used in the programs.
3. Microsoft C supports this mixed language programming. So it can combine assembly code routines in C as a separate language.
4. C program calls assembly language routines that are separately assembled by-MASM (MASM Assembler). These assembled modules are linked with the compiled C modules to get executable file. Fig shows the compile, assemble and link processes using C compiler, MASM assembler, and TUNIC.
Fig 2.5
2.7.5 Programs based on Stacks, Strings, Procedures, Macros, Timers, Counters &
delay
1.Write and execute an Assembly Language Program (ALP) to 8086 Processor to Add,
Subtract and Multiply 16 bit unsigned numbers. Store the result in extra segment.
ALGORITHM:
1. Start the Data segment
2. Initialize the input1 with 16-bit number and input2 with 16-bit number
3. End the Data segment
4. Start the extra segment
5. End extra segment
6. Assume data, code, extra segments for assembler
7. Initialize Data segment & extra segment
8. Refresh the Contents of AX register with XOR operation
9. Move input1 to AX register
10. Move input2 to BX register
11. Perform addition/subtraction/division operation
12. Move the result to corresponding locations
13. End the Code segment & the program
DATA SEGMENT
Value1 DW1234H
Value2 DW0123H
DATA ENDS
ASSUME CS:CODE, DS:DATA
CODE SEGMENT
START: MOV AX, DATA
MOV DS,AX
MOV AX,0000H
MOV BX,0000H
MOV SI,OFFSET Value1
MOV DI,OFFSET Value2
MOV AX,[SI]
MOV BX,[DI]
ADD AX, BX
INT 3H
CODE ENDS
END START
2. Write and execute an Assembly Language Program (ALP) to 8086 Processor to sort the
given array in ascending & descending order.
ALGORITHM:
1. Start the Data segment
2. Initialize the input values by using Assembler directives
3. Ends the Data segment
4. Start the code segment
5. Initialize START Directive
6. Initialize Data segment
7. Clear the Contents of AX, & BX registers.
8. Move the array values into SI register.
9. Initialize the count register & Move the first content of SI register to AL register
10. Increment SI value by one.
11. Compare the AL content with content of SI register.
12. If result is Positive perform exchange operation between AL register & content of SI register.
13. Decrement SI by one& Move the content of AL to SI location.
14. Increment SI register & Decrement count register by one.
15. Perform conditional statement JNZ LOOP1
16. Decrement BX register by one
17. Perform conditional statement JNZ LOOP2
18. Terminate the program with Interrupt statement.
19. Ends the code segment.
DATA SEGMENT
Num1 DB 52H, 30H, 62H, 42H, 17H
DATA ENDS
ASSUME CS:CODE, DS:DATA
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV AX, 0000H
MOV BX, 0000H
BACK: MOV SI, OFFSET Num1
MOV CX, 0004H
BACK1: MOV AL, [SI]
INC SI
CMP AL, [SI]
JBE X
XCHG AL, [SI]
DEC SI
MOV [SI], AL
INC SI
X: DEC CX
JNZ BACK1
DEC BX
JNZ BACK
INT 3H
CODE ENDS
END START
DESCENDING ORDER
DATA SEGMENT
Num1 DB 52H, 30H, 62H, 42H, 17H
DATA ENDS
ASSUME CS: CODE, DS: DATA
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV AX, 0000H
MOV BX, 0000H
BACK: MOV SI, OFFSET Num1
MOV CX, 0004H
BACK1: MOV AL, [SI]
INC SI
CMP AL, [SI]
JNBE X
XCHG AL, [SI]
DEC SI
MOV [SI], AL
INC SI
X: DEC CX
JNZ BACK1
DEC BX
JNZ BACK
INT 3H
CODE ENDS
END START
3.To Write and execute an Assembly Language Program (ALP) to 8086 Processor to find the
length of a given string which terminates with a special character‘$’.
ALGORITHM:
1. Start the Data segment
2. Initialize the given string in data segment which is ended with special character.
3. Ends the Data segment
4. Start the code segment
5. Initialize START Directive
6. Initialize Data segment
7. Clear the Contents of AX register.
8. Initialize the CX registers as zero.
9. Assign the values to SI register using indexed addressing mode.
10. Move the content of SI to AL register
11. Compare AL with “$”
12. Perform Conditional Operation if JE them jump to Interrupt
13. Else Increment SI & Increment CX
14. Repeat the loop until Compare with $ symbol.
15. Divide the result by the content of BX register.
16. Terminate the program with Interrupt statement
17. Ends the Code segment.
18. End the program.
DATA SEGMENT
Str DB “INDIA$”
DATA ENDS
ASSUME CS: CODE, DS: DATA
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV AX, 0000H
MOV CX, 0000H
MOV SI, OFFSET Str
X: MOV AL, [SI]
CMP AL, ‘$’
JE Y
INC SI
INC CX
JMP X
Y: INT 3H
CODE ENDS
END START
2.8 References :
1. Microprocessors and Interfacing ,Douglas V Hall,T ata Mc Graw Hill
2. http://www.telnet7.net/articles/not_mine/8086_achitecture.htm
3. The 8086/8088 Family, John Uffenbuck, Pearson Media, LPE
2.9 Objective Questions
1. Which of the following is an illegal 8086 instruction?
(a)MOV 20h,BX (b) INC AL (c) AND BX,BX (d) ADD AX,30h
2. Which of the following is an illegal 8086 instruction
(a) MOV AX,[BX] (b) INC [BX] (c) ADD [AX], [BX] (d) ADD AX, [CX]
3..Branch prediction is used in the context of
(a) pipelining (b) program loops (c) cache memory (d) ALU operation
4. A conditional jump instruction
(a) always cause a transfer of control
(b) always involves the use of the status register
(c) always modifies the program counter
(d) always involves testing the Zero flag
5. The advantage of memory mapped I/O over I/O mapped I/O is,
(a) Many instructions supporting memory mapped I/O (b) Faster
(c) Require a bigger address decoder (d) All the above
6. BHE(bar) of 8086 microprocessor signal is used to interface the
(a) Even bank memory (b) Odd bank memory
(c) I/O (d) DMA
7. In 8086 microprocessor one of the following statements is not true.
(a) Coprocessor is interfaced in MAX mode
(b) Coprocessor is interfaced in MIN mode
(c) I/O can be interfaced in MAX / MIN mode
(d) Supports pipelining
8. What is meant by Maskable interrupts?
(a) An interrupt which can never be turned off.
(b) An interrupt that can be turned off by the programmer.
(c)both a& b
(d) none
9. In 8086 the overflow flag is set when
(a) The sum is more than 16 bits
(b) Signed numbers go out of their range after an arithmetic operation
(c) Carry and sign flags are set
(d) During subtraction
10. In 8086 microprocessor the following has the highest priority among all type interrupts.
(a) NMI (b) DIV 0 (c) TYPE 255 (d) OVER FLOW
Answer Key:-
1. A 2 C 3 A 4 A 5 A 6 B 7 B 8 B 9 B 10 A
2.10 Short Questions
1. What are the various interrupts in 8086?
2. What is meant by Maskable interrupts?
03.What is Non-Maskable interrupts?.
04.Which interrupts are generally used for critical events?
05.Give example for Non-Maskable interrupts?
06.What is the difference between microprocessor and microcontroller?
07. What is interrupt?
2.11 Subjective Questions (Based on University Patterns)
1. What is addressing mode? Explain different modes supported by 8086 with examples.
2. Explain memory organization concept of 8086.
3. Explain memory banking concept of 8086?
4. Write a procedure to calculate the factorial of the N using recursive procedure.
5. Differentiate between: procedure and macro.
6. WAP for 8086 in assembly language to check if string initialized in the data segment is
Palindrome or not.
7. Explain with example string instructions.
8. WAP to add 4 digit BCD number in AX to the similar in BX maintaining valid BCD result.
9. What do you mean by assembler directive? Explain any four in detail?
10. Explain the following instructions:
a. XLAT
b. RCL
c. LAHF
d. TEST
e. SAR
f. DAS
g. AAD
h. DAA
i. CLD
j. MOVSB
11. Explain interrupt structure of 8086.
12. What is interrupt? Explain different types of interrupts supported by 8086.
13. Write short notes on I/O mapped I/O and memory mapped I/O
14. Design 8086 based single board microprocessor system with following specification:
a. 8086 CPU working at 5 MHz
b. 64k of EPROM memory using 16K devices for operating software.
c. 256K of SRAM using 64k devices
d. Two input ports and two output ports [all 8-bit], interrupt driven.
Explain the design. Show memory and I/O mapped.
15. Design 8086 based single board microprocessor system with following specification:
A. 8086 CPU working at 5 MHz
B. 64k of EPROM memory using 16K devices
C. 128K of SRAM using 64k devices