ML 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

University of Technology

Department of Communications Engineering


Optical Communication Systems Engineering Branch

8086 Emulator

Experiment No.1
Microprocessor Laboratory

Mousa Saad Luaibi


Mohammed Ahmed Jassim

Third Stage

Morning Study

Group (B)

24th October 2021

31st October 2021


Microprocessor Laboratory Experiment No.1

1.1 Introduction
This program is extremely helpful for those who just begin to study assembly
language. It compiles the source code and executes it on emulator step by step. Visual
interface is very easy to work with. You can watch registers, flags and memory while
your program executes.

Arithmetic & Logical Unit (ALU) shows the internal work of the central processor unit
(CPU). Emulator runs programs on a virtual PC, this completely blocks your program
from accessing real hardware, such as hard-drives and memory, since your assembly
code runs on a virtual machine, this makes debugging much easier. 8086 machine code
is fully compatible with all next generations of Intel's microprocessors, including
Pentium II and Pentium 4.

1.2 Inside the CPU


1.2.1 General Purpose Registers
8086 CPU has 8 general purpose registers, each register has its own name:

Figure (1) 8086 CPU

➢ AX - the accumulator register (divided into AH / AL).


➢ BX - the base address register (divided into BH/BL).
➢ CX - the count register (divided into CH /CL).
➢ DX - the data register (divided into DH / DL).
➢ SI - source index register.
➢ DI - destination index register.
➢ BP - base pointer.
➢ SP - stack pointer.
Microprocessor Laboratory Experiment No.1

Despite the name of a register, it's the programmer who determines the usage
for each general-purpose register. The main purpose of a register is to keep a
number (variable). The size of the above registers is 16 bit, it's something like:
0011000000111001b (in binary form), or 12345 in decimal (human) form. 4
general purpose registers (AX, BX, CX, DX) are made of two separates 8-bit
registers, for example if AX= 0011000000111001b, then AH=00110000b and
AL=00111001b. Therefore, when you modify any of the 8-bit registers 16-bit
register is also updated, and vice-versa. the same is for other 3 registers, "H" is
for high and "L" is for low part. Because registers are located inside the CPU, they
are much faster than memory. Accessing a memory location requires the use of
a system bus, so it takes much longer. Accessing data in a register usually takes
no time. therefore, you should try to keep variables in the registers. register sets
are very small and most registers have special purposes which limit their use as
variables, but they are still an excellent place to store temporary data of
calculations.

1.2.2 Segment registers


➢ CS - points at the segment containing the current program.
➢ DS - generally points at segment where variables are defined.
➢ ES - extra segment register, it's up to a coder to define its usage.
➢ SS - points at the segment containing the stack.

Although it is possible to store any data in the segment registers, this is never a
good idea. the segment registers have a very special purpose - pointing at
accessible blocks of memory. Segment registers work together with general
purpose register to access any memory value. For example, if we would like to
access memory at the physical address 12345H (hexadecimal), we should set the
DS = 1230H and SI = 0045H. This is good, since this way we can access much more
memory than with a single register that is limited to 16-bit values. CPU makes a
calculation of physical address by multiplying the segment register by 10h and
adding general purpose register to it (1230h * 10H + 45H = 12345H):

The address formed with 2 registers is called an effective address. By default,


BX, SI and DI registers work with DS segment register; BP and SP work with SS
segment register. Other general-purpose registers cannot form an effective
address! also, although BX can form an effective address, BH and BL cannot!
Microprocessor Laboratory Experiment No.1

1.2.3 Special Purpose Registers


➢ IP - the instruction pointer.
➢ Flags register - determines the current state of the microprocessor.

IP register always works together with CS segment register and it points to


currently executing instruction.

Flags register (processor status register) is modified automatically by CPU after


mathematical operations, this allows to determine the type of the result, and to
determine conditions to transfer control to other parts of the program. There
are 16-bits in this register, each bit is called a flag and can take a value of 1 or 0.

➢ Carry Flag (CF) - this 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.
➢ Zero Flag (ZF) - set to 1 when result is zero. For none zero result this flag is
set to 0.
➢ Sign Flag (SF) - set to 1 when result is negative. When result is positive it is
set to 0. Actually, this flag take the value of the most significant bit.
➢ Overflow Flag (OF) - set to 1 when there is a signed overflow. For example,
when you add bytes 100 + 50 (result is not in range -128...127).
➢ Parity Flag (PF) - this 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. Even if result is a word
only 8 low bits are analyzed!
➢ Auxiliary Flag (AF) - set to 1 when there is an unsigned overflow for low
nibble (4 bits).
➢ Interrupt enable Flag (IF) - when this flag is set to 1 CPU reacts to interrupts
from external devices.
Microprocessor Laboratory Experiment No.1

➢ Direction Flag (DF) - this flag is used by some instructions to process data
chains, when this flag is set to 0 - the processing is done forward, when this
flag is set to 1 the processing is done backward.
Generally, you cannot access these registers directly.

1.3 Memory Access


