You are on page 1of 6

ACCESSING THE SYSTEM’S HARDWARE

4.0 Outcomes
At the end of this chapter, you should be able to:

1. Explain the 3 methods used for accessing the hardware

2. Use the DOS function calls to access the hardware

4.1 Introduction
You can access the PC system hardware at one of three general levels from assembly
Language.
• Programming the hardware directly
• Use ROM BIOS routines to access the hardware
• Use operating system’s (MS-DOS) calls to access the hardware.
Each level of system access has its own set of advantages and disadvantages.

4.2 Programming the Hardware Directly


1) Programming the hardware directly offers two advantages over the other schemes:
• Control and
• Efficiency.
2) Programming the hardware directly has its drawbacks.
• The needs to create different versions of the same program to work with the
same hardware type (monitor for example) produced by different manufacturers

4.3 Using ROM BIOS


The Basic Input Output System or BIOS provide a hardware-independent interface to various
devices in the IBM PC system. For example, one of the BIOS services is a video display driver.
By making various calls to the BIOS video routines, your software will be able to write
characters to the screen regardless of the actual display board installed. The BIOS allows you to
manipulate devices in a very low level fashion

4.4 Using Operating System (MS-DOS) Calls


The operating system (MS-DOS) provides a high-level interface to many devices. This high-level
interface greatly reduces the amount of effort in accessing the hardware.
Accessing the hardware using BIOS or DOS call requires the use of software interrupts

4.4.1 What is an Interrupt?


1. An interrupt is a signal that tells the CPU to temporarily stop what it is doing and go do
something else.
2. Interrupt can be either External or Internal
3. External interrupts are generated by external hardware devices. While Internal interrupts
are generated by a running program
4. There are 256 different interrupts numbered from 0 to 255.
5. In real mode, the lowest 1024 bytes of memory are reserved for the Interrupt Vector Table
(IVT) containing the addresses for each of the 256 possible interrupts.
4.4.2 Invoking an Interrupt
• A program can invoke any of these interrupts with a special instruction, known as INT, and
is given the number of the interrupt. Thus an INT 21h instruction invokes interrupt
number 33 decimal.
• When an interrupt occurs (hardware or software), the processor multiplies its number by 4
and looks at the resulting memory location to find the address of the piece of code which
handles the interrupt.
• It then places the current address in the program (the value of Instruction Pointer, EIP) and
the processor flags on the stack, and jumps to the beginning of the interrupt handler.
• When the interrupt handler finishes, it invokes a special instruction to return from the
interrupt. This instruction takes the previously saved flags and program address off of the
stack and places them back in the appropriate registers in the CPU.
• The interrupt handler has to be careful to preserve any registers that it uses which are not
used to communicate results to the program that invoked the interrupt.
• If the interrupt can be triggered by a hardware interrupt (only certain ones can on IBM PC's,
XT's, and AT's), then the interrupt handler has to preserve ALL registers, since the interrupt
could have happened anywhere.
4.4.3 MS-DOS Calling Sequence
• MS-DOS is called via the int 21h instruction.
• To select an appropriate DOS function, load the AH register with a function number before
issuing the int 21h instruction. There are 256 DOS functions numbered from 0h to FFh
• Most DOS calls require other parameters as well. Generally, these other parameters are
passed in the CPU's register set. Few DOS functions are explained below.

4.4.3.1 Terminate Program Execution


Function: AH = 4Ch

Entry parameters: AL = Return code


Exit parameters: Does not return to your program
• This is the function call normally used to terminate your program.
• It returns control to the calling process. A return code can be passed to the calling process
in the al register. Exactly what meaning this return code has is entirely up to you.

4.4.3.2 Display a Character String


Function: AH = 09
Entry Parameter: DS:DX = Address of the character string
Description: This function displays a string of characters on the screen. The address of the
string should be in the DS:DX register pair. The string must be terminated with the $. The
string can be of any length and may contain control characters.

4.4.3.3 Buffered Keyboard Input


Function: AH = 0A
Entry Parameter: DS:DX = Address of a keyboard input buffer
Description:
• The first byte of the buffer must be the size of the buffer, which can be up to 255 bytes.
• The second byte is filled by the function when it returns. It contains the number of character
typed.
• The remaining bytes contain the typed characters followed by a carriage return (0D).
This function continues to read the keyboard until either the buffer is filled or the carriage
return (Enter key) is pressed.
The program EX4_1.asm shown in Figure 4.1 is an example of using DOS function calls to
access the hardware. Let’s go through the program.

The first two lines:


mov ax,seg message1 ;get segment of message1
mov ds,ax ; put it in ds
Set the DS register to point to the data segment. This is necessary as the operating system
doesn’t take care of that. The next 3 lines:
mov dx, offset message1 ; get offset of message1
mov ah,09h ; display message1
int 21h

Use the DOS function 09 to displays Message 1, What is your name ?, on the screen.

Notice the dollar sign, $, at the end of message1 in the data section. The next 3 lines:
mov dx,offset nam ; get offset of name
mov ah,0Ah ; get name from user
int 21h

use DOS function A to allow the user to type his/her name which is stored in the buffer nam .
Notice how the buffer was declared in the data section. The first number, 31, is the maximum
number of character that the user can input. The second number 33 should be always be the
first number plus 2 (31+2 = 33). Next, the DOS function 9 is used again to displays
Message2, It is nice to meet you. Finally, DOS function 4C is used to terminate the

program. Note that, you remove these two lines:


mov ax,4c00h ;Returns control to DOS
int 21h ;

and replace them with the .exit directive as shown in Chapter 1.


; EX4_1.ASM
; input the name and display a message
.MODEL SMALL
.stack 200h
.DATA
message1 DB 'What is your name ? $'
nam db 31, 33 dup(0)
message2 db 10,13,'It is nice to meet you $'
.CODE
START:
mov ax,seg message1 ;get segment of message1
mov ds,ax ; put it in ds
mov dx, offset message1 ; get offset of message1
mov ah,09h ; display message1
int 21h
mov dx,offset nam ; get offset of name
mov ah,0Ah ; get name from user
int 21h
mov ah,09h
mov dx,offset message2 ;get offset message2
int 21h ; display message2
mov ax,4c00h ;Returns control to DOS
int 21h ;
END START

Figure 4.1

4.5 Review Questions


1) The advantages of accessing the hardware by direct programming are:
a) Speed and efficiency
b) Control and Speed
c) Control and Efficiency
d) None of the above
2) The Access the hardware using DOS or BIOS, you need to:
a) Use interrupts
b) Use DOS function calls
c) None of the above
3) In real mode, the interrupt table
a) Use 1 Kbytes of memory
b) Has 256 interrupts
c) All of the above
d) None of the above
4) In real-mode, the memory-address range reserved for the interrupt table is?
a) 0000H to 003FFH
b) 00000H to 003FH
c) 00000H to 003FFH
5) List 3 methods of accessing the hardwires
6) List one advantage and one disadvantage of each of the three methods listed in 4 above
7) Write an assembly program that request the user to enter two messages each up to 40
characters then print each message on a separate line
8) What is the difference between DOS functions 6 and 9?
9) Write an assembly program that open for reading a file named Test.txt on the current
directory.
10) Write an assembly program that request the user to type a filename then delete that
file.

You might also like