Intro To Assembly Language
Intro To Assembly Language
Assembly Language
By
Rafi Ibn Sultan
Lecturer
Department of CSE
Varendra University
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 specifications
• We use the Microsoft Macro Assembler (MASM)
• Assembly language code is generally not case sensitive, but we use
upper case
Statements
• Programs consist of statements, one per line
• Each statement is either
• An instruction (which the assembler translates into machine code)
• An assembler directive, which instructs the assembler to perform some specific
task
• Both instructions and directives have up to four fields:
name operation operand(s) comment
• An example of an instruction:
START: MOV CX, 5 ;initialize counter
• An example of an assembler directive:
MAIN PROC
Name Field
• The name field is used for:
• Instruction labels
• Procedure names
• Variable names etc.
• The assembler translates names into memory addresses
• Limitations of name field:
• Names can be from 1 to 31 characters long
• may consist of letters, 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
Example of Illegal Names
Name Fields Reasons
• The assembler directive that defines a byte variable takes the following form:
name DB initial_value
(Ex:) ALPHA DB 4
• A question mark (“?") used in place of an initial value sets aside an
uninitialized byte
(Ex: ) BYT 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
Word Variables
• The assembler directive for defining a word variable has the following
form:
WRD DW -2
• 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
Arrays
• 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
• If the assembler assigns the offset address 0200h to B_ARRAY, then
memory would look like this:
Symbol Address Contents
W_ARRAY+2 0302h 40
.ASM
file
.OBJ
file
.EXE
file
The LEA instruction
• For printing a string INT 21h, function 9, expects the offset address of
the character string to be in DX
• We use a new instruction:
• LEA destination, source
LEA DX, MSG
• LEA stands for "Load Effective Address.“
• It puts a copy of the source offset address into the destination
Why We Need to Initialize DS
• When a program is loaded, we need to initialize DS so that DS can contain
the Data Segment Number
• As DS does not contain the segment number of the data segment to correct
this, a program containing a data segment begins with these two
instructions:
MOV AX,@DATA
MOV DS,AX
• @Data is the name of the data segment defined by .DATA
• The assembler translates the name @DATA into a segment number
• Two instructions are needed because a number (the data segment number)
may not be moved directly into a segment register
Assembly Program for Printing a
String