Вы находитесь на странице: 1из 22

Copyright Dorling Kindersley India Pvt Ltd

PROGRAMMING
CONCEPTS IV
CHAPTER 5

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

Input/Output Programming
An I/O device is frequently referred to as a

port with an associated address


Any I/O device has a unique address, known
as port address or simply mentioned as port

The x86 Microprocessor - Lyla B Das

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

Figure 5.1 | I/O ports with address bus and data bus
and the read and write control signals

Copyright Dorling Kindersley India Pvt Ltd

Memory Mapped I/O


In this scheme, the I/O device has the same

address space and addressing scheme as


memory
For example, given the instruction MOV AL,
[3456H], the address 3456H may be a
memory location or the address of an I/O
device
So, I/O addressing does not need any special
instructions
However, the memory space gets reduced
The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

Peripheral or Isolated I/O


Also referred to as I/O mapped I/O .
Here, there are special instructions catering

to input and output devices


The address space is disjoint and separate
from the memory address space

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

I/O Instructions
IN

Usage: IN accumulator, port


OUT

Usage: OUT port, accumulator

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

Fixed Port Addressing


This is used only when the address of an

I/O device is
8 bits wide
Here, the address of the port is directly
mentioned in the instruction
Note that data can be 8 bits or 16 bits,
depending on
the data bus width of the I/O device

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

Fixed Port Addressing


Examples

IN AL, 45H
IN AX, 12H
OUT 34H, AL
OUT 0FCH, AX

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

Variable Port Addressing


This is used when the addressed port has a

16-bit address
Here, the port address is to be loaded into DX
and then only the I/O instruction can be used
Only the DX register can be used for this

The x86 Microprocessor - Lyla B Das

Copyright Dorling Kindersley India Pvt Ltd

Variable Port Addressing


Examples
MOV DX, 9876H
IN AL, DX
MOV DX, 0FC6H
OUT DX, AX

The x86 Microprocessor - Lyla B Das

10

Copyright Dorling Kindersley India Pvt Ltd

Example 5.1
An 8086 is connected to two ports with 8-bit
addresses. The address of the input port is
67H and the address of the output port is
FEH. The input port sends a 16-bit data to the
processor. Only the lower 12 bits are to be
transferred to the output port. Write the
relevant program lines for this.

The x86 Microprocessor - Lyla B Das

11

Copyright Dorling Kindersley India Pvt Ltd

Example 5.1 Solution

The x86 Microprocessor - Lyla B Das

12

Issues to be resolved for successful execution and linking

of different modules. The different modules that constitute


the solution may be in different code segments.
The data used by one module may have to be accessed

by many different modules and the permission for this


must be indicated.
Some labels used in a module may not be found therein.

In that case, there must be some indication that these are


defined in some other module to which linking is possible
and will be done.
The x86 Microprocessor - Lyla B Das

13

Copyright Dorling Kindersley India Pvt Ltd

Modular Programming

When a data item or a procedure is to be allowed

to be accessed by other modules, it is declared


as PUBLIC
Examples: PUBLIC num1, num2, num3
This may also be declared as
PUBLIC num1
PUBLIC num2
PUBLIC num3
A procedure named MULT can be allowed access
from other modules if it is declared as
PUBLIC mult
The x86 Microprocessor - Lyla B Das

14

Copyright Dorling Kindersley India Pvt Ltd

The directive PUBLIC

Copyright Dorling Kindersley India Pvt Ltd

The directive EXTRN


When a module needs to use data and code

that have been defined elsewhere, it should


use the directive EXTRN meaning that the
labels being used in this module are external
to the module
Format

EXTRN name1:type
EXTRN name1:type, name2:type
The x86 Microprocessor - Lyla B Das

15

Copyright Dorling Kindersley India Pvt Ltd

The directive EXTRN


Examples

EXTRN num1:byte
EXTRN num2:word,num3:word
EXTRN multi:far; a far procedure is declared

The x86 Microprocessor - Lyla B Das

16

Copyright Dorling Kindersley India Pvt Ltd

Running two modules


moda.asm and modb.asm

The x86 Microprocessor - Lyla B Das

17

Copyright Dorling Kindersley India Pvt Ltd

Running two modules


moda.asm and modb.asm

The x86 Microprocessor - Lyla B Das

18

Copyright Dorling Kindersley India Pvt Ltd

Assembling the two modules


The modules can be assembled and linked

together
The command is
ml filename1.asm filename2.asm
filename3.asm
Example:
ml moda.asm modb.asm

The x86 Microprocessor - Lyla B Das

19

Copyright Dorling Kindersley India Pvt Ltd

Example
5.4
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 19811997. All rights reserved.
Assembling: moda.asm
Assembling: modb.asm
Microsoft (R) Segmented Executable Linker Version 5.60.339 Dec
5 1994
Copyright (C) Microsoft Corp 19841993. All rights reserved.
Object Modules [.obj]: moda.obj+
Object Modules [.obj]: modb.obj
Run File [modul1a.exe]: moda.exe
List File [nul.map]: NUL
Libraries [.lib]:
Defi nitions File [nul.def]:
LINK : warning L4021: no stack segment
C:\MASM6~1.14N\BIN>
The x86 Microprocessor - Lyla B Das

20

Copyright Dorling Kindersley India Pvt Ltd

Listing and Debugging


The list files of both modules can be obtained

by the command
ml/Fl moda.asm modb.asm.
Note In the list file, the address of lines of
code which use external data/program have
the letter E in it
For debugging use the command debug
moda.exe. Then, the sequence of steps of
going from one code segment to the other
and returning can be clearly observed
The x86 Microprocessor - Lyla B Das

21

Copyright Dorling Kindersley India Pvt Ltd

Frequently Used Modules


We can place frequently used programs in

different modules, and link it to our modules


when needed
Examples are conversion programs, display
programs and the like

The x86 Microprocessor - Lyla B Das

22