Documente Academic
Documente Profesional
Documente Cultură
Assembly Languages
Label
Assembler
• A computer will not understand any program written in a language, other
than its machine language. The programs written in other languages must
be translated into the machine language. Such translation is performed
with the help of software. A program which translates an assembly
language program into a machine language program is called an
assembler. If an assembler which runs on a computer and produces the
machine codes for the same computer then it is called self assembler or
resident assembler. If an assembler that runs on a computer and produces
the machine codes for other computer then it is called Cross Assembler.
• Assemblers are further divided into two types: One Pass Assembler and
Two Pass Assembler. One pass assembler is the assembler which assigns
the memory addresses to the variables and translates the source code into
machine code in the first pass simultaneously. A Two Pass Assembler is the
assembler which reads the source code twice. In the first pass, it reads all
the variables and assigns them memory addresses. In the second pass, it
reads the source code and translates the code into object code.
Linker & Loader
• Linker
A linker or link editor is a computer program that
takes one or more object files generated by
a compiler and combines them into a
single executable program.
• Loader
A “Loader” is a program which accepts the object
program as input, makes them executable by the
computer and initiates execution.
Assembler
• Design and Implementation of
Assembler
Source Object
Assembler Linker
Program Code
Executable
Code
Loader
Advantages & Disadvantages of Assembly Language
• Advantages Assembly Language:
1.The symbolic programming of Assembly Language is easier
to understand and saves a lot of time and effort of the
programmer.
2.It is easier to correct errors and modify program
instructions.
3.Assembly Language has the same efficiency of execution as
the machine level language. Because this is one-to-one
translator between assembly language program and its
corresponding machine language program.
• Disadvantages Assembly Language:
1.One of the major disadvantages is that assembly language is
machine dependent. A program written for one computer
might not run in other computers with different hardware
configuration.
Functions of a Basic Assembler
• Convert mnemonic operation codes to
machine language equivalents
• Convert symbolic operands to machine
addresses (pass 1)
• Build machine instructions
• Convert data constants to internal
representations
• Write the object program and assembly listing
files
Basic Elements of Assembly Language
• Integer Constants
1/2002 JNM 11
Basic Elements of Assembly Language
1/2002 JNM 12
Basic Elements of Assembly Language
• Reserved Words
– Instruction Mnemonics
– Directives
– Attributes (BYTE or WORD)
– Operators
– Predefined Symbols
1/2002 JNM 14
Examples
• Mystring db “this is a string”,0
– Mystring is the name
• Length = $ - mystring
– Length is a reserved word (can not use)
• Loop1:
– Loop1 is the label for a loop instruction
1/2002 JNM 15
Assembly Language
Statements
1/2002 JNM 16
General Format of a Statement
[name] [mnemonic] [operands] [;comment]
1/2002 JNM 18
Standard Assembler Directives
Directive Description
title Title of the listing file
.model Specify the program's memory model
.stack Set the size of the stack segment
.data Mark the beginning of the data segment
.code Mark the beginning of the code segment
proc Begin procedure
endp End of procedure
end End of program assembly
1/2002 JNM 19
Instructions
• A statement that is executed by the processor
at runtime
• Fall into five general categories
– Data transfer (mov ax, 5)
– Arithmetic (add ax, 20)
– Transfer of control (call MySub)
– Logical (Jz next1)
– Input/output (In al, 20)
1/2002 JNM 20
General Format of an Instruction
Label: Mnemonic Operand(s) ;Comment
• Label (optional)
• Instruction mnemonic (required)
• Operand(s) (usually required)
• Comment (optional)
1/2002 JNM 21
Labels
• An identifier that acts as a place marker for either instructions or
data
• Used to transfer program execution to a labeled instruction
• Act as place markers
– marks the address (offset) of code and data
• Data label
– must be unique
– example: count DWORD 100 (not followed by colon)
• Code label
– target of jump and loop instructions
– example: target: (followed by colon)
Mov ax, bx
….
jmp target
1/2002 JNM 22
Instruction Mnemonics
• A short word that identifies the operation
carried out by an instruction
Mov assign one value to another
Add add two values
Sub subtract one value from another
Call call a procedure
1/2002 JNM 23
Formatting of Instructions and
Number of Operands Needed
Mnemonic destination operand, source operand
1/2002 JNM 24
Instruction Examples
• No operands
stc ; set carry flag
• One operand
inc eax ; increment register eax
call Clrscr ; call procedure Clrscr
jmp L1 ; jump to instruction with label L1
• Two operands
add ebx, ecx ; register ebx = ebx + ecx
sub var1, 25 ; memory variable var1 = var1 - 25
• Three operands
imul eax,ebx,5 ; register eax = ebx * 5
Operands
• A memory operand is specified by either the name of
a variable or a register that holds the address of a
variable.
• A variable name implies the address of the variable;
96 constant operand
2+4 constant expression operand
Eax register operand
Count variable operand
1/2002 JNM 26
Comments
• Comments are good!
– explain the program's purpose
– when it was written, and by whoms
– revision information
– tricky coding techniques
– application-specific explanations
• Single line comments begin with a semicolon
; Words after the semicolon are comments.
; Comments may begin anywhere on a line
• Multi-line comments
Block comments begin with the comment directive a user-specified
symbol
COMMENT &
This line is a comment.
So is this line. The ampersand symbol is personal choice.
&
1/2002 JNM 27