0% found this document useful (0 votes)
8 views26 pages

Assembly Programming with DEBUG Guide

This document presents the NASM assembler programming tool and its development environment NASMIDE. It describes the basic structure of an assembler program and provides instructions for editing such a program with NASMIDE.

Translated by

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

Assembly Programming with DEBUG Guide

This document presents the NASM assembler programming tool and its development environment NASMIDE. It describes the basic structure of an assembler program and provides instructions for editing such a program with NASMIDE.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MedAli KAMMOUN Assembly programming with DEBUG

TP No. 1: Assembly Programming with


DEBUG
I. Purpose of the TP

Familiarize yourself with assembly language on PC using DEBUG.

II. Presentation of DEBUG

II.1. Assembler on PC with DEBUG


The Pentium is the successor to the 80486 from the Intel family. It is oriented towards RISC architecture techniques.
(Reduced Instruction Set Computer). It adopts simple instructions wired on silicon, an architecture
superscalar with two pipelines, it operates by applying the concept of predictive branching. It calls upon
specialized caches for instructions and data. But it is still a CISC (Complex Instruction) processor.
Set Computer.
In this lab, we will focus on the user of DEBUG, a tool that allows for close monitoring.
the execution of a program, and to better understand the architecture and instruction set of the Pentium.

II.2. Pentium Instruction Set


The instruction set of the Pentium derives from its predecessors 40486 and 80386. This allows keeping the
compatibility of systems. Below we list the most commonly used instructions:

Code Function Example meaning


Transfer instructions
MOV Movement MOV AH,09 AH :=09
operand
PUSH Loading in the PUSH AX Charger AX in the stack
pile
POP Battery extraction POP BX Charge the top of the
stake in BX
Arithmetic instructions
ADD Addition ADD AX,3 AX := AX + 3
INC Increment by 1 INC AX AX := AX + 1
SUB Subtraction SUB CX,5 CX :=CX-5
DEC Decrement by 1 DEC AX AX := AX - 1
CMP Comparison Compare CX with 3 Compare CX with 3
MUL Multiplication MUL AX,7
DIV Division DIV AX, 9
Logical instructions
AND ET logic AND AX, BX AX=AX and BX
OR OR logic OR AX, BX AX = AX or BX
XOR Exclusive OU XOR AX,BX AX = AX xor BX
NOT Logical inversion NOT AX AX=not AX
Wiring instructions
conditional
I Jump if equal I label
JNE Jump if not equal JNE label

ISET Mahdia Industrial Computing 3thlevel 2


MedAli KAMMOUN Assembly programming with DEBUG

Code Function Example meaning


Unconditional connection
and call of procedures
JMP Connection JMP label IP := label if
unconditional short circuit
LOOP Loop LOOP
label
CALL Call for procedures CALL proc IP := procedure address
RET Return of procedure RET IP := return address
Instructions
inputs/outputs
IN Operator entry
OUT Operator exit
Instructions for addresses
LEA Load the address AX := address of the label
effective

III. Use of DEBUG


DEBUG is called to trace the execution of a program already edited in a file or to edit a
new program. To call Debug, go to DOS and type:

C:\>DEBUG
-
Any loaded program is placed in memory in an appropriate segment. A displacement of 100H relative to
At the beginning of the segment, specify the starting address. It must be mentioned in the Debug.

C:\>DEBUG
-A 100

Debug selects an address for your program. It displays it and adds your offset.
Q1: What is the segment address chosen by your Debug?

For a segment address of 21CB and an offset of 100, the actual address of the first instruction of your
program is equal to (21CB0H + 100H).

Q2: What is the starting address of the program chosen by your Debug?

To edit a program, proceed as follows:

C:\>DEBUG
-A 100
21CB :0100 MOV DL,3F
21CB:0102 MOV AH,2
21CB:0104 INT 21H
21CB:0106 INT 20H
21CB:0108

The two instructions 'MOV AH,2' and 'INT 21H' allow the display of the character whose ASCII code is
stored in DL. To execute the program we do:

-G
?
Normal end of the program
-

Q3: Write a program that displays the character A.

ISET Mahdia Industrial Computing 3thlevel 3


MedAli KAMMOUN Assembly programming with DEBUG

To know the content of a memory area, we use the U (Unassembler) command.

- U 100 107
21CB :0100 B23F MOV DL,3F
21CB :0102 B402 MOV AH,2
21CB:0104 CD21 INT 21H
21CB:0106 CD20 INT 20H

