Professional Documents
Culture Documents
LEARNING GUIDE
Week No.: 6
TOPIC/S
EXPECTED COMPETENCIES
CONTENT/TECHNICAL INFORMATION
I/O Instructions
The instruction set contains one type of instruction that transfers information to
an I/O device (OUT) and another to read information from an I/O device (IN).
Instructions (INS and OUTS found on all versions except the 8086/8088) are also
provided to transfer strings of data between the memory and an I/O device. (Brey, 1997)
Both the IN and OUT instructions transfer data between an I/O device and the
microprocessor's accumulator (AL, AX, or EAX). The I/O address is stored in register
DX as a 16-bit I/O address or in the byte (p8) immediately following the opcode as an
8-bit I/O address. Intel calls the 8-bit form (p8) a fixed address because it is stored with
the instruction, usually in a ROM. The 16-bit I/O address in DX is called a variable
address because it is stored in a DX then used to address the I/O device. Other
instructions that use DX to address I/Oare INS and OUTS instructions. (Brey, 1997)
Whenever data are transferred using the IN or OUT instruction, the I/O address,
often called a port number, appears on the address bus. The external I/O interface
decodes it in the same manner that it decodes a memory address. The 8-bit fixed port
number (p8) appears on address bus connections A7- A0 with bits A15-A8 equal to
000000002. The address connections above A15 are undefined for an I/O instruction.
The 16-bit variable port number (DX) appears on address connection A15-A0. (Brey,
1997)
This means that the first 256 I/O port addresses (OOH-FFH) are accessed by
both the fixed and variable I/O instructions, but any I/O address from OlOOH-FFFFH
is accessed only by the variable I/O address. In many dedicated task systems, only the
rightmost 8-bits of the address are decoded, thus reducing the amount of circuitry
required for decoding. In a PC computer, all 16 address bus bits are decoded with
locations OOXXH-03XXH, which are the 110 addresses used for 110 inside the PC.
(Brey, 1997)
The INS and OUTS instructions address the 110 device using the DX register,
but do not transfer data between the accumulator and the 110 device as IN and OUT.
Instead, these instructions transfer data between memory and the 110 device. The
memory address is located by ES:DI for the INS instruction and DS:SI for the OUTS
instruction. As with other string instructions, the contents of the pointers are
incremented or decremented as dictated by the state of the direction flag (DF). Both
75
INS and OUTS can also be prefixed with the REP prefix, allowing more than one byte
or word to be transferred between 110 and memory. (Brey, 1997)
Isolated I/O
- The most common I/O transfer technique used in the Intel microprocessor-
based system is isolated I/O. The term isolated describes how the I/O
locations are isolated from the memory system in a separate I/O address
space.
– in the PC, isolated I/O ports are used to control peripheral devices.
– an 8-bit port address is used to access devices located on the system
board, such as the timer and keyboard interface.
– a 16-bit port is used to access serial and parallel ports, video and disk drive
systems.
(https://microprocessors414.files.wordpress.com, 2015)
Memory and I/O Maps for 8086 Microprocessors. (Isolated I/O) (Brey, 1997)
- Memory-mapped I/O does not use the IN, INS, OUT, or OUTS
instructions.
76
- It uses any instruction that transfers data between the microprocessor and
memory and treated as a memory location in memory map
- Advantage is any memory transfer instruction can access the I/O device.
- Disadvantage is a portion of memory system is used as the I/O map thus
reduces memory available to applications
(https://microprocessors414.files.wordpress.com, 2015)
Memory and I/O Maps for 8086 Microprocessors. (Memory Mapped I/O) (Brey, 1997)
– the PC uses part of I/O map for dedicated functions, as shown below.
– I/O space between ports 0000H and 03FFH is normally reserved for the
system and ISA bus.
– ports at 0400H–FFFFH are generally available for user applications,
mainboard functions, and the PCI bus.
– 80287 coprocessor uses 00F8H–00FFH, so Intel reserves I/O ports 00F0H–
00FFH.
(https://microprocessors414.files.wordpress.com, 2015)
77
The I/O map of a personal computer illustrating many of the fixed I/O areas
(Brey, 1997)
The basic input device is a set of three-state buffers. The basic output device is
a set of data latches. The term IN refers to moving data from the I/O device into the
microprocessor, and the term OUT refers to moving data out of the microprocessor to
the I/O device. (Brey, 1997)
Three-state buffers are used to construct the 8-bit input port depicted below.
• External TTL data are connected to the inputs of the buffers.
– buffer outputs connect to the data bus
• The circuit of allows the processor to read the contents of the eight switches that
connect to any 8-bit section of the data bus when the select signal becomes a logic 0.
(https://microprocessors414.files.wordpress.com, 2015)
78
• Receives data from the processor and usually must hold it for some external device.
– latches or flip-flops, like buffers in the input device, are often built into the
I/O device
• Figure below shows how eight light-emitting diodes (LEDs) connect to the processor
through a set of eight data latches.
• The latch stores the number output by the microprocessor from the data bus so that
the LEDs can be lit with any 8-bit binary number.
(https://microprocessors414.files.wordpress.com, 2015)
79
The basic output interface connected to a set of LED displays (Brey, 1997)
• Latches hold the data because when the processor executes an OUT, data are
only present on the data bus for less than 1.0 µs.
– the viewer would never see the LEDs illuminate
• When the OUT executes, data from AL, AX, or EAX transfer to the latch via
the data bus.
• Each time the OUT executes, the SEL signal activates, capturing data to the
latch.
– data are held until the next OUT
• When the output instruction is executed, data from the AL register appear on
the LEDs.
Handshaking
Many I/O devices accept or release information at a much slower rate than the
microprocessor. Another method of I/O control, called handshaking or polling,
synchronizes the I/O device with the microprocessor. An example device that requires
handshaking is a parallel printer that prints 100 characters per second (CPS). It is
obvious that the microprocessor can definitely send more than 100 CPS to the printer,
so a way to slow the microprocessor down to match speeds with the printer must be
developed. (Brey, 1997)
80
The DB25 connector found on computers and the Centronics 36-pin connector
found on printers for the Centronics parallel printer interface. (Brey, 1997)
• The software polls or tests the BUSY pin to decide whether the printer is busy.
– If the printer is busy, the processor waits
– if not, the next ASCII character goes to the printer
• This process of interrogating the printer, or any asynchronous device like a printer, is
called handshaking or polling.
The main difference between memory decoding and isolated I/0 decoding is the
number of address pins connected to the decoder. We decode A32-A0, A23-A0, or A19-
A0 for memory and A15-A0 for isolated I/O. Sometimes, if the I/O devices use only fixed
I/O addressing, we decode only A7-A0. Another difference is that we use the IORC and
IOWC to activate I/O devices for a read or a write operation. On earlier versions of the
microprocessor, IO/M = 1 and RD or WR are used to activate I/O devices. On the
newest versions of the microprocessor, the M/IO = 0 and W/R are used to activate I/O
devices.
81
As mentioned, the fixed 1/0 instruction uses an 8-bit I/O port address that
appears on A15-A0 as 0000H-00FFH. If a system will never contain more than 256 I/O
devices, we often decode only address connections, A0 for an 8-bit I/O port address.
Thus, we ignore address connections A15-A8.
(You cannot ignore these address connections in a personal computer-all 16 bits
must be used.) Please note that the DX register can also address I/O ports OOH-FFH.
Also note that if the address is decoded as an 8-bit address, then we can never include
I/0 devices that use a 16-bit I/O address. (Brey, 1997)
• Fixed I/O instruction uses an 8-bit I/O port address that on A15–A0 as 0000H–00FFH.
– we often decode only address connections A7 – A0 for an 8-bit I/O port address
• The DX register can also address I/O ports 00H–FFH.
• If the address is decoded as an 8-bit address, we can never include I/O devices using
a 16- bit address.
– the PC never uses or decodes an 8-bit address
Figure below shows a 74ALS138 decoder that decodes 8-bit I/O ports F0H - F7H.
– identical to a memory address decoder except we only connect address bits
A7 –A0 to the inputs of the decoder.
(https://microprocessors414.files.wordpress.com, 2015)
A port decoder that decodes 8-bit I/O ports. This decoder generates
active low outputs for ports F0H-F7H. (Brey, 1997)
Figure below shows the PAL version of this decoder. Notice that this is a better
decoder circuit because the number of integrated circuits has been reduced to one
device, the PAL. The program for the PAL appears in after the figure. (Brey, 1997)
82
A PAL16L8 decoder that generates I/O port signals for port F0H – F7H and its corresponding code to function
as a decoder (Brey, 1997)
• Data transferred to an 8-bit I/O device exist in one of the I/O banks in a 16-bit
processor such as 80386SX.
• The I/O system on such a microprocessor contains two 8-bit memory banks.
• Figure below shows separate I/O banks for a 16-bit system such as 80386SX.
• Because two I/O banks exist, any 8-bit I/O write requires a separate write.
(https://microprocessors414.files.wordpress.com, 2015)
The I/O banks found in 8086, 80186, 80286 and 80386SX (Brey, 1997)
• I/O reads don’t require separate strobes.
– as with memory, the processor reads only the byte it expects and ignores the other
byte
– a read can cause problems when an I/O device responds incorrectly to a read
operation
84
• Figure below shows a system with two different 8-bit output devices, located at 40H
and 41H.
• These are 8-bit devices and appear in different I/O banks.
– thus, separate I/O write signals are generated to clock a pair of latches that capture
port data
(https://microprocessors414.files.wordpress.com, 2015)
An I/O port decoder that selects ports 40H and 41H for output data. (Brey, 1997)
• Figure above shows a 16-bit device connected to function at 8-bit addresses 64H &
65H.
• The PLD decoder does not have a connection for address bits BLE (A0) and BHE
because the signals don’t apply to 16-bit-wide devices.
• The program for the PLD, illustrated in below, shows how the enable signals are
generated for the three-state buffers (74HCT244) used as input devices.
(https://microprocessors414.files.wordpress.com, 2015)
Although 32-bit wide I/O ports are not common, they may eventually become
commonplace because of new buses found in computer systems. The once-promising
EISA system bus supports 32-bit I/O as well as the VESA local and current PCI bus,
but these are only found in some computer systems.
The circuit below illustrates a 32-bit input port for the 80386DX or 80486
microprocessors. As with prior interfaces, this circuit uses a single PAL to decode the
I/O ports and four 74LS244 buffers to connect the I/O data to the data bus. The I/O
ports decoded by this interface are 8-bit ports 70H-73H, as illustrated by the PAL
program below. Also notice again that we decode only an 8-bit I/O port address.
With the Pentium/Pentium Pro microprocessors and their 64-bit data bus, I/O
ports appear in various banks as determined by the I/O port address. For example, 8-bit
I/O port 0034H appears in Pentium I/O bank 5, while the 16-bit 1/0 port 0034H-0035H
appears in Pentium banks 5 and 6. A 32-bit I/O access in the Pentium system can appear
in any four consecutive I/O banks. For example, 32-bit 1/0 port 0100H-0103H appears
in banks 0-3. (Brey, 1997)
(Rahman, 2020)
01 = Port B is selected
10 = Port C is selected
Features
• Data bus buffer: It is a bidirectional (tri-state) data bus. Notice that this connects
every single block in 8255 with each other except the read-write control logic
block. All the data flows through this bus.
• Ports A, B, and C are divided into two groups.
• Group A: Port A (PA0-PA7) + Port C upper (PC4-PC7)
• Group B: Port B (PB0-PB7) + Port C upper (PC0-PC3)
• There are separate control units for Group A and Group B that get their control
signals from the main read-write logic control logic block. The control logic
receives individual control signals as inputs and responds accordingly.
• Group A control and Group B control blocks are connected to their respective
Group blocks. They control the flow of data and modes of operation at those
ports.
• Controls of group A and group B, represented by the control blocks ‘group A
control‘ and ‘group B control‘ together make up ‘the control port.’ This port is
used to specify the modes of operations and is manipulated by the
microprocessor (by writing control word). Hence, these two blocks are
connected by the data bus in the above diagram.
• These control blocks play a crucial role as they ensure that ports can be used in
a number of ways. Hence, 8255 gives a lot of freedom and control to the user.
(Rahman, 2020)
• If we see from outside, there is only one control port. But internally, there are
two different latches to store the control word for I/O mode and BSR mode.
Control words for these modes are stored in the corresponding latches.
(Rahman, 2020)
Operating Modes
I/O mode
In I/O mode of operation, Ports A, B, and C of the 8255 can function in 3 different
modes:
• Mode 0
• Mode 1
• Mode 2
(Rahman, 2020)
These modes can be selected by setting and resetting specific bits of the I/O
mode control word. (Rahman, 2020)
You can observe from the illustration below how the microprocessor selects
the mode of operation by issuing control instructions at the control port (control
word). (Rahman, 2020)
In other words, the below diagram explains to you, “what exactly does the
microprocessor write to the control port to set a particular port in a particular mode of
operation.” (Rahman, 2020)
92
Mode 0
We can say that this mode is used for I/O devices whose timing characteristics
are already known. For example, we can use this mode for an input device that wants
to provide us with input every 0.5 seconds. So, we program the microprocessor to read
from that device after every 0.5 seconds without the input device actually telling the
microprocessor that it wants to send data. (Rahman, 2020)
This mode can be applied when we need to read switch settings or temperature
readings from a sensor. In such cases, the readings are always present at the input port.
The microprocessor reads them when it requires the data for processing. (Rahman,
2020)
93
This mode uses handshaking to make the exchange of data more efficient.
In mode 1 and mode 2 operations of I/O ports, the I/O device and the processor
can communicate with each other apart from the exchange of input and output data. It
goes down like this:
When the input device writes data at a port, it tells the port that new data has
entered. The port then informs the microprocessor that new unread data is there at the
port. When the microprocessor is done reading the data, the port informs the input
device that the data on the port is read and new data can be entered. This
communication happens using the ‘handshaking signals’. (Rahman, 2020)
The lines of port C are used for handshaking signals. Thus, only port A and port
B can work in mode 1, while port C provides handshake signals. (Rahman, 2020)
Three handshake signals are required for a port to work in handshake mode.
Hence, port A and port B can both work in mode 1 simultaneously, with two lines of
port C still remaining. The unused lines of port C for handshaking can be used for I/O
in mode 0. (Rahman, 2020)
The below table specifies which lines of port C provide handshake signals for
ports A and B. (Rahman, 2020)
In mode 0 and mode 1, ports A and B can only work as either input ports or
output ports. But in mode 2, port A can play both roles simultaneously. A port
programmed in mode 2 can take inputs and give outputs. Since data transfer is
bidirectional in mode 2, handshake signals required are also more. (Rahman, 2020)
There are five handshake signals required for port A to function in mode 1.
These are provided by port C pins PC3-PC7. The remaining three pins can function as
handshake pins for port B working in mode 1 or can function as normal I/O pins if port
B is programmed to work in mode 0. (Rahman, 2020)
It can be easily deduced that only port A can work in mode 2 since only three
pins of port C remain after allocating handshake signals to port A in mode 2 and the
required number for port B also to work in mode 2 is five.
As stated before, bits of port C can be set and reset individually in the BSR
mode. It is achieved by writing the appropriate control instruction at the control port.
These instructions are called ‘BSR mode control words.’ The illustration below
explains what you need to write on the control port to manipulate the bits of port C in
BSR mode. (Rahman, 2020)
PROGRESS CHECK
1. Design a NAND Gate Address Decoder that could access the DMA
Controller in the Personal Computer
2. Design an Address Decoder using the 3-8 Line decoder that could access
the Timer in the Personal Computer
3. Design an Address Decoder using the Dual 2 to 4 Line decoder that could
access the CGA Adapter.
4. Identify the following modes, ports and inputs/outputs do the following
control words set the 8255 PPI.
a. 101010102
b. 111110112
c. 100101112
d. 001001102
e. 100100012
5. What is the distinction between addressing the memory as compared to
addressing the I/O’s? Justify your answer in not less than 3 sentences and
not more than 5 sentences.
REFERENCES
Brey, B. B. (1997). The Intel Microprocessors. Upper Saddle River, New Jersey: Prentice-
Hall, Inc.
LEARNING GUIDE
TOPIC/S
1. Introduction to Assembly Language and Machine language.
2. Creation of a basic assembly language Shell program with program terminate
interrupt.
EXPECTED COMPETENCIES
Upon completing this Learning Module, you will be able to:
1. Know what is Assembly Language
2. Program basic Applications using 8086 Microprocessor
CONTENT/TECHNICAL INFORMATION
Machine Language
Since computers are digital devices, they only recognize binary data. Every
program, video, image, and character of text is represented in binary. This binary data,
or machine code, is processed as input by the CPU. The resulting output is sent to the
operating system or an application, which displays the data visually. For example, the
ASCII value for the letter "A" is 01000001 in machine code, but this data is displayed
as "A" on the screen. An image may have thousands or even millions of binary values
that determine the color of each pixel. (https://techterms.com, 2020)
Assembly Language
initializations, and allow the use of variables and labels as well as controlling output.
(Austerlitz, 2003)
On PCs, Assembler is normally used only under MS-DOS. When running a 32-
bit, protected-mode operating system (including Windows 95/NT and above), low-level
programs which directly access registers or memory locations produce protection
violations. All low-level access must be made through appropriate software drivers.
(Austerlitz, 2003)
For MS-DOS PCs, the most popular Assembly language was Microsoft Macro
Assembler, or MASM. As with most popular compilers, MASM was upgraded on a
regular basis. Most of this discussion refers to version 5.0 or later, which simplified the
use of certain directives and included support for instructions available only on 80286
and 80386 CPUs. (Austerlitz, 2003)
Since larger models require larger addresses, they produce bigger and slower
programs than a smaller model will. In selecting a model for a program, try to estimate
the maximum amount of data storage you will need. Let us say you are writing an FFT
program, using 16-bit integer math and a maximum sample size of 2048 points. Since
each point requires two integers (real and imaginary) and each integer is 2 bytes long,
you need 8096 bytes just to store the input (or output) data. Even if you had separate
arrays for input and output data, that would still be only 16,192 bytes. As a safety
98
margin, for temporary storage, we will double this number, to 32,384 bytes, which is
only half of a 64K segment. It is more difficult to estimate the size of the code. In this
example, we would start with the Small model. If the code turned out to be larger than
64K (which is not easy to do in assembly language), we would move to the Medium
model. These same memory models also apply to Microsoft's high-level DOS language
compilers. If you are writing a MASM program to work with another high-level
language, you should use the same memory model for both. (Austerlitz, 2003)
Here is an example of a simple MASM program that displays a text string (“This
is a simple MASM program”) on the screen using DOS function 09h:
Several directives are used here. DOSSEG tells MASM to take care of the order
of the various segments (code, data, stack), a detail we would rather ignore. The
directive .DATA indicates the start of the data segment while .CODE indicates the start
of the code segment. The message is referred to by the label text, where the DB directive
(Defines Bytes) indicates that this is byte data (the quotation marks indicate ASCII
text). The string must be terminated by ASCII character 24h (“$”) for DOS function
09h. The executable instructions are placed in the code segment. The label, go, refers
to the start of the program. The address of the text string is loaded into registers DS:DX.
Then DOS function 09h is called, to display the string. Finally, DOS function 4Ch is
called to exit the program and return to DOS. The final END directive tells MASM to
begin program execution at the label (address) go. (Austerlitz, 2003)
variable initializations. The INCLUDE directive can read this file in during assembly.
(Austerlitz, 2003)
These instructions are used to transfer the data from the source operand to the
destination operand. Following are the list of instructions under this group −
MOV − Used to copy the byte or word from the provided source to the provided
destination.
POP − Used to get a word from the top of the stack to the provided location.
(https://www.tutorialspoint.com, 2020)
IN − Used to read a byte or word from the provided port to the accumulator.
OUT − Used to send out a byte or word from the accumulator to the provided port.
(https://www.tutorialspoint.com, 2020)
LEA − Used to load the address of operand into the provided register.
LDS − Used to load DS register and other provided register from the memory
LES − Used to load ES register and other provided register from the memory.
(https://www.tutorialspoint.com, 2020)
LAHF − Used to load AH with the low byte of the flag register.
PUSHF − Used to copy the flag register at the top of the stack.
POPF − Used to copy a word at the top of the stack to the flag register.
101
Arithmetic Instructions
(https://www.tutorialspoint.com, 2020)
NPG − Used to negate each bit of the provided byte/word and add 1/2’s complement.
(https://www.tutorialspoint.com, 2020)
(https://www.tutorialspoint.com, 2020)
102
DIV − Used to divide the unsigned word by byte or unsigned double word by word.
IDIV − Used to divide the signed word by byte or signed double word by word.
CBW − Used to fill the upper byte of the word with the copies of sign bit of the lower
byte.
CWD − Used to fill the upper word of the double word with the sign bit of the lower
word.
(https://www.tutorialspoint.com, 2020)
These instructions are used to perform operations where data bits are involved,
i.e. operations like logical, shift, etc.
AND − Used for adding each bit in a byte/word with the corresponding bit in another
byte/word.
OR − Used to multiply each bit in a byte/word with the corresponding bit in another
byte/word.
XOR − Used to perform Exclusive-OR operation over each bit in a byte/word with
the corresponding bit in another byte/word.
(https://www.tutorialspoint.com, 2020)
SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in LSBs.
SHR − Used to shift bits of a byte/word towards the right and put zero(S) in MSBs.
SAR − Used to shift bits of a byte/word towards the right and copy the old MSB into
the new MSB.
(https://www.tutorialspoint.com, 2020)
103
ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and to
Carry Flag [CF].
ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB and to
Carry Flag [CF].
RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF and CF to
MSB.
RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to CF and CF to
LSB.
(https://www.tutorialspoint.com, 2020)
String Instructions
SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a byte in
AL or string word with a word in AX.
LODS/LODSB/LODSW − Used to store the string byte into AL or string word into
AX.
(https://www.tutorialspoint.com, 2020)
104
CALL − Used to call a procedure and save their return address to the stack.
JMP − Used to jump to the provided address to proceed to the next instruction.
(https://www.tutorialspoint.com, 2020)
JLE/JNG − Used to jump if less than/equal/if not greater than instruction satisfies.
(https://www.tutorialspoint.com, 2020)
105
STI − Used to set the interrupt enable flag to 1, i.e., enable INTR input.
CLI − Used to clear the interrupt enable flag to 0, i.e., disable INTR input.
(https://www.tutorialspoint.com, 2020)
These instructions are used to execute the given instructions for number of
times. Following is the list of instructions under this group −
LOOP − Used to loop a group of instructions until the condition satisfies, i.e., CX = 0
(https://www.tutorialspoint.com, 2020)
Interrupt Instructions
These instructions are used to call the interrupt during program execution.
INT − Used to interrupt the program during execution and calling service specified.
(https://www.tutorialspoint.com, 2020)
106
8086 Interrupts
Interrupts are also triggered by different hardware; these are called hardware
interrupts. To make a software interrupt there is an INT instruction, it has very simple
syntax:
INT value
The following example uses INT 10h sub-function 0Eh to type a "Hello!"
message. This function displays a character on the screen, advancing the cursor and
scrolling the screen as necessary.
(http://www.emu8086.com, 2011)
108
mov dh, 10
mov dl, 20
mov bh, 0
mov ah, 2
int 10h
(http://www.emu8086.com, 2011)
109
(http://www.emu8086.com, 2011)
110
example:
mov al, 1
mov bh, 0
mov bl, 0011_1011b
mov cx, msg1end - offset msg1 ; calculate message size.
mov dl, 10
mov dh, 7
push cs
pop es
mov bp, offset msg1
mov ah, 13h
int 10h
jmp msg1end
msg1 db " hello, world! "
msg1end:
(http://www.emu8086.com, 2011)
111
note: the emulator and windows command line prompt do not support background
blinking, however to make colors look the same in dos and in full screen mode it is
required to turn off the background blinking.
0 0000 black
1 0001 blue
2 0010 green
3 0011 cyan
4 0100 red
5 0101 magenta
6 0110 brown
7 0111 light gray
8 1000 dark gray
9 1001 light blue
A 1010 light green
B 1011 light cyan
C 1100 light red
D 1101 light magenta
E 1110 yellow
F 1111 white
note:
; use this code for compatibility with dos/cmd prompt full screen mode:
mov ax, 1003h
mov bx, 0 ; disable blinking.
int 10h
(http://www.emu8086.com, 2011)
112
(http://www.emu8086.com, 2011)
113
Note:
the resolution of the wait period is 977 microseconds on many systems (1 million
microseconds - 1 second).
Windows XP does not support this interrupt (always sets CF=1).
(http://www.emu8086.com, 2011)
114
For the emulator physical drive A: is this file c:\emu8086\FLOPPY_0 (for BIOS
interrupts: INT 13h and boot).
Note: DOS file system limits the file and directory names to 8 characters, extension is
limited to 3 characters;
example of a valid file name: myfile.txt (file name = 6 chars, extension - 3 chars).
extension is written after the dot, no other dots are allowed. Back to Top
INT 21h / AH=1 - read character from standard input, with echo, result is stored in
AL.
if there is no character in the keyboard buffer, the function waits until any key is
pressed.
(http://www.emu8086.com, 2011)
115
example:
mov ah, 1
int 21h
mov ah, 2
mov dl, 'a'
int 21h
mov ah, 6
mov dl, 255
int 21h ; get character from keyboard buffer (if any) or set ZF=1.
(http://www.emu8086.com, 2011)
116
example:
mov ah, 7
int 21h
INT 21h / AH=9 - output of a string at DS:DX. String must be terminated by '$'.
example:
org 100h
mov dx, offset msg
mov ah, 9
int 21h
ret
msg db "hello world $"
INT 21h / AH=0Ah - input of a string to DS:DX, fist byte is buffer size, second byte
is number of chars actually read. this function does not add '$' in the end of string. to
print using INT 21h / AH=9 you must set dollar character at the end of it and start
printing from address DS:DX + 2.
example:
org 100h
mov dx, offset buffer
mov ah, 0ah
int 21h
jmp print
buffer db 10,?, 10 dup(' ')
print:
xor bx, bx
mov bl, buffer[1]
mov buffer[bx+2], '$'
mov dx, offset buffer + 2
mov ah, 9
int 21h
ret
the function does not allow to enter more characters than the specified buffer size.
see also int21.asm in c:\emu8086\examples
(http://www.emu8086.com, 2011)
117
INT 21h / AH=0Ch - flush keyboard buffer and read standard input.
entry: AL = number of input function to execute after flushing buffer (can be
01h,06h,07h,08h, or 0Ah - for other values the buffer is flushed but no input is
attempted); other registers as appropriate for the selected input function.
org 100h
mov dx, offset filepath
mov ah, 39h
int 21h
ret
Return:
Carry Flag is clear if successful, AX destroyed.
Carry Flag is set on error AX = error code.
Notes: even if new directory name includes a drive letter, the default drive is not
changed,
only the current directory on that drive.
(http://www.emu8086.com, 2011)
119
example:
org 100h
mov ah, 3ch
mov cx, 0
mov dx, offset filename
mov ah, 3ch
int 21h
jc err
mov handle, ax
jmp k
filename db "myfile.txt", 0
handle dw ?
err:
; ....
k:
ret
Return:
CF clear if successful, AX = file handle.
CF set on error AX = error code.
(http://www.emu8086.com, 2011)
120
example:
org 100h
mov al, 2
mov dx, offset filename
mov ah, 3dh
int 21h
jc err
mov handle, ax
jmp k
filename db "myfile.txt", 0
handle dw ?
err:
; ....
k:
ret
Note: data is read beginning at current file position, and the file position is updated
after a successful read the returned AX may be smaller than the request in CX if a
partial read occurred.
(http://www.emu8086.com, 2011)
121
return:
CF clear if successful; AX = number of bytes actually written.
CF set on error; AX = error code.
note: if CX is zero, no data is written, and the file is truncated or extended to the
current position data is written beginning at the current file position, and the file
position is updated after a successful write the usual cause for AX < CX on return is a
full disk.
Note: DOS does not erase the file's data; it merely becomes inaccessible because the
FAT chain for the file is cleared deleting a file which is currently open may lead to
filesystem corruption.
Notes:
for origins 1 and 2, the pointer may be positioned before the start of the file; no error
is returned in that case, but subsequent attempts to read or write the file will produce
errors. If the new position is beyond the current end of file, the file will be extended
by the next write (see AH=40h).
(http://www.emu8086.com, 2011)
122
example:
org 100h
mov ah, 3ch
mov cx, 0
mov dx, offset filename
mov ah, 3ch
int 21h ; create file...
mov handle, ax
mov al, 0
mov bx, handle
mov cx, 0
mov dx, 7
mov ah, 42h
int 21h ; seek...
filename db "myfile.txt", 0
handle dw ?
data db " hello files! "
data_size=$-offset data
buffer db 4 dup(' ')
(http://www.emu8086.com, 2011)
123
Notes:
the returned path does not include a drive and the initial backslash.
INT 21h / AH=4Ch - return control to the operating system (stop program).
Note: allows move between directories on same logical drive only; open files should
not be renamed!
INT 33h / AX=0000 - mouse initialization. any previous mouse pointer is hidden.
returns:
if successful: AX=0FFFFh and BX=number of mouse buttons.
if failed: AX=0
example:
mov ax, 0
int 33h
see also: mouse.asm in examples.
(http://www.emu8086.com, 2011)
124
INT 33h / AX=0003 - get mouse position and status of its buttons.
returns:
if left button is down: BX=1
if right button is down: BX=2
if both buttons are down: BX=3
CX = x
DX = y
example:
mov ax, 3
int 33h
Further Reading:
Click the link below for the tutorial on how to program using Assembly Language
using the EMU8086 Software:
https://drive.google.com/drive/folders/1DjvqbBqAOeib4pOTQhsxnVXnaDCmXvGF
?usp=sharing
PROGRESS CHECK
REFERENCES
LEARNING GUIDE
TOPIC/S
1. Introduction to Microcontrollers
2. Introduction to the Arduino Microcontroller Board and Atmel Microcontroller and
its function.
EXPECTED COMPETENCIES
CONTENT/TECHNICAL INFORMATION
Microcontroller
Microcontrollers VS Microprocessors
People will sometimes use the term “microprocessor” or "MPU" when referring
to a microcontroller, but these two devices are not necessarily the same. Both
microprocessors and microcontrollers function as small, highly integrated computer
systems, but they may serve different purposes. (Keim, 2019)
127
Elements of a Microcontroller
The CPU performs arithmetic operations, manages data flow, and generates
control signals in accordance with the sequence of instructions created by the
programmer. The extremely complex circuitry required for CPU functionality is not
visible to the designer. In fact, thanks to integrated development environments and
high-level languages such as C, writing code for microcontrollers is often a fairly
straightforward task. (Keim, 2019)
Memory
Volatile memory (i.e., RAM) is used for temporary data storage. This data is
lost when the microcontroller loses power. Internal registers also provide temporary
data storage, but we don’t think of these as a separate functional block because they are
integrated into the CPU. (Keim, 2019)
Peripherals
We use the word “peripheral” to describe the hardware modules that help a
microcontroller to interact with the external system. The following bullet points identify
the various categories of peripherals and provide examples.
Support Circuitry
ATMega328P Microcontroller
ATMEGA328P is a 28 pin chip as shown in pin diagram above. Many pins of the chip
here have more than one function. We will describe functions of each pin in below
table. (https://components101.com, 2018)
(https://components101.com, 2018)
131
Continued
Pin Pin Name Description Secondary Function
No.
11 PD5 Pin5 of T1(Timer1 External Counter Input)
(T1/OC0B) PORTD
OC0B (PWM - Timer/Counter0 Output
Compare Match B Output)
12 PD6 Pin6 of AIN0(Analog Comparator Positive I/P)
(AIN0/OC0A) PORTD
OC0A (PWM - Timer/Counter0 Output
Compare Match A Output)
13 PD7 (AIN1) Pin7 of AIN1(Analog Comparator Negative I/P)
PORTD
14 PB0 Pin0 of ICP1(Timer/Counter1 Input Capture Pin)
(ICP1/CLKO) PORTB
CLKO (Divided System Clock. The
divided system clock can be output on the
PB0 pin)
15 PB1 (OC1A) Pin1 of OC1A (Timer/Counter1 Output Compare
PORTB Match A Output)
16 PB2 (SS/OC1B) Pin2 of SS (SPI Slave Select Input). This pin is
PORTB low when controller acts as slave.
Continued
Pin Pin Name Description Secondary Function
No.
20 AVCC Power for Internal ADC Converter
21 AREF Analog Reference Pin for ADC
22 GND Ground
23 PC0 (ADC0) Pin0 of ADC0 (ADC Input Channel 0)
PORTC
24 PC1 (ADC1) Pin1 of ADC1 (ADC Input Channel 1)
PORTC
25 PC2 (ADC2) Pin2 of ADC2 (ADC Input Channel 2)
PORTC
26 PC3 (ADC3) Pin3 of ADC3 (ADC Input Channel 3)
PORTC
27 PC4 Pin4 of ADC4 (ADC Input Channel 4)
(ADC4/SDA) PORTC
SDA (Two-wire Serial Bus Data
Input/output Line)
28 PC5 (ADC5/SCL) Pin5 of ADC5 (ADC Input Channel 5)
PORTC
SCL (Two-wire Serial Bus Clock Line)
(https://components101.com, 2018)
Features
CPU 8-bit AVR
Number of Pins 28
Operating Voltage +1.8 V TO +5.5V
(V)
Number of 23
programmable I/O
lines
Communication Master/Slave SPI Serial Interface(17,18,19 PINS) [Can be
Interface used for programming this controller]
Continued
PWM channels 6
External Oscillator 0-4MHz @ 1.8V to 5.5V
Since ATmega328P is used in Arduino Uno and Arduino nano boards, you can
directly replace the arduino board with ATmega328 chip. For that first you need to
install the Arduino bootloader into the chip (Or you can also buy a chip with bootloader
– ATMega328P-PU). This IC with bootloader can be placed on Arduino Uno board and
burn the program into it. Once Arduino program is burnt into the IC, it can be removed
and used in place of Arduino board, along with a Crystal oscillator and other
components as required for the project. Below is the pin mapping between Arduino
Uno and ATmega328P chip. (https://components101.com, 2018)
Arduino
Power Supply
The Arduino Uno power supply can be done with the help of a USB cable or an
external power supply. The external power supplies mainly include AC to DC adapter
otherwise a battery. The adapter can be connected to the Arduino Uno by plugging into
the power jack of the Arduino board. Similarly, the battery leads can be connected to
the Vin pin and the GND pin of the POWER connector. The suggested voltage range
will be 7 volts to 12 volts. (https://www.elprocus.com, 2020)
The 14 digital pins on the Arduino Uno can be used as input & output with the
help of the functions like pinMode(), digitalWrite(), & Digital Read().
(https://www.elprocus.com, 2020)
Pin1 (TX) & Pin0 (RX) (Serial): This pin is used to transmit & receive TTL
serial data, and these are connected to the ATmega8U2 USB to TTL Serial chip
equivalent pins. (https://www.elprocus.com, 2020)
Pin 2 & Pin 3 (External Interrupts): External pins can be connected to activate
an interrupt over a low value, change in value. (https://www.elprocus.com, 2020)
Pins 3, 5, 6, 9, 10, & 11 (PWM): This pin gives 8-bit PWM o/p by the function
of analogWrite(). (https://www.elprocus.com, 2020)
SPI Pins (Pin-10 (SS), Pin-11 (MOSI), Pin-12 (MISO), Pin-13 (SCK): These
pins maintain SPI-communication, even though offered by the fundamental hardware,
is not presently included within the Arduino language. (https://www.elprocus.com,
2020)
Pin-13(LED): The inbuilt LED can be connected to pin-13 (digital pin). As the
HIGH-value pin, the light emitting diode is activated, whenever the pin is LOW.
(https://www.elprocus.com, 2020)
Pin-4 (SDA) & Pin-5 (SCL) (I2C): It supports TWI-communication with the
help of the Wire library. (https://www.elprocus.com, 2020)
AREF (Reference Voltage): The reference voltage is for the analog i/ps with
analogReference(). (https://www.elprocus.com, 2020)
Reset Pin: This pin is used for reset (RST) the microcontroller.
(https://www.elprocus.com, 2020)
136
Memory
Communication
Features
Microcontroller ATmega328P – 8 bit AVR family microcontroller
Operating Voltage 5V
Recommended Input Voltage 7 – 12 V
Input Voltage Limits 6 – 20 V
Analog Input Pins 6 (A0 – A5)
Digital I/O Pins 14 (Out of which 6 provide PWM output)
DC Current on I/O Pins 40 mA
DC Current on 3.3V Pin 50 mA
Flash Memory 32 KB (0.5 KB is used for Bootloader)
SRAM 2 KB
EEPROM 1 KB
Frequency (Clock Speed) 16 MHz
(https://components101.com, 2018)
Arduino Uno can detect the surroundings from the input. Here the input is a
variety of sensors and these can affect its surroundings through controlling motors,
lights, other actuators, etc. The ATmega328 microcontroller on the Arduino board can
be programmed with the help of an Arduino programming language and the IDE
(Integrated Development Environment). Arduino projects can communicate by
software while running on a PC.
137
Arduino Programming
Once the Arduino IDE tool is installed in the PC, attach the Arduino board to
the computer with the help of USB cable. Open the Arduino IDE & select the right
board by choosing Tools–>Board..>Arduino Uno, and select the right Port by choosing
Tools–>Port. This board can be programmed with the help of an Arduino programming
language depends on Wiring. (https://www.elprocus.com, 2020)
To activate the Arduino board & flash the LED on the board, dump the program
code with the selection of Files–> Examples..>Basics..>Blink. When the programming
codes are dumped into the IDE, and then click the button ‘upload’ on the top bar. Once
this process is completed, check the LED flash on the board.
(https://www.elprocus.com, 2020)
PROGRESS CHECK
1. What is the difference between a microcontroller and a microprocessor?
2. Can the ATMega328P Microcontroller be used without the Arduino Board?
Justify your answer in 5-10 sentences only.
3. On the features of the Arduino Board, it says that the operating voltage is 5 volts,
why is it then that the recommended input voltage is 7 – 12 Volts? Justify your
answer in 5-10 sentences only.
REFERENCES