8086 CPU can access up to 1 MB of random-access memory (RAM), it is limited by
segment/offset construction. Since segment registers (CS, SS, ES, DS) can hold
maximum value of 0FFFFH and offset registers (IP, BX, SI, DI, BP, SP) can also hold
maximum value of 0FFFFH, the largest logical memory location that we can access is
FFFF:FFFF or physical address: 0FFFFH * 10H + 0FFFFH = 10FFEFH = 65535 * 16 + 65535
= 1,114,095 bytes.
Modern processors have a larger register so they have much larger memory area that
can be accessed, but the idea is still the same. To access memory, we can use these four
registers: BX, SI, DI, BP. Combining these registers inside [ ] symbols, we can get
different memory locations.

These combinations are supported (addressing modes):

[BX + SI] [SI] [BX + SI] + d8


[BX + DI] [DI] [BX + DI] + d8
[BX + SI] d16 (variable offset only) [BP + SI] + d8
[BX + DI] [BX] [BP + DI] + d8

[SI] + d8 [BX + SI] + d16 [SI] + d16


[DI] + d8 [BX + DI] + d16 [DI] + d16
[BP] + d8 [BP + SI] + d16 [BP] + d16
[BX] + d8 [BP + DI] + d16 [BX] + d16

d8 - stays for 8-bit displacement.


d16 - stays for 16-bit displacement.
Displacement can be a immediate value or offset of a variable, or even both. It's up to
compiler to calculate a single immediate value. Displacement can be inside or outside
of [ ] symbols, compiler generates the same machine code for both ways. Displacement
is a signed value, so it can be both positive or negative. Generally, the compiler takes
care about difference between d8 and d16, and generates the required machine code.
Microprocessor Laboratory Experiment No.1

For example, let's assume that DS = 100, BX = 30, SI = 70.


The following addressing mode: [BX + SI] + 25, is calculated by processor to this physical
address: 100 * 16 + 30 + 70 + 25 = 1725.

By default, DS segment register is used for all modes except those with BP register, for
this SS segment register is used. There is an easy way to remember all those possible
combinations using this chart:

You can form all valid combinations by taking only one item from each column or
skipping the column by not taking anything from it. As you see BX and BP never go
together. SI and DI also don't go together. Here is an example of a valid addressing
mode: [BX+5]. The value in segment register (CS, DS, SS, ES) is called a "segment", and
the value in purpose register (BX, SI, DI, BP) is called an "offset". When DS contains
value 1234H and SI contains the value 7890H it can be also recorded as 1234:7890. The
physical address will be 1234H * 10H + 7890H = 19BD0H.

1.4 Where to start?


1. Start Emu8086 by selecting its icon from the start menu, or by running
Emu8086.exe.
2. Select "Samples" from "File" menu.
3. Click [Compile and Emulate] button (or press F5 hot key).
4. Click [Single Step] button (or press F8 hot key), and watch how the code is being
executed.
5. Try opening other samples, all samples are heavily commented, so it's a great
learning tool.
Microprocessor Laboratory Experiment No.1

1.4.1 Using Emulator

[Single Step] button executes instructions one by one stopping after each
instruction.
(Run] button executes instructions one by one with delay set by step delay
between instructions.
Double click on register text-boxes opens "Extended Viewer" window with value
of that register converted to all possible forms. You can modify the value of the
register directly in this window.
Double click on memory list item opens "Extended Viewer" with WORD value
loaded from memory list at selected location. Less significant byte is at lower
address: LOW BYTE is loaded from selected position and HIGH BYTE from next
memory address. You can modify the value of the memory word directly in the
"Extended Viewer" window, you can modify the values of registers on runtime
by typing over the existing values.
Microprocessor Laboratory Experiment No.1

(Flags] button allows you to view and modify flags on runtime.

1.4.2 Compiling Assembly Code


Type your code inside the text area, and click [Compile] button. You can be asked
for place where to save the compiled file. After successful compilation you can
click [Emulate] button to load the complied file in emulator.

1.4.3 Error Processing


Compiler reports about errors in a separate information window

MOV DS, 100 - is illegal instruction because segment registers cannot be set
directly, general purpose register should be used:
MOV AX, 100
MOV DS, AX
MOV AL, 300 - is illegal instruction because AL register has only 8 bits, and thus
maximum value for it is 255 (or 11111111b), and the minimum is 128.
Microprocessor Laboratory Experiment No.1

1.5 Procedure
For each of the following exercises:
1. Write down the following program in the text editor and make # COM file.
2. Compile & emulate the program to its Hex code.
3. Compare between the assembled and disassembled programs.
4. Write down the machine instructions (Hex code) of the program
5. Use the single step execution command to run the program.
6. Verify the contents of the registers used in each instruction used in this program
including the IP in each step of the execution.

1.6 Exercises
1. Execute the following program and write the value of AX,BX,CX.
MOV AX,552BH
MOV BX,8922
MOV CX,AX
HLT
❖ Solution:
AX value BX value CX value
Instruction IP value
AH AL BH BL CH CL
MOV AX,552BH 55 2B 00 00 00 00 0003
MOV BX,8922 55 2B 22 DA 00 00 0006
MOV CX,AX 55 2B 22 DA 55 2B 0008