Q4 : Analyser et décoder les instructions MOV et INT.

To save the work done in a file [Link], we use the following commands:
-N [Link]
-R CX
CX 000
:8
-R BX
BX 000
:
-W
-Q
C:\>

The two registers CX and BX indicate the size of the program to be saved. In this case, it is 8 bytes.
starting from the departure address (100).

To run the program, you must type the name of the file:

C:\>TP1
?
C:\>

IV. Exercises
1. Write a program '[Link]' that displays all lowercase letters.
2. Write a program "[Link]" that displays all uppercase letters.
3. Write a program "[Link]" that displays all the digits from 0 to 9.
4. Disassemble the program "[Link]".
5. Display the status of the registers using the command
-R
6. Execute the program step by step using the command
-T
7. Restart the execution of the program using the command
- G 100
8. Launch the execution of a portion of the program using
-G=100 106
9. Perform a memory dump using the command
-D 100
10. Modify the memory address case "xxxx:0101"
-E 101
xxxx :0101 xx.41
11. Make a memory dump to view the BIOS date
D F000 :FFF0
12. Write a program that displays a message on the screen.

ISET Mahdia Industrial Computing 3thlevel 4


Assembly programming with NASM

Objectives:
The objective of this manipulation is to familiarize the student with the environment of
NASMIDE assembler programming. We will use this tool to create programs.
advances based mainly on conditional and unconditional branch instructions.

Materials used:
A PC or compatible.

Software:
Microsoft Windows.
NASMIDE.

Duration:
3:00 AM
MedAli KAMMOUN Assembly programming with NASM

TP N° 2: Assembly Programming with


NASM

I. But of the TP
Familiarize yourself with the freeware assembler NAS associated with the NASMIDE syntax editor.

II. Presentation of NASM

II.1. What is NASM?


