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 :

;main procedure instructions
;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 in one segment
2. MEDIUM : code in more than one in one segment
3. COMPACT  :  code in one in more than one segment
4. LARGE :  code in more than one in more than one array larger than 64k bytes
5. LARGE :  code in more than one 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 :


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 


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.

Description and Example of Named Constants

EQU (Equates)

To assign a name to a constant, we can use the EQU (equates) pseudo-op. THe syntax is 

name                            EQU                                     constant

For Example :   the statement

LF                         EQU                            0AH

assigns the name LF to 0Ah, the ASCII code of the line feed character. The name LF may now be used in place of 0Ah anywhere in the program. Thus, the assembler translates the instructions

MOV            DL, 0AH



into the same machine instruction

The symbol on the right of an EQU can also be a string 

Example :


Then instead of

MSG       DB                    'TYPE YOUR NAME'

we could say

MSG    DB                    PROMPT

Note : no memory is allocated for EQU names.


Description and Example of Array

** In assembly language, an array is just a sequence of memory bytes or word.

For Example : To define a three-byte array called B_ARRAY, whose initial values are 10h, 20h, and 30h, we can write,

B_ARRAY                  DB                                   10H,20H,30H

The name B_ARRAY  is associated with the first of these bytes, B_ARRAY+1 with the second, and B_ARRAY+2 with the third. If the assembler assigns the offset address 0200h to B_ARRAY, then memory would look like this : 

Symbol                         Address                           Contents

B_ARRAY                            200h                                 10h

B_ARRAY+1                        201h                                 20h 

B_ARRAY +2                       202h                                 30h 

In the same way, an array of words may be defined.

Example : 

W_ARRAY                       DW                                 1000,40,29887,329

sets up an array of four words, with initial value 1000, 40,29887, and 329.
The initial word is associated with the name W_ARRAY, the next one with W_ARRAY+2, the next with B_ARRAY+4, and so on. If the array starts at 0300h, it will look like this : 

Symbol                          Address                           Contents

W_ARRAY                            0300h                                 1000d

W_ARRAY+2                        0302h                                 40d

W_ARRAY +4                       0304                                  29887d

W_ARRAY +6                      0306h                                 329d 

High and low Bytes of a Word

Sometimes we need to refer to the high and low bytes of a word variable.  Suppose we define 

WORD1                        DW                           123H

The low byte of WORLD1 contains 34h, and the high byte contains 12h. The low byte has symbolic address WORD1, and the high byte has symbolic address WORD1+1.

Character Strings

An array of ASCII codes can be initialized with a string of characters.

Example : 

LETTERS                                       DB                           'ABC'

is equivalent to

LETTERS                 DB                    41H,42H,43H

Inside a String the assembler differentiates between upper and lower case. Thus, the string "abc" is translated into three bytes with values 61h,62h, and 63h

It is possible to combine characters and number in one definition;

Example :

MSG     DB                        'HELLO', 0AH, 0DH, 'S'

is equivalent to  

MSG DB                            48H, 45H, 4CH, 4CH, 4FH, 0AH, 0DH, 24H

Description and Example of Program word variable

Word Variables:

The assembler directive for defining a word variable has the following form:

           name D W                            initial_value

the pseudo-op     D W    MEANS "Define word. "for example 

          W R D    D W         - 2

as with byte variables , a question mark in place of an initial value means an uninitialized word . the decimal range of initial values that can be specified is - 32768  to 32767  for a signed interpretation ,or  0 to   65535  for an unsigned interpretation.

Description and Example of Program variable

Variable play the same role in assembly language that they do in high-level languages. Each variable
 has a data type and is assigned a memory address by the program .The data -defining pseudo-op can
 be used to set aside one or more data items of the given type .

                 in this section we use D B and  D W  to define byte variable , word variable ,and arrays of byte ,word are used in chapter 18 in connection with multiple - precision and noninteger operations  

Description and Example of Program Byte variable

Byte variable :    

The assembler directive that defines a byte variable takes the following form:
        name DB    .                   initial_value
where the pseudo-op DB stands for "Define Byte"
             for example ,
This directive causes the assembler to associate a memory byte with the name ALPHA, and initialize it to 4,A question mark("?") used in place of an initial value sets aside an uninitialized byte ; for example ,
       BYTE     DB
The decimal range of initial values that can be specified is -128 to 127 if a signed  interpretation is being given ,or 0 to 255 for an unsigned interpretation .These are the ranges of values that fit in a byte.
**Characters : Characters and character strings must be enclosed in single or double quotes; For example, "A" or 'hello'. Characters are translates into their ASCII codes by the assembler, so there is no difference between using "A" and 4h (the ASCII  code for "A") in a program.

