0% found this document useful (0 votes)
18 views12 pages

Coal 8

The document discusses how to manually generate machine code for assembly language instructions. It explains the format of the 1st and 2nd byte of machine code instructions, and provides examples of finding the machine code for MOV instructions considering different registers.

Uploaded by

veermiracle681
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views12 pages

Coal 8

The document discusses how to manually generate machine code for assembly language instructions. It explains the format of the 1st and 2nd byte of machine code instructions, and provides examples of finding the machine code for MOV instructions considering different registers.

Uploaded by

veermiracle681
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

H. M.

Faisal (03455275108) Computer Organization and Assembly Language (CS-530)

Lesson Plan #

Objectives:The main objectives are: (a)Machine code.

Content: In this lecture we are going to discuss that how machine codes are generated. When we write
and an instruction in assembly, then its equivalent machine code is generated by emu 8086. So,
our concern is to know that how can we generate or find the machine code of an assembly
instruction manually.

Methods: For every new topic, I will first provide the definition and the basic working, then I will request
some student to come to the board and with the help of class discussion repeat that discussion. This
process will not only give them confidence, but will also clear their idea and encourage them to
openly discuss any complications with this topic.

Resources: Besides the lecture handout, this lesson will draw from the following Text books: Assembly
Language Programming and Organization of the IBM-PC, Ytha Yu and Charles Marut.

Evaluation: In this lecture I will not be using any formal evaluation process. I will be using informal
techniques to monitor if the students have absorbed the material or not: (i) By making the students
step up to the board and solve some examples. (iii) By judging the class interaction, which is a
clear indicator of their interest.

Assignment: Give them a home assignment on programming practices.


Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
15 mins Introduction
In this lecture we are going to discuss that how machine codes are generated. When we write and
an instruction in assembly, then its equivalent machine code is generated by emu 8086. So, our
concern is to know that how can we generate or find the machine code of an assembly instruction
manually.
So, let’s try to understand the whole procedure to find the machine code.
Machine code
Machine code of each instruction varies in number of bytes.Maximum size of a machine code or an
instruction can be 6 bytes. Each byte has its own format to consider when we are generating
machine code. Here keep in mind that the machine code of each instruction depends on the
considered memory or register through which we are generating machine code.

MOV BL, AL
For example, in above instruction, the machine code is 8AD8H if we consider BL register and
88C3H if we consider AL register. Here don’t worry about how the above codes are generated, the
main thing for you to pay the attention is that the machine code of each instruction depends on
the considered memory or register through which we are generating machine code.

So, now we understand the format of each byte starting from first byte.

First Byte Format


First byte contains following information:
1. What is opcode (i.e., operation code).
2. Whether the considered register or memory is destination or source.
3. Whether the considered register/memory is 8-bit or 16-bit.

Here,
• W = 1 if considered register is 16 bit and 0 otherwise.
• D = 1 if considered register is destination and 0 otherwise.
• Opcode depends on operation and you can find it on internet (i.e., MOV, ADD, SUB etc.).

Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
5 Example regarding first byte
mins
MOV AX, BX

Here we have to find the machine code of above instruction and keep in mind that we are going to
consider AX register to generate code.

Here we have to write values for each portion of first byte. So,
OPCODE = 100010 (Operation code for MOV instruction is 100010)
D = 1 (because AX is destination)
W= 1 (because AX is 16 bit register)

So machine code for first byte will be as follow:


OPCODED W = 1000101 1 = 1000 1011 = 8BH

Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
15 mins Second byte format
Second byte contains following information:
1. What is the second operand in instruction, that is, whether it is memory or
register?
2. What is the name (binary) of first considered register.
3. What is the name (binary) of second operand (operand of step 1).

Here MOD means what is the second operand in instruction, REG means what is the name (binary)
of first considered register, and R/M means what is the name (binary) of second operand (operand
of step 1).
Values for all these portions will be found using following tables:

Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
5 mins
Example regarding second byte
MOV AX, BX

Here we again consider the previous instruction as an example in which we have found machine
code for first byte.
Here,
MOD = 11 (because second operand, which is BX, is register with no displacement)
REG = 000 (because considered register is AX and value for AX register in table is 000)
R/M = 011 (because second operand is BX and its value in table is 011)

So here machine code for second byte will be written just like first byte:
MOD REG R/M = 11 000 011 = 1100 0011 = C3

So, we will get complete code for instruction MOV AX, BX after combining code of both first and
second byte because it was 2 Byte instruction. i.e.,
Complete code = 8BC3h (2 byte instruction)

Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
5 Example 1
mins MOV DL, BH
Here we are considering DL register.
For first byte:

OPCODE = 100010 (Operation code for MOV instruction is 100010)


D = 1 (because DL is destination)
W= 0 (because DL is 8 bit register)

First byte code = 100010 1 0 = 1000 1010 = 8A


For second byte:

MOD = 11 (because second operand, which is BH, is register with no displacement)


REG = 010 (because considered register is DL and value for DL register in table is 010)
R/M = 111 (because second operand is BH and its value in table is 111)

Second byte code = 11 010 111 = 1101 0111 = D7