NASM ([Link] is a free 80x86 assembler
of use. NASMIDE ([Link] ) is a development environment
specially developed to work with NASM under DOS. (There is also a version of NASMIDE
under Windows allowing the development of 32-bit assembly programs.

II.2. Structure of an assembly program


An assembly program generally begins with the definition of data and then with the code.
Il s'agit tout d'abord de préciser à l'assembleur si on va écrire du code 16 bits ou 32 bits. Dans notre cas, nous
We will generate 16-bit code by specifying the directive [BITS 16]. We then need to specify the address of the program.
in the code segment using the directive [ORG 0x100]. (0x100 means that 100 is a number in
hexadecimal
Then, it is necessary to specify by [SEGMENT .data] that we are going to define variables and constants in the segment of
data.
The directive [SEGMENT .text] then allows to introduce the instructions of the program in language
assembler.

II.3. Editing a program


Check that the following options are configured in the options|assembler menu.

ISET Mahdia Industrial Computing 3thlevel 6


MedAli KAMMOUN Assembly programming with NASM

Check that the following options are configured in the menu options | directories

- Edit the program that displays a character A on the screen (the same as in TP 1). (Note, int 21
is now written as int 0x21 or int $21
- Press the F2 key to save the program.
- Press the keys alt + F9 simultaneously to assemble the program.
- An error window should normally indicate that no error has occurred.

- Press the Ctrl + F9 keys simultaneously to run the program.

ISET Mahdia Industrial Computing 3thlevel 7


MedAli KAMMOUN Assembly programming with NASM

In summary, pay attention to the format of numeric constants:

100 Æ decimal
100h Æ hexadecimal
0x100 Æ hexadecimal as well
$100 Æ hexadecimal as well but the first character must be a digit
10000100b Æ binary

II.4. Declaration of variables in the data segment


It is possible to define variables of character type, string, integer, long integer, real, etc. using
pseudo instructions.

Declaration of integer variables of byte type: db

nb db $10
tab db 1, 2 ,3 ,4 ,5 ,6 ,7 ,8 ,10
message db Hello IUP GSI TI
(13Æ carriage return, 10Æ next line, $Æ end of string

Declaration of integer variables of word type: dd

R dd 1024

Declaration of floating point variables: dd, dq, dt

A dd 1.2
B dd 1.e10
C dd 1.e+10
D dq 1.e-10
pi dt 3.141592353589793238462

II.5. Use of variables in the program


r db 40
Example: move al, [r]
move [r], cl

ÂIt is impossible to assign one variable directly to another variable; it is mandatory to go through
by a register.

ÂIt is also impossible to assign a constant directly to a variable; it is also necessary to pass.
by a register.

II.6. Definition of labels


Unlike DEBUG, it is possible to define labels to which certain instructions could point.
to connect. This avoids the laborious counting of bytes when it comes to making a jump in the program.
A label is declared by a name of less than 8 characters and ending with ':'

Example:

LOOP: ADD AX, CX ;Ax = Ax + Cx


.
.
.
JNE LOOP; jump if not equal

ISET Mahdia Industrial Computing 3thlevel 8


MedAli KAMMOUN Assembly programming with NASM

III. Exercises
Exercise 1:

1) Create the flowchart of the program that calculates the sum of the first 11 integers.
(0 + 1 + 2 +... + 10 + 11). Then write the program in 8086 assembly. We will use the instruction for this.
MOV, CMP, JNE, ADD, DEC or INC... We will use a variable R to store the result and a variable N.
to store the number 11.

2) Then display the result on the screen using interrupt $21 and deduce based on the character
display the numerical value of the result.

3) Same exercise but using the LOOP instruction.

Exercise 2: Using interrupt $21 in mode 9

The interruption with 21 at 9 allows displaying character strings that end with character 13, 10
$13Æ return carriage, 10Æ next line, $Æ end of string
To do this, you need to define a message with the pseudo instruction db (see db explanations) by ending this
message by 13, 10, '$'.
Then you need to assign the address of this message to registedxl (mov dx, mess where mess is the name given to)
message).

Write a program that displays the following message ten times on the screen with a line break and a newline:

If I do well in lab work, I will become a god of assembly and the teacher will be proud of me.

Exercise 3:

Design an application that calculates the product of two numbers A = 100 and B = 170. The numbers A and B are
coded on 8 bits each.
• What instruction should be used?
• Where are the operands?
• Where is the result of the operation.
• Display the bytes of the register containing the result of the operation.
• Deduce the numerical result from the displayed characters.

Mêmes questions mais avec des nombres A et B de 16 bits. PrendreA = 1642etB = 10.
Try with A=3227 and B=5.
How does the result appear?
How to make printable characters always appear?

ISET Mahdia Industrial Computing 3thlevel 9


Assembly programming with NASM:
addressing

Objectifs :
The objective of this lab is to introduce the principles of manipulating complex data.
through advanced assembler programs. For this, NASMIDE will be used as
tool for creating some programs based on the principle of addressing 'based
indexed." This principle will be used to manipulate the elements of arrays and strings.
characters. At the end of this process, the student will have to translate a sorting algorithm that
we will apply to a string.

Materials used:
A PC or compatible.

Software:
Microsoft Windows.
NASMIDE.

Durée :
1erapart: 3H00mn
2thpart: 1H30mn
Med Ali KAMMOUN Assembly programming with NASM: addressing

TP No. 3: Assembly Programming with


NASM: addressing
I. Goal of the practice
Master the different types of addressing, especially for accessing elements of arrays.

II. Reminders
To address the various elements of an array, indexed addressing must be used based on taking the register.
BX as base and DI or SI as index. To set the base of the table (table displacement)
in the data segment) in BX, just write:
MOV BX, name of the array.
Then, indexed-based addressing is used in the following way:
MOVE AL, [BX+SI] put the SIemearray element in the AL register
Where represents the position of the element in the array that we want to reach.

[Link]
Exercise 1:
Here is the following table:
tab db 5, 30, 20, 1,11, 8
Sum the 6 elements of this array and put the result in a variable S. Display the result.
the screen, deduce the numerical value.

Exercise 2:
Write an application that counts the number of letter 'e' in any sentence (minimum 50 characters)
ending with the character '$'.
Represent the organization chart.
Put the result in the register and add 32.
Print the character on the screen and check if the character that appears is consistent

Exercise 3:
Define a string of at least 50 characters ending with '$'.
Create an application (flowchart + program) that replaces all letters of a given type with a
another type (for example all 'e' by 'a').
We will enter the two letters from the keyboard using interrupt 21,8.
Key inputs will be preceded by a message of the type 'Replace:' and 'by:'

Exercise 4: Sorting an array


Here is the following array: 5, 8, 1, 7, 6
Let the following algorithm be:

ISET Mahdia Industrial Computing 3emelevel 11


MedAli KAMMOUN Assembly programming with NASM: addressing