2. Execute the following programs and list the generated errors.


➢ MOV DS,B822
MOV BL,256
MOV CX,BL
HLT
❖ Solution:
Instruction True / False The reason The correction
1. Cannot use segment register with an
MOV DS,B822 False immediate value. MOV AX,0B822H
2. The immediate value neither decimal MOV DS,AX
nor hexadecimal.
As the first operand is 8-bits and the
MOV BL,256 False second operand is more than 8-bits, so it is MOV BX,256
not possible to store and copy data
between different sizes.
MOV CX,BL True ‫ـــــ‬ ‫ـــــ‬
Microprocessor Laboratory Experiment No.1

➢ MOV AX,01011111
MOV CH,AX
MOV DX,80
MOV 200H,CX
HLT
❖ Solution:

Instruction True / False The reason The correction


The letter “B” must be placed
after the immediate value in order
for it to be considered a binary
number and to the copying
process will take place on it,
MOV AX,01011111 False because if the letter “B” is not MOV AX,01011111B
placed, it will the immediate value
consider is decimal and cannot be
transferred because “AX” is 16-
bits and the immediate value is
more than 16-bits
The AX register is a 16-bit register
MOV CH,AX False whereas CH register is an 8-bit MOV CX,AX
register, thus the instruction is
wrong because of size mismatch
MOV DX,80 True ‫ـــــ‬ ‫ـــــ‬
Not allowed copy the register to
MOV 200H,CX False MOV [0200H],CX
the immediate value

1.7 Discussion
1. a) What is the function of an assembler and compiler?
❖ Assembler, it translates a medium-level or assembly language (what humans
could write in simple shape) to machine language (a pattern of bits that what
processor works), which means it allows the programmer to use mnemonic codes
such as ADD for addition in place of binary number such as 01000111.

❖ Compiler, it translates source code written in a high-level language (what humans


could write in advanced shape, e.g., C++) into a set of machine-language
instructions that can be understood by CPU.
Microprocessor Laboratory Experiment No.1

b) What is the object code? What is the source code?


❖ Object code, it a set of instructions written in the machine language that the
microprocessor operates in and understood by "binary bits".
❖ Source code, it a set of instructions written by a human in a high-level language
(for example, C++) and then converted by the compiler into object code to be
executed by a computer.

2. What is the difference between the physical and the logical address?

Subject The logical address The physical address


Generated by the CPU
The location in a memory
Generation while the program is
unit
running
Visibility The user can view it The user can never view it
It uses to access the The user can not directly
Access
physical address access it
It imaginary and don't
exist physically in the
It real and exist in the
Existence memory, where
memory unit
sometimes called virtual
address
Syntax SBA : Offset Address SBA * 10 + Offset Address
Example 5D27H : 2C30H PA=SS*10+2C30H=5FEA0H

3. Calculate the physical memory location for each of the following cases?
a- The logical address D470H in the extra segment.
b- The logical address 2D90H in the stack segment.
C- MOV [BP],AL if BP=2C30H.
Assume ES=52B9, SS=5D27, DS=E000, and CS=B3FF.
Microprocessor Laboratory Experiment No.1

Logical Address = Segment Base Address : Offset Address


Physical Address = Segment Base Address * 10 + Offset Address

Logical Address Physical Address


52B9H : D470H PA = ES * 10 + D470H = 60000H
5D27H : 2D90H PA = SS * 10 + 2D90H = 60000H
5D27H : 2C30H PA = SS * 10 + 2C30H = 5FEA0H

4. Which segment will be accessed for the instruction MOV [BX],AH.


❖ The above instruction is addressed with the (Register Indirect Addressing Mode)
method, and since modes [BX],[BP],[SI],[DI] use the data segment (DS) by default,
the segment that will be accessed and the data is store in it, the data segment
(DS).

5. Write the addressing modes of 8086 assembly language program.

Addressing Modes Syntax Example


Register Addressing AX,BX,CX,DX MOV CX,DX
Immediate Addressing ‘A’,55,0101B,22H MOV BX,5555H
Memory Operand SBA : Base + Index +
MOV AX,DS:[BX]+[SI]+[3421H]
Addressing Modes Displacement
Direct Addressing Mode SBA : Direct Address MOV BX,DS:[1134H]
Register Indirect
SBA : [BX,BP,SI,DI] MOV DX,DS:[SI]
Addressing Mode
Based Addressing Mode SBA : [BP,BX] + Displacement MOV CX,SS:[BP]+[6234H]
Indexed Addressing SBA : Index Register +
MOV AX,DS:[SI]+[2534H]
Mode Displacement
Based-Indexed SBA : [BX,BP] + Index Register +
MOV AX,DS:[BX]+[SI]+[1234H]
Addressing Mode Displacement

6. Name Five 16-bit members of 80x86 Family architecture.


❖ Accumulator Register (AX)
❖ Base Register (BX)
❖ Count Register (CX)
❖ Data Register (DX)
❖ Source Index Register (SI)

You might also like