Complete code = 8AD7h (2byte instruction)

Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
5 mins
Example 2
MOV BL, AL

Here we will consider AL register.


For first byte:

OPCODE = 100010 (Operation code for MOV instruction is 100010)


D = 0(because AL is source)
W= 0 (because AL is 8 bit register)
First byte code = 100010 0 0 = 1000 1000 = 88

For second byte:

MOD = 11 (because second operand, which is BL, is register with no displacement)


REG = 000 (because considered register is AL and value for AL register in table is 000)
R/M = 011 (because second operand is BL and its value in table is 011)

Second byte code = 11 000 011 = 1100 0011 = C3

Complete code = 88C3h ( 2byte instruction)

Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
5 mins Example 3
MOV AL, [DI]
Here we will consider AL register.
For first byte:

OPCODE = 100010 (Operation code for MOV instruction is 100010)


D = 1(because AL is destination)
W= 0 (because AL is 8 bit register)

First byte code = 100010 1 0 = 1000 1010 = 8A

For second byte:


MOD = 00 (because second operand, which is [DI], is memory with no displacement and its value is
table is 00)
REG = 000 (because considered register is AL and value for AL register in table is 000)
R/M = 101 (because second operand is [DI] and its value in above table is 101)
Second byte code = 00 000 101 = 0000 0101 = 05

Complete code = 8A05h ( 2byte instruction)

Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
15 mins Complete 6 bytes format

Here is Byte 3 we will have to write Low displacement data and in Byte 4 we will have to write High
displacement data. For example in ADD [BX][DI]+1234H, AX instruction, we will have to write 34 in
Byte 3 and 12 in Byte 4.
Let’s understand this with an example.
Example 4
ADD [BX][DI]+1234H, AX
Here we are considering AX register.
For first byte:
OPCODE = 000000 (Operation code for ADD instruction is 000000)
D = 0(because AX is source)
W= 1 (because AX is 16 bit register)

First byte code = 000000 0 1 = 0000 0001 = 01


For second byte:

MOD = 10 (because second operand, which is [BX][DI]+1234H, is memory with 16 bit displacement
and its value in table is 10)
REG = 000 (because considered register is AX and value for AX register in table is 000)
R/M = 001 (because second operand is [BX][DI]+1234H and its value in above table is 001)

Second byte code = 10 000 001 = 1000 0001 = 81

For 3rd and 4th byte:

Here Byte3 = 34 and Byte4= 12.


You can easily find which data should be in byte3 and byte4 by considering following structure of a
register.
If we write 1234 in BX then

So Low disp data should be 34 and high disp data should be 12.

Now we will get complete code by combining codes of all these bytes.

Complete code = 01 81 34 12 H( 4 bytes instruction)

Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
16
mins
Immediate Addressing
In previous topic, we were only using register or memories with different instructions. But now we
are going to use direct values or you can say immediate addressing. For example we were dealing
with instructions like MOV AX, BX but now we are going to discuss instructions like MOV AX, 1. So
here you can see that we are now moving direct value into AX which was not the case in previous
examples.
So, let us understand how machine codes are generated in immediate addressing.
Here you have to keep in mind that when you are dealing with immediate addressing, then the
format of bytes and operation code change depending on the operation being performed and you
can easily search about these formats and operation code on the internet. Because now you have
enough idea about how machine codes are generated, so I hope now you can generate machine
codes by just understanding the format of bytes.

For example the formats of bytes for MOV instruction are given below.
2 byte instruction format:

Here first byte is in orange color and second byte is in blue color
• 4 bits Opcode depends on operation and you can find it on internet (i.e., MOV, ADD, SUB
etc.)
• W = 1 if register is 16 bit and 0 otherwise.
• 3 bits Reg is binary of register which can be found using below table.
• Here in second byte data is the actual value which is being moved in the register.

3 byte instruction format:

Here first byte is in orange color and 2nd and 3rd bytes are in blue color
 Rest of the points are same as 2 byte format. Just difference is that if value consists
of 4 characters then low byte data and high byte data will be placed in their
respective bytes just like as we did in previous example.
Here again I repeat that the above formats are just for MOV instruction/operation and these
formats may differ in case of ADD, SUB etc. operations.

Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
4 Example 1
mins
MOV AH, 1
Here we will consider register to generate code.

 Opcode = 1011 (operation code for MOV instruction is 1011 in case of immediate
addressing).
 W = 0 because AH is 8-bit register.
 Reg = 100 for AH register in above table.
 Data = 01 (it is the actual value which is being moved i.e, 1 in MOV AH, 1)

First byte code = 1011 0100 = B4


Second byte code = 01

Complete code = B401h(2 byte instruction)

Example 2
MOV AX, 1234h
 Opcode = 1011 (operation code for MOV instruction is 1011 in case of immediate
addressing).
 W = 1 because AX is 16-bit register.
 Reg = 000 for AX register in above table.
 Low byte = 34 and High byte = 12 because:

First byte code = 1011 1000 = B8


Second byte code = 34
Third byte code = 12

Complete code = B8 34 12h(3 byte instruction)

You might also like