IÅ0
jÅ1
nÅ5
while i < n-1
while j < n
If tab[i] > tab[j] then Swap(tab, i, j)
j=j+1
as long as
i=i+1
j=i+1
as long as

Apply the following algorithm to the table filling in the following state table:

Step i j tab[0] tab[1] tab[2] tab[3] tab[4]

Let the following string be:

the aim of this problem is to sort this string of characters in ascending order of ASCII code

Write the program that sorts the previous string in ascending order of ASCII code.
We will take SI for index i and DI for index j.
We will display the result using interrupt $21, 9.

ISET Mahdia Industrial Computing 3èmelevel 12


Graphic assembler programming in
MCGA mode

Objectives:
The objective of this manipulation is to present the principles of graphical programming.
using assembly language. At the end of this manipulation, the student will be able to
produce complex geometric shapes by directly manipulating video memory,
by programs in assembly language.

Materials used:
A PC or compatible.

Software:
Microsoft Windows.
NASMIDE.

Duration:
6:00 am
MedAli KAMMOUN Graphical assembly programming in MCGA mode

TP N° 4: Assembly Language Programming


Graph in MCGA mode
I. Objective of the practical work
Program the display in Graphic MCGA mode.

II. Presentation of the MCGA mode


The MCGA mode is a graphics mode created by IBM. Its particularity is to have 256 colors and a
definition of 320 pixels by 200 pixels. As a result, each pixel is represented by one byte. The screen memory is
thus represented by 320*200*1 contiguous bytes (64000).

320
0 1 2 3 4 5 6 7 318 319
. . . . . . .
. . . . . . .
320 321

200

III. Access to MCGA mode


To access the MCGA mode, we will use the BIOS software interrupts. The BIOS interrupt
is the interruption 0x10 and the function that allows you to choose your video mode is function 0. In this function
One must specify the video mode desired. The mode corresponding to MCGA is mode 0x13.
In summary, to activate the MCGA mode you need to:
-ahÅ 0
-alÅ 0x13
Interrupt call 0x10

To return to text mode at the end of the program, we will use mode 0x03.
-ahÅ 0
-alÅ 0x03
Interrupt call 0x10

IV. Access to pixels


To access a pixel, it is necessary to write to the video memory of the MCGA mode. This video memory starts
at address 0xa000 and extends over 64000 bytes.
To access a pixel with coordinatesx, y, x∈ [0.319] and y∈ [0.199] It is therefore necessary to access the memory.
address video*320+x.
The color of this pixel will depend on the byte that we write at the pixel's address. The color associated with a value of
The octet is defined in a palette that can also be modified. But that is another story ...
To write at address 0xA000 + offset, you must use the extra segment ES associated with an offset.
represented by BX. We can possibly add to all of this a fixed or variable displacement with the registers.
SIetDI.

ISET Mahdia Industrial Computing 3thlevel 14


MedAli KAMMOUN Graphical assembly programming in MCGA mode

Example:

move ax, 0xa000


move es, ax
move dl, 0xaa
mov bx, 160
move [es:bx], dl we write to address 0xa000 + 160

you
move bx, 160
move [es:bx + 320], dl ; we write to the address 0xa000 + 160 +320

you

move bx, 160


move si, 320
move [es:bx + si], dl ; we write at the address 0xa000 + 160 + 320

you

move bx, 160


move si, 320
move [es:bx + si + 640], dl we write to the address 0xa000 + 160 + 320 + 640

[Link]
Exercise 1:
Turn on the pixel at coordinates (200,100) with color code 0x32.
Turn on the first 256 pixels, assigning them a color varying from 0 to 255.
Same question but increasing the thickness of the line (5 lines).
Same question but regarding the entire height of the screen (see following figure).

Exercise 2:
Surround the screen with a frame 5 pixels thick in the color of your choice.
Grid the screen with a line of 5 pixels thick and a spacing of 10 pixels between the lines.

Exercise 3: (for the best)


Write a program that moves a rectangular pattern (8*6 pixels) to the bottom left of the window.Æright
from the rightÆ left, ... as long as no key is pressed.
The display and erasure must be done with an exclusive OR to avoid erasing what is present.
possibly already on the screen.

ISET Mahdia Industrial Computing 3thlevel 15


Med Ali KAMMOUN Graphic assembly programming in MCGA mode

The key press test will be performed using interrupt 0x16 (see appendix)
The variables to be used will be as follows:

Between each display, it is necessary (at least on Pentiums) to generate a delay. For this,
we will write a simple timer subprogram of the type:

