Monday, April 25, 2016

Description of INT 21h

** INT 21h may be used to invoke a large number of DOS functions ; a particular function is requested by placing a function number in the AH register and invoking INT 21h. Here we are interested in the following functions :


Function Number                    Routine

1                                            Single-key input

2                                            Single-Character Output

9                                            Character String Output

INT 21h functions expect input values to be in certain registers and return output values in other registers. There are listed as we describe each function.

Function 1:

Single-Key input

Input : AH = 1
output :  AL = ASCII code if character key is pressed
             = 0 if non-character key is pressed

To invoke the routine, execute these instructions :

MOV  AH,1       ;input key function
INT 21h              ;ASCII code in AL

The processor will wait for the user to hit a key if necessary. if a character key is pressed, AL gets its ASCII code; the character is also displayed on the screen. If any other key is pressed, such as an arrow key, F1-F10, and so on, AI, will contain 0. The instructions following the INT 21h can examine AL and take appropriate action.

Because INT 21h, function 1, doesn't prompt the user for input, he or she might not know whether the computer is waiting for input or is occupied by some computation. The next function can be used to generate an input prompt.


Function 2 :

Display a character or execute a control function

Input :             AH   = 2
                       DL   =    ASCII code of the display character or control character

Output  :   AL  = ASCII code of the display character or control character

What is Code Segment? With Example

Code Segment : The code segment contains a program's instructions. The declaration syntax is

.CODE           name

Where name is the optional name of the segment(there is no need for a name in a SMALL program, because the assembler will generate an error).

Inside a code segment, instructions are organized as procedures. The simplest procedure definition is

name    PROC
;body   of   the   procedure
name    ENDP


where name is the name of the procedure, PROC and ENDP are pseudo-ops that delineate the procedure.


Here is an example of a code segment definition :

.CODE
MAIN PROC
;main procedure instructions
MAIN ENDP
;other procedure go here  

What is STACK Segment? With Example

STACK Segment : The purpose of the STACK Segment declaration is to set aside a block of memory (the stack area) to store the stack. The stack area should be big enough to contain the stack at its maximum size. The declaration syntax is 

.STACK                size

Where size is an optional number that specifies the stack area size in bytes.

Example :

.STACK          100H

Sets aside 100h bytes for the stack area(a reasonable size for most applications). Is size is omitted, 1KB is set aside for the stack area.

What is Data Segment? With Example

Data Segment : A program's data segment contains all the variable definitions. Constant definitions are often made here as well, but they may be placed elsewhere in the program since no memory allocation is involved. To declare a data segment, we use the directive. DATA, followed by variable and constant declarations.

Example :

.DATA                  DW 2
WORD1                DW 5
WORD2                DB 'THIS IS A MESSAGE'
MASK                   EQU 10010010B

Write down the memory model



1. SMALL : code in one segment.data in one segment
2. MEDIUM : code in more than one segment.data in one segment
3. COMPACT  :  code in one segment.data in more than one segment
4. LARGE :  code in more than one segment.data in more than one segment.no array larger than 64k bytes
5. LARGE :  code in more than one segment.data in more than one segment. array may be  larger than 64k bytes.


Description and Example of MOV and XCHG

** The MOV instruction is used to transfer data between registers, between a register and a memory location, or to move a number directly into a register or memory location.

The syntax is -

MOV destination,source
                              -


Here are some examples :

MOV AX,WORD1

This reads "MOVE WORD1 to AX". The contents of register AX are replaced by the contents of memory location WORD1. The contents of WORD1 are unchanged. In other words, a copy of WORD1 is sent to AX 

MOV  AX,BX

AX gets what was previously in BX.BX is unchanged.

MOV   AH,  'A'.

Description and Example of A Few basic Instructions

** There are over a hundred instructions in the instruction set for the 806 CPU; there are also instructions especially for the more advanced processors. In this section we discuss six of the most useful instructions for transferring data and doing arithmetic. The instructions we present can be used with either byte or word operands.

In the following, WORD1 and WORD2 are word variables, and BYTE1 and BYTE2 are byte variables. Recall from chapter 3 that AH is the high byte of register AX, and BL is the low byte of BX.