table 4.1   Data-Defining Pseudo-ops

Pseudo-op                              Stands for

DB                                           define byte

DW                                         define word

DD                                         define doubleword  (two consecutive words)

DQ                                         define quadword (four consecutive words) 

DT                                          define tenbytes (ten consecutive bytes)

Description and Example of Program Data

**Numbers : A binary number is written as a bit string followed by the letter "B" or "b"; for example,  1010B

A decimal number must begin with a decimal digit and end with the letter "H" or "h"; For Example, 0ABCH  (the reason for this is that the assembler would be unable to tell whether a symbol such as "ABCH" represents the variable name "ABCH" or the hex number ABC).

Any of the preceding numbers may have an optional sign. Here are examples of legal and illegal numbers for MASM.

Number                                          Type

11011                                              decimal

11011B                                            binary

65223                                              decimal

-21843D                                          decimal

1,234                                                illegal-contains a nondigit character hex

1B4DH                                             illegal hex number-doesn't end in "H"

1B4D                                                 illegal hex number -doesn't begin with                                            
FFFFH                                              a decimal digit

0FFFFH                                            hex

Description and Example of Comment Field

** The comment field of a statement is used by the programmer to say something about  what the statement does. A semicolon marks the beginning of this field, and the assembler ignores anything typed after  the semicolon, Comments are optional, but because assembly language is so low-level, it is almost impossible to understand an assembly language program without comments. In fact, good programming practice dictates a comment on almost every line. The art of good commentary is developed through practice, Don't say something obvious, like this :

MOV CX,0                            ;move   .0 to CX

Instead, use comments to put the instruction into the context of the program :

MOV CX, 0                        ;CX    counts    terms,            initially   0

It is also permissible to make an entire line a comment, and to  use them to create space in a program.

Earn More money From the trusted freelancing sites:

Start freelancing & Earn money 

Thursday, April 21, 2016

Description and Example of Operand Field

For an instruction, the operand field specifies the data that are to be acted on by the operation. An instruction may have zero, one, or two operands 

For Example :

NOP                               no operands : does nothing

INC AX                          the operand; adds 1 to the contents of AX

ADD WORD1, 2              two operands; adds 2 to the contents of memory word WORD1

In a two-operand instruction, the first operand is the destination operand. It is the register or memory location where the result is stored. The second operand is the source operand. The source is usually not modified by the instruction.

For an assembler directive, the operand field usually contains more information about the directive.

Description and Example of Operation Field

For an instruction, the operation field contains a symbolic operation code(opcode). The assembler translates a symbolic opcode into a machine language opcode. Opcode symbols often describe the operation's function;

For Example, MOV, ADD, SUB.

In an assembler directive, the operation field contains a pseudo-operation code(pseudo-op). Pseudo-ops are not translated into machine code;rather, they simply tell the assembler to do something.

For example, the PROC pseudo-op is used to create a procedure

Description and Example of Name Field

The name field is used for instruction labels.procedure names, and variable names. The assembler translates into memory addresses.

Names can be from 1 to 31 characters long, an may consist of letter, digits, and the special characters ? . @ _ $%. Embedded blanks are not allowed. If a period is used, it must be the first character. names may not begin with a digit. The assembler does not differentiate between upper and lower case in a name.

Examples of legal names


Examples of illegal names

TWO WORDS                             contains a blank
2abc                                              begins with a digit
A45.28                                          .not first character
YOU&ME                                     Contains an illegal character

What is Assembly Language?

Assembly Language : Assembly language programs are translated into machine language instructions by an assembler, so they must be written to conform to the assembler's specification.

Statements : Programs consist of statements, one per line. Each statement is either an instruction, which the assembler translate into machine code, an assembler directive, which instructs the assembler to perform some specific task, such as allocating memory space for a variable or creating a procedure. Both instructions and directives have up to four fields.

name          operation             operand(s)            comment

An example of instruction is

START :        MOV CX,5                  ;initialize counter

Here, the name field consists of the label START. The operation is MOV, the operands are CX and 5, and the comment is;initialize counter.

An example of a assembler directive is

MAIN               PROC

MAIN is the name, and the operation field contain PROC. This particular directive creates a procedure called MAIN

Learn More from Blog :  

Expert Web Engineering

Expert Computer Networking

Talent Programming Language C

Logical Discrete Mathematics

Expert Compiler Design

Expert Data Structure

Professional Algorithm Design

Professional Responsive Web Page Design By Bootstrap

Talent Software Engineering