Chapter 4 Constructing The Machine Code For 8086 Instruction
Chapter 4 Constructing The Machine Code For 8086 Instruction
1
Outline
Programming Overview
Assembly Language
Instruction Format
The OPCODE , D, W,
MOD, REG , R/M, Displacement
2
4.1 Programming Overview
• System software
• Allows one to develop application programs for MP-based
systems
• Includes:
Editors –create and change source programs
Compilers –translate high-level source code to machine code
Assemblers –translate assembly into binary or obj code
Interpreters – execute one statement at a time
Debuggers – interactive executing & debugging
OS – performs resource mgt and human-machine interaction
3
Program development steps
Defining the problem
Think about the problem that the program has
to solve
Write down operations in general terms
E.g. temperature control problem
Read temperature from sensor
Add correction factor of +7
Save result in a memory location
4
Cont.…
• Representing program operations
• Algorithm – the formula or sequence of operations used to
solve a programming problem
• Two common ways of representing algorithm:
• Flow charts: graphic representation of different
program operations
• Symbols:
DECISION INPUT
PROCESS
OUTPUT START
CONNECTOR
SUB
ROUTINE TERMINATION
5
Cont.…
READ VALUE
FROM SENSOR
ADD 7
STORE RESULT
IN MEMORY
WAIT 1
HOUR
24
SAMPLE
S?
STOP
6
Cont.…
• Pseudocodes
• English-like statements used to represent the commonly
used structures:
• SEQUENCE, IF-THEN-ELSE, WHILE-DO
• Early days approach: structured programming – breaking
problem into independent modules (or reverse)
• E.g IF Temperature is less than 70 degrees THEN
Turn on heater
ELSE
Turn off heater
7
Cont…
• HIGH-LEVEL LANGUAGEs
8
• Learning any imperative programming language involves
mastering a number of common concepts:
• Variables: Declaration/definition
• Assignment: Assigning values to variables
• Input/output: Displaying messages/displaying variable
values
• Control flow: Loops, JUMPs
Programming in assembly language involves mastering the
same concepts and a few other issues.
9
Variables
• We will use the 8086 registers as the variables in our
programs.
10
Assignment
• In programming languages such as C/C++/Java, assignment takes the form:
• x = 42 ;
• y = 24;
• z = x + y;
• In assembly language we carry out the same operation but we use an
instruction to denote the assignment operator (“=”). The above
assignments would be carried out in 8086 assembly language as follows:
• Mov ax, 42
• Add ax, 24
• Mov bx, ax
11
• The mov instruction carries out assignment.
• It allows us to place a number in a register or in a memory
location.
Example:
• mov bx, ‘A’ To store the ASCII code for the letter A in
register bx.
• mov bx, 2 Loads the value 2 in to bx
12
Input/output
• The 8086 provides the instructions IN for input and OUT for
output.
• These instructions are quite complicated to use, so we usually
use the operating system to do I/O for us instead.
• In assembly language we must have a mechanism to call the
operating system to carry out I/O.
• In addition, we must be able to tell the operating system what
kind of I/O operation we wish to carry out, e.g. to read a
character from the keyboard, to display a character or string
on the screen, etc…...
13
• In 8086 assembly language, we do not call operating system
subprograms by name, instead, we use a software interrupt mechanism
• The 8086 INT instruction generates a software interrupt.
• It uses a single operand which is a number indicating which MS-DOS
subprogram is to be invoked.
• For I/O, the number used is 21h. Thus, the instruction INT 21h
transfers control to the operating system, to a subprogram that handles
I/O operations.
• This subprogram handles a variety of I/O operations by calling
appropriate subprograms.
• This means that you must also specify which I/O operation (e.g. read
a character, display a character,…) you wish to carry out. This is done
by placing a specific number in a specific register.
14
• The ah register is used to pass this information.
15
Character Output
There are three elements involved in carrying out this operation using the
INT instruction:
We specify the character to be displayed. This is done by storing the
character’s ASCII code in a specific 8086 register. In this case we use the dl
register, i.e. we use dl to pass a parameter to the output subprogram.
We specify which of MS-DOS’s I/O subprograms we wish to use.
The subprogram to display a character is subprogram number 2h. This
number is stored in the ah register.
We request MS-DOS to carry out the I/O operation using the INT
instruction. This means that we interrupt our program and transfer control
to the MS-DOS subprogram that we have specified using the ah register.
16
• Example : Write a code fragment to display the character ‘a’
on the screen:
mov dl, ‘a’ ; dl = ‘a’
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos, output character
17
Character Input
• There are also three elements involved in performing character
input:
• As for character output, we specify which of MS-DOS’s I/O
subprograms we wish to use, i.e. the character input from the
keyboard subprogram. This is MS-DOS subprogram number
1h. This number must be stored in the ah register.
• We call MS-DOS to carry out the I/O operation using the INT
instruction as for character output.
• The MS-DOS subprogram uses the al register to store the
character it reads from the keyboard.
18
• Example: Write a code fragment to read a character from the
keyboard
mov ah, 1h ; keyboard input subprogram
int 21h ; character input
; character is stored in al
19
• Example: Reading and displaying a character:
20
• Like carrying out an I/O operation, termination of a program is
accomplished by using the INT instruction. This time MS-DOS
subprogram number 4c00h is used and is stored in register AX.
• It is the subprogram to terminate a program and return to MS-DOS.
Hence, the instructions:
mov ax, 4c00h ; Code for return to MS-DOS
int 21H ; Terminates program and return to MS-DOS.
21
Program 1:
• A complete program to display the letter ‘a’ on the screen:
22
23
Program 2:
• Write a program to load character ‘ ? ’ into register ax and
display the same on the screen.
24
Assembly Programming
Language
25
Cont…
• Language levels – there are three:
• MACHINE LANGUAGE:
• Sequence of binary codes
• E.g. 1011 1000 B8
1001 0100 94
0000 0001 01
This is binary code for the 8086 instruction
MOV AX, 0194H
This is very difficult to understand
To make it easier, HEX representation is used
26
Cont…
• ASSEMBLY LANGUAGE
Uses two-, three- or four-letter mnemonics to represent
each instruction type
Mnemonics – abbreviations for instruction opcodes
Special program called assembler is required
Assembler – allows operator to input code program in
mnemonic form
Standard form consists of four fields:
Use assembler
28
Instruction format
†An instruction is a command to the microprocessor to perform a
given task on a specified data.
†1 st
field is called operation code field or op-code indicates the
type of operation to be performed by the CPU.
†2nd
field is called operand field indicates data field on which the
operation by instruction op-code.
Op-code D W
Op-code field indicates the operation to be carried out
Op-Code W
2nd Byte
1 1 REG R/M
38
Example
E.g. Machine Code for MOV CH, BL This instruction
transfers 8 bit content of BL into CH
The 6 bit Opcode for this instruction is 1000102 D bit
indicates whether the register specified by the REG field
of byte 2 is a source or destination operand.
D=0 indicates BL is a source operand.
W=0 byte operation
In byte 2, since the second operand is a register MOD field
is 112.
The R/M field = 101 (CH)
Register (REG) field = 011 (BL)
Hence the machine code for MOV CH, BL is
10001000 11 011 101
Byte 1 Byte2
= 88 DDH
Register to/ from memory with no displacement
• This format is also 2 byte long
• 1st byte of code same as that of register to register format
• 2nd byte of code consist of MOD , REG,R/M field.
• MOD indicates the displacement is present or not .if present then
it is 8 bit or 16 bit.
Mod Displacement
0 0 No displacement