You are on page 1of 6

Class Note

CS 250

Part III: Introduction to Assembly Language

1
1. Why Assembly Language?
 Complete control over the processor
 Space efficiency
 Faster running (execution)
 System programs
 Communication programs

2. Four steps involved in Assemble Language Programming


1) entering the program (source code)
2) assembly the program (object file)
3) linking the program (executed file)
4) loading the program (executable program in memory)

Figure 1: The four images of an assembly language program

3. Program Source Code


3.1 Format of an assembly language statement
{identifier} Keyword {{parameter}} {;comment}
 identifier: is a programmer-supplied token much like a variable name in a higher-level
language. Identifier is composed of the letters, the digits and some special characters.
The first character cannot be one of digits 0 through 9. If the keyword is an
instruction, then the identifier is called label.
 Keyword: If the statement is an assembly language instruction, then the keyword is the
instruction op code. If the statement is a directive, then the keyword is the title of that
directive.
The directives are used in a program to describe the context in which the instructions
are to be assembled into machine instructions and in which the data allocation
statements are to be processed into data space.
 Parameters: If the keyword is an instruction, then the parameters are the operand. If the
keyword is a directive, then the parameters are the attributes of that directive.
 Comment: is a string of text that stores only as internal documentation for a program. A
semicolon identifiers all subsequent text in a statement as a comment.

4. An example of listing of assembly program

2
basic structure of assembly program

4.1 Some directive statements


.MODEL SMALL allow to use Pentium instruction
.586
.STACK 100h : is a segment directive which defines 100h words as program STACK.
The linker sets the values of SS and SP.
.DATA : is a segment directive, followed by one or more data allocation directives to
define the variable and constant used by program.
Note: The data segment will be set by program
mov ax, @data

3
mov ds, ax
(to be discussed shortly)

.CODE : the code segment directive to set CS


.END identifier : the end of program directive. The identifier is the place where the
program to start to run.

4.2 Data allocation statements


(identifier) (keyword) (parameters)
varname data-defination-type {int {{,int}}}
where data-definition-type is DB (define Byte)
DW (define Word)
DD (define Double word)
DQ (define Quad word)
Example:
Message DB ‘hello, my name is Bill Jones’, 13, 10, ‘$’
which allocate a block of memory 31 bytes long, initializes it to the text string ‘hello, my
name is Bill Jones’, 13, 10, ‘$’, and assigns the identifier Message to it.

In general
(1) a character can be defined by enclosing paired single (‘) or double (“) or its ASCII
code value)
(2) a string of character (text) is defined by the text enclosed by a paired (‘) or (“) and
must be ended by $
(3) the identifier is the name of the first memory allocation
Abte DB 12, 99, 20
Defines a memory allocation of 3 bytes. The Abte is the variable name whose initial
value is 12.
(4) DUP operator defines a number of same value allocations
For example, Ones DB 5 DUP (1)
Defines 5 bytes whose initial values are 1.

Procedure directive: A procedure is a section of a program whose beginning is

4
defined with a PROC directive and whose termination is defined by an ENDP
directive:
Proc name PROC

Proc name ENDP
For example:
Hello PROC

Hello ENDP

4.3 Instruction Statement


(identifier) (keyword) (parameter) (;comment)
{label:} instruction op code {operand{,operand}} {;comment}

 The MOV instruction


MOV destination, source

Note:
1) both source and destination can not be memory referenced addresses
2) if the destination is a segment register, then source can not be an immediate value
(constant)
For example MOV ax @data is not allowed which should be replaced by following
two instructions:
MOV ax @data
MOV ds ax
where, @data is an immediate value after assembler
3) OFFSET is an operator whose result is an immediate value:
OFFSET MESSAGE is the distance in bytes from the beginning of the data
segment to the first byte in MESSAGE

 The INT 21h instructions


INT 21h defines more than 100 DOS functions (DOS function calls) for most input,
output and other essential machine functions. The contents of the AH register are used
to specify the function to be invoked. When INT 21h is involved, it directs program
flow to that function, and then return to the program after the function is done.

5
Example:
DOS function 09h: display a string of characters whose offset is specified by DX.
AH : 09h
DX : offset of MESSAGE

DOS function 4ch:return control to DOS

The following is a list of other most useful DOS function.

1) DOS function 08h: input a key to AL.


MOV AH, 08h
INT 21h
Note: the Dos functions do not protect AX register. You should save the input in
AL before calling another DOS function.
2) DOS function 01h: input a key to AL and display the key.
MOV AH, 01h
INT 21h
3) DOS function 02h: display a character in DL.
MOV AH, 02h
MOV DL, xxx ; the character is in xxx
INT 21h
4) DOS function 05h: print out a character in DL
MOV AH, 05h
MOV DL, xxx ; the character is in xxx
INT 21h
5) DOS function 0Ah: input a string of characters into an array (array_1).
MOV AH, 0Ah
MOV DX, offset array_1
INT 21h

You might also like