tempo: move cx, 0xffff


tt: nop
loop tt
ret

To move the pattern to the left or right, we will use a delta variable that will take the value 1.
according to the direction of movement. This variable will serve as an increment to display the next position of the
motive.
It will be necessary to test if we reach the upper right limit (320-L) to give delta the value -1 and
test the lower left limit (0) to set delta to +1.

Exercise 4: (for the best of the best)

Write a Breakout game in 8086 assembly (paddle + ball + bricks).

Rq:

You can use: INT 16,1 - Get Keyboard Status


AH = 01
On return:
ZF = 0 if a key is pressed (even Ctrl-Break)
AX = 0 if no scan code is available
AH = ~scan code~ ; AL = ASCII character or zero if special function key

ISET Mahdia Industrial Computing 3thlevel 16


Assembly programming with TASM

Objectives:
The aim of this manipulation is to introduce the principles of programming.
assembler according to TASM syntax. For this, we will use Turbo-ASM from Borland as
tool for creating some simple programs.

Materials used:
A PC or compatible.

Software:
Microsoft Windows.
- TASM.

Duration:
3:00 AM
MedAli KAMMOUN Assembly programming with TASM

TP N° 5: Assembly Programming with


TASM
I. Objective of the TP
Programming in assembly language with Turbo ASM

II. Presentation of the Turbo Assembler


Turbo assembler is a compiler-assembler that transforms a text file containing instructions.
assemblers (mnemonics) in binary code to be executed on an x86 PC (or compatible).
You must therefore edit the source file in the included text editor, save it, and then assemble (we say assemble
rather than compiling; a term reserved for high-level languages like C or Pascal). Documentation
independent is at your disposal on Turbo ASM

III. Structure of a program in TASM


