0% found this document useful (0 votes)
188 views

Chapter 4 Constructing The Machine Code For 8086 Instruction

Uploaded by

temesgen adugna
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
188 views

Chapter 4 Constructing The Machine Code For 8086 Instruction

Uploaded by

temesgen adugna
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 50

Chapter Four

Constructing the Machine Code


for
8086 Instructions Format

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.…

• Example flowchart START

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

Use more English like statements, which may represent


many machine code instructions

Interpreters or compilers – convert these into machine


codes

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.

• Registers have predefined names and do not need to


be declared.

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.

• For example, the subprogram to display a character is


subprogram number 2h.

• This number must be stored in the ah register.

• When the I/O operation is finished, the interrupt service


program terminates and our program will be resumed.

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:

mov ah, 1h ; keyboard input subprogram

int 21h ; read character into al

mov dl, al ; copy character to dl

mov ah, 2h ; character output subprogram

int 21h ; display character in dl

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.

It is also possible to use as follows;


mov ah, 4ch
int 21H
• It is must to terminate a program code, otherwise the program may
crash

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:

LABEL OP CODE OPERAND COMMENT FIELD


FIELD FIELD FIELD
NEXT: ADD AL, 07H ;ADD CORRECTION
FACTOR
27
Cont..
Label: represents an address which is not specifically known at the time
the statement is written
Opcode: mnemonic for the instruction to be performed

Operand: name for data items to be acted up on by an instruction

Comment: reminds the function that an instruction or group of


instructions performs in the program

To convert assembly to machine:


Manual: e.g. MOV AX, 0194  B8 94 01  BINARY

Use assembler

28
Instruction format
†An instruction is a command to the microprocessor to perform a
given task on a specified data.

†Each instruction has two parts: one is task to be performed, called


the operation code (opcode), and the second is the data to be
operated on, called the operand.

†An instruction format defines layout of bits of an instruction, in terms


of its constituent parts.

†An instruction format must include an opcode and implicitly or


explicitly, zero or more operands.

†Each explicit operand is referenced using one of addressing modes.


Cont...
†A machine language instruction format has more than one number
of field

†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.

†It has 6 general format of instruction in 8086 instruction set.


†The 8086 Instruction Format vary from 1 to 6 bytes in length
Cont...
†A machine language instruction format has more than one
number of field

† 1st field is called operation code field or op-code which


indicates the type of operation to be performed by the CPU.

†2nd field is called operand field (Data field) that indicates


data field on which the operation by instruction op-code.
Cont...
†There are six general formats of instructions in 8086
instruction set.

One byte Instruction


Register to Register
Register to/from memory with no displacement
Register to/from Memory with Displacement
Immediate operand to register
immediate operand to memory with 16-bit displacement
OP-CODE BYTE:
This byte is always present in each instruction .
This indicates the operation to be carried out by 8086
 Format of op-code byte:

Op-code D W
 Op-code field indicates the operation to be carried out

 D  D bit indicates whether the register is a source /destination


register.

 D=0 indicates that the register is source register.

 D=1 indicates that the register the destination register.

 W  word or byte:- W bit indicates whether the instruction is a byte


or word instruction.

 W=0 indicates instruction that operates on bytes 8 bits.

 W=1 indicates instruction that operates on words 16 bits.


One byte instruction
• This format is only 1 byte long and may have implicit data or register
operands.
• The least 3 bits of op-code are used to specify the register operand.
• Otherwise all the 8 bit form an op-code and the operand are implied.
For example:
• CLC : clear carry 1 1 1 1 1 0 0 0 F8H
This is an operation without any operand, which clear the carry flag bit.
• Adjust After addition AAA 00110111 37H
Here the operand to this instruction is implicit and it take the contents of
register AL.
Register to Register
• This format is 2 byte long
• 1st byte of code consist of operation code of instruction and width of
the operand specified by w bit.
• 2nd byte of code consist of register & R/M field.
• REG indicates the name of the register i.e source or destination.
• R/M indicates source or destination operand is located in register
1st Byte
6 bit 1 bit

Op-Code W

2nd Byte

1 bit 1 bit 3 bit 3 bit

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

0 1 Low order displacement


with sign extended to 16
bit
1 0 Low order and high order
displacement
1 1 r/m field is treated as a
‘reg’ field
R/M MOD=00 MOD =01 MOD =10
000 (BX) + (SI) (BX)+(SI)+D8 (BX)+(SI)+D16

001 (BX)+(DI) (BX)+(DI)+D8 (BX)+(DI)+D16

010 (BP)+(SI) (BP)+(SI)+D8 (BP)+(SI)+D16

011 (BP)+(DI) (BP)+(DI)+D8 (BP)+(DI)+D10


100 (SI) (SI) + D8 (SI) + D16

101 (DI) (DI) + D8 (DI) + D16

110 Direct address (BP) + D8 (BP) + D16

111 (BX) (BX) + D8 (BX) + D16


Example
Machine Code for SUB BX, (DI)
This instruction subtracts the 16 bit content of memory
location addressed by DI and DS from Bx.
The 6 bit Opcode for SUB is 0010102.
D=1 so that REG field of byte 2 is the destination operand.
W=1 indicates 16 bit operation.
MOD = 00
REG BX = 011
R/M (DI) = 101
The machine code is 0010 1011 0001 1101
2 B 1 D
Register to/from memory with displacement
• This type of instruction format contain one or two additional bytes for
displacement along with 2 byte format of register to/from memory
without displacement
Memory Mode (EA Register
MOD / R/M Calculation) Mode
00 01 10 W=0 W=1
000 (BX)+(SI) (BX)+(SI)+d8 (BX)+(SI)+d16 AL AX
001 (BX) + (DI) (BX)+(DI)+d8 (BX)+(DI)+d16 CL CX
010 (BP)+(SI) (BP)+(SI)+d8 (BP)+(SI)+d16 DL DX
011 (BP)+(DI) (BP)+(DI)+d8 (BP)+(DI)+d16 BL BX
100 (SI) (SI) + d8 (SI) + d16 AH SP
101 (DI) (DI) + d8 (DI) + d16 CH BP
110 d16 (BP) + d8 (BP) + d16 DH SI
111 (BX) (BX) + d8 (BX) + d16 BH DI
Example
Code for MOV 1234 (BP), DX
Code for MOV instruction is 100010
DX using REG field, the D bit must be 0, The REG field must be
010 to indicate DX register.
The W bit must be 1 to indicate it is a word operation.
1234 [BP] is specified using MOD value of 10 and R/M value of
110
Displacement of 1234H.
The 4 byte code for this instruction would be 89 96 34 12H.
Opcode D W MOD REG R/M LB displacement HB displacement

100010 0 1 10 010 110 34H 12H


Immediate operand to register
• In this format first byte as well as the 3 bits from the second byte which are
used for REG field in case of register –to-register are used for op-code.
• It also contains one or two bytes of immediate data
Immediate operand to memory with 16 bit
displacement
This type of instruction format requires 5 or 6 byte for coding

The first two byte contain the information of


OPCODE ,MOD ,R/M field

The remaining 4 bytes contain 2 bytes of displacement and 2


bytes of data
Cont.….
ND
E 50

You might also like