As in any program, the source file must be entered rigorously. Each definition and each
instructions should thus be written on a new line (so that the assembler can differentiate the different
The source file contains:

1. A program name under TITLE follows a name. This part is optional.


2. A section to declare a stack that is defined in the stack segment delimited by the directives
SEGMENT STACK and ENDS
3. Definitions of data declared by directives. These are grouped in the data segment.
bounded by the SEGMENT and ENDS directives
4. Then the instructions are placed (which are somewhat the heart of the program), the first of which must be
preceded by a label, that is to say by a name given to it. These are grouped in the segment
of instructions delimited by the SEGMENT and ENDS directives
5. Finally, the file must end with the directive END followed by the name of the label of the first instruction.
(to allow the compiler to know the first instruction to execute (Semicolons mark the
start of comments, meaning that all characters to the right of a semicolon will be ignored
Here is what a source file (ASM file) looks like:

TITLE nom_programme

Pile SEGMENT Stack;

Pile ENDS

DATA SEGMENT

Declare the data here

DATA ENDS

CODE SEGMENT

ASSUME DS:DATA, CS:CODE

DebutCode:

Put your program code here

CODE ENDS

ISET Mahdia Industrial Computing 3thlevel 18


MedAli KAMMOUN Assembler programming with TASM

END DebutCode
Example:

;***********************************
;* A F I C H A I N . A S M *
;****************************TA 2.0*
AFICHAIN

Pile SEGMENT Stack;

Pile ENDS

Data SEGMENT

DataChain DB This text is in the data segment.

Data ENDS

CODE SEGMENT
ASSUME DS:Data, CS:CODE
DebutCode:
move bx,Data
move ds,bx set DS to point to the .DATA segment
move dx,OFFSET DataString
;make DX point to the displacement of
DataString in the .DATA segment
move ah,9 ;function to display DOS string
int 9 PM function call
move ah,4ch program deactivation function
;the DOS
int 9 PM call of the DOS
CODE ENDS
END DebutCode

IV. Edition and execution of a program with TASM


The algorithm for solving the given problem being established, the first step is to create the file.
containing the source program in Assembly language using any text editor for this: EDIT
MS/DOS or the editor included in the Pascal or C compiler, ... This file must have the .ASM extension. For
to assemble this program and make it an object module, you need to use an assembler: MASM from Microsoft or
Borland TASM.
If the source file is [Link], the assembler will create the file [Link], and optionally the files
[Link].
The file [Link] contains the binary code of the program. This code is not yet executable. It will be.
after the editing of links.
The output of the file [Link] on screen or printer shows the code for each line of the source program.
generated, the relative address of the instructions or the generated memory area and an error message
eventually.
At the end of the assembly, the assembler displays a message that indicates the number of warnings (WARNINGS)
which could lead to a malfunction of the program and the number of serious errors (SEVERE
ERRORS). Correct the errors and redo the assembly if necessary.
The next step is the link editing that we request by running LINKTOTO. The link editor creates the
[Link] contains the executable object module and [Link] contains the list of
global symbols. You can submit several object modules as input names["[Link]","[Link]"]
[Link], when calling the linker with LINKTOTO TITI TATA.
Finally, the execution of the program will be launched by typing TOTO (under the control of MS/DOS).
The program debugging can be done using the DEBUG debugging program by typing
[Link]. There are other debugging programs such as TurboDebugger provided with the
C compiler, CodeView more complete than DEBUG, but the latter has the advantage of being provided with MS/DOS).

ISET Mahdia Industrial Computing 3thlevel 19


MedAli KAMMOUN Assembler programming with TASM

[Link]
Exercise 1:
Draw the flowchart of a program that displays the message 'Hello everyone' on the screen.
the organizational chart in the program and check its proper functioning.
Modify the previous program to display the same message 10 times. Start by listing
the organizational chart.

Exercise 2:
- Dressez l ’organigramme d ’un programme qui vous pose la question : « Somme-nous l ’après midi ? » (O/N). Si
if you type 'O' it shows you 'Good afternoon', otherwise it shows you 'Hello'.
Translate the flowchart into assembly program under TASM.

Exercise 3:
Draw the flowchart of a program that reads a lowercase character from the keyboard and converts it into
uppercase.
Transform the organizational chart into a program under TASM.

Exercise 4:

Draw the flowchart of a program that displays the following shape.

‘* *’
** **
*** ***
********
The use of a loop is mandatory.
Transform the organizational chart into a program under TASM.

ISET Mahdia Industrial Computing 3thlevel 20


Assembly programming of an interface
Input/Output: the i8255

Objectives:
The objective of this manipulation is to present to the student the principles of the
programming of the 8255 parallel interface. The experiment focuses on a parallel port
implemented on the MTS-86C Kit and is controlled by an 8255. The student's role is to
manipulate this port by writing assembly codes to control diodes
you can receive information from push buttons.
Materials used:
The MTS-86C Kit.
A PC or compatible device equipped with a serial communication port.

Logiciels :
Microsoft Windows
Hyper Terminal

Duration:
1eraétape : 3H00mn
MedAli KAMMOUN Assembly programming of an Input/Output interface: the i8255

TP No. 6: Assembly Programming of a


Input/Output interface: the i8255
I. Objective of the practical work

The purpose of this manipulation is to become familiar with the use of the parallel communication interface.
8255 integrated into the MTS-86C system.

II. Presentation
The exchanges between the microprocessor and its external environment (keyboard, screen, printer,…) occur at
the help of interface circuits often called 'Input/Output couplers'. These exchanges can be carried out
in parallel or in series.
As part of this operation, we will use a coupler for parallel connection from the 8200 family.
Intel: this is the PPI (Programmable Peripheral Interface) 8255.

[Link] 8255

The Intel 8255 is a programmable input/output interface designed for use with microprocessors.
Intel. It has 24 programmable input/output pins organized into two groups of 12 pins each and it can be
used in 3 modes.

In the first mode (MODE 0), each group of 12 pins can be programmed in batches of 4 pins as input.
or output. In (MODE 1), the second mode, each group can be programmed so that 8 lines
can be configured as inputs or outputs. The third mode, (MODE 2), is a bidirectional mode where 8
Lines are used as a bidirectional bus.

III.1. Functions of the pins

Data Bus Buffer


This bidirectional 3-state bus is used to interface the 8255 to the system bus. Data is transmitted
and received via the buffer according to the input or output instruction generated by the microprocessor. The Control
Word and configuration information are also transmitted via this bus.
Read/Write and Control Logic
The function of this block is to manage all transfers, internal and external, of data, control, or status.
Words.

__
CS
This pin, active in the low state, allows communication between the 8255 and the CPU.
__
RD
This pin, active in the low state, allows the 8255 to transfer data or configuration information.
to the CPU via the data bus.

__
WR

ISET Mahdia Industrial Computing 3thlevel 22


MedAli KAMMOUN Assembly programming of an Input/Output interface: the i8255

This pin, active in the low state, allows the microprocessor to send data or the Control Words to the
8255.

A0and A1
These input pins, combined with the WR and RD pins, control the selection of the ports (A, B, or C) or the
Control Word register.

__ __ __
A1 A0 WR CS Input Operations (Read)
RD
0 0 0 1 0 Port A->Data Bus
0 1 0 1 0 Port B->Data Bus
1 0 0 1 0 Port C->Data Bus
Output Operations (Write)
0 0 1 0 0 Data Bus -> Port A
0 1 1 0 0 Data Bus -> Port B
1 0 1 0 0 Data Bus -> Port C
1 1 1 0 0 Data Bus -> Control
Deactivation functions
x x x x 1 Data Bus -> 3 states
1 1 0 1 0 Illegal condition
x x 1 1 0 Data Bus -> 3 states

III.2. The control of groups


The CPU sends a Control Word to the 8255 to configure the ports. The Control Word contains
information such as: the mode, the bit set, the bit reset, etc.…
Each control block (Group A and Group B) accepts commands from the Read/Write Control logic, and
receives the Control Word from the internal data bus and sends commands to the associated port.
Group A of Control --- Port A and Port C (heavy weight) [C7-C4].
Control Group B --- Port B and Port C (of low weight) [C3-C0].

III.3. The selection of modes


There are 3 modes of using the 8255 interface: Mode 0, Mode 1, and Mode 2.
When the RESET input is high, all ports are configured as inputs. If the RESET input is
Disabled, the 8255 interface retains its state (in input) without any prior initialization.
The modes of ports A and B can be configured separately. However, port C is divided into two groups.
configured according to the needs of ports A and B.

ISET Mahdia Industrial Computing 3thlevel 23


MedAli KAMMOUN Assembly programming of an Input/Output interface: the i8255

Control Word format in MODE 0

Control Word format in MODE 1

ISET Mahdia Industrial Computing 3emelevel 24


Med Ali KAMMOUN Assembly programming of an Input/Output interface: the i8255

Control Word format in MODE 2

IV. Manipulation
The MTS-86C system has 3 programmable parallel interfaces 8255. The interface number 3 (PPI-3) is used
for experimentation and it is connected to the diodes and the SW buttons according to the electronic diagrams
available in the appendix.
According to these diagrams, port A is connected to the buttons SW and port B is connected to the diodes via the
74LS240 circuits.

IV.1. Example of programming

Suppose we want to write a program in assembly that allows us to turn on the LED diode.
pressing the corresponding SW button.
The program is then written as follows:

CNT3 EQU 3FD6H; Assignment of port address


APORT3 EQU 3FD0H
BPORT3 EQU 3FD2H

CODE SEGMENT
ASSUME CS:CODE, DS:CODE
ORG 0

START : MOVE SP,4000H Configuration of the Stack Pointer


MOVE AL,90H Configuration of the Control Word of the 8255
(Port A=Input, Port B and C=Output)
MOV DX, CNT3 Output port configuration
OUT DX, AL Writing the Control Word

ISET Mahdia Industrial Computing 3emelevel 25


MedAli KAMMOUN Assembly programming of an Input/Output interface: the i8255

J1: MOV DX, APORT3 Input port configuration


IN,DX Enter the data
NOT ALL Invert the signal
MOV DX, BPORT3 Output port configuration
OUT DX,AL Extract the data
JMP J1 Reconnect to J1

CODE ENDS
END START

Test the proper functioning of the previous program on the MTS-86C board.

VI.2. Programming the 8255 interface

• Write a program to turn on all the LEDs.


• Write a program to display the hexadecimal number 95 on the 8 LEDs.
• Write a program to continuously alternate the lighting of the diodes from right to left (On
turn on the 4 diodes on the right then on the left and we continue.

ISET Mahdia Industrial Computing 3thlevel 26


MedAli KAMMOUN Practical work Microprocessors

Annex
ASCII Table (7-bit)

Decimal Octal Hex Binaire Value 063 077 03F 00111111 ?


-------------------------------------------------------------------------------------- 064 100 040 01000000 @
000 000 000 00000000 NUL (Null char.) 065 101 041 01000001 A
001 001 001 00000001 SOH (Start of Header) 066 102 042 01000010 B
002 002 002 00000010 STX (Start of Text) 067 103 043 01000011 C
003 003 003 00000011 ETX (End of Text) 068 104 044 01000100 D
004 004 004 00000100 EOT (End of Transmission) 069 105 045 01000101 E
005 005 005 00000101 ENQ (Enquiry) 070 106 046 01000110 F
006 006 006 00000110 ACK (Acknowledgment) 071 107 047 01000111 G
007 007 007 00000111 BEL (Bell) 072 110 048 01001000 H
008 010 008 00001000 BS (Backspace) 073 111 049 01001001 I
009 011 009 00001001 HT (Horizontal Tab) 074 112 04A 01001010 J
010 012 00A 00001010 LF (Line Feed) 075 113 04B 01001011 K
011 013 00B 00001011 VT (Vertical Tab) 076 114 04C 01001100 L
012 014 00C 00001100 FF (Form Feed) 077 115 04D 01001101 M
013 015 00D 00001101 CR (Carriage Return) 078 116 04E 01001110 N
014 016 00E 00001110 SO (Serial In)(Shift Out) 079 117 04F 01001111 O
015 017 00F 00001111 SI (Serial Out)(Shift Out) 080 120 050 01010000 P
016 020 010 00010000 DLE (Data Link Escape) 081 121 051 01010001 Q
017 021 011 00010001 DC1 (XON) (Device Control 1) 082 122 052 01010010 R
018 022 012 00010010 DC2 (Device Control 2) 083 123 053 01010011 S
019 023 013 00010011 DC3 (XOFF)(Device Control 3) 084 124 054 01010100 T
020 024 014 00010100 DC4 (Device Control 4) 085 125 055 01010101 U
021 025 015 00010101 NAK (Negative Acknowledgement) 086 126 056 01010110 V
022 026 016 00010110 SYN (Synchronous Idle) 087 127 057 01011111 W
023 027 017 00010111 ETB (End of Trans. Block) 088 130 058 01011000 X
024 030 018 00011000 CAN (Cancel) 089 131 059 01011001 Y
025 031 019 00011001 EM (End of Medium) 090 132 05A 01011010 Z
026 032 01A 00011010 SUB (Substitute) 091 133 05B 01011011 [
027 033 01B 00011011 ESC (Escape) 092 134 05C 01011100 \
028 034 01C 00011100 FS (File Separator) 093 135 05D 01011101 ]
029 035 01D 00011101 GS (Group Separator) 094 136 05E 01011110 ^
030 036 01E 00011110 RS (Request to Send) 095 137 05F 01011111 _
031 037 01F 00011111 US (Unit Separator) 096 140 060 01100000 `
032 040 020 00100000 SP (Space) 097 141 061 01100001 a
033 041 021 00100001 ! 098 142 062 01100010 b
034 042 022 00100010 " 099 143 063 01100011 c
035 043 023 00100011 # 100 144 064 01100100 d
036 044 024 00100100 $ 101 145 065 01100101 e
037 045 025 00100101 % 102 146 066 01100110 f
038 046 026 00100110 & 103 147 067 01100111 g
039 047 027 00100111 ' 104 150 068 01101000 h
040 050 028 00101000 ( 105 151 069 01101001 i
041 051 029 00101001 ) 106 152 06A 01101010 j
042 052 02A 00101010 * 107 153 06B 01101011 k
043 053 02B 00101011 + 108 154 06C 01101100 l
044 054 02C 00101100 , 109 155 06D 01101101 m
045 055 02D 00101101 - 110 156 06E 01101110 n
046 056 02E 00101110 . 111 157 06F 01101111 o
047 057 02F 00101111 / 112 160 070 01110000 p
048 060 030 00110000 0 113 161 071 01110001 q
049 061 031 00110001 1 114 162 072 01110010 r
050 62 32 50 2 115 163 073 01110011 s
051 063 033 00110011 3 116 164 074 01110100 t
052 64 34 1A 4 117 165 075 01110101 u
053 065 035 00110101 5 118 166 076 01110110 v
054 066 036 00110110 6 119 167 077 01110111 w
055 067 037 00110111 7 120 170 078 01111000 x
056 070 038 00111000 8 121 171 079 01111001 y
057 071 039 00111001 9 122 172 07A 01111010 z
058 072 03A 00111010 : 123 173 07B 01111011 {
059 073 03B 00111011 ; 124 174 07C 01111100 |
060 074 03C 00111100 < 125 175 07D 01111101 }
061 075 03D 00111101 = 126 176 07E 01111110 ~
062 076 03E 00111110 > 127 177 07F 01111111 DEL

ISET Mahdia Industrial Computing 3thlevel 27

You might also like