introduction to assembly language(chapter 1)
TRANSCRIPT
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
1/27
Computer Organization &Assembly Languages
Introduction to Assembly Language
Chapter 1
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
2/27
Assembly Language
Sometimes referred to
as assemblyor ASL, assembly languageis a
low-levelprogramming language used to
interface with computer hardware. Assemblylanguage uses structured commands as
substitutions for numbers allowing humans to
read the code easier than looking at binary.
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
3/27
Basic Components in Assembly
Language
Instruction, Directive, Label, and Comment;NUMOFF.ASM: Turn NUM-LOCK indicator off.
.MODEL SMALL
.STACK
.CODE
.STARTUP
MOV AX,40H ;set AX to 0040H
D1: MOV DS,AX ;load data segment with 0040H
MOV SI,17H ;load SI with 0017H
AND BYTE PTR [SI],0DFH ;clear NUM-LOCK bit
.EXIT
END
Comments
Assembly directive
Instructions
Assembly directive
Label
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
4/27
Assembler
A computer will not understand any program written in a language, otherthan its machine language. The programs written in other languages mustbe translated into the machine language. Such translation is performedwith the help of software. A program which translates an assemblylanguage program into a machine language program is called anassembler.If an assembler which runs on a computer and produces the
machine codes for the same computer then it is called self assembler orresident assembler. If an assembler that runs on a computer and producesthe machine codes for other computer then it is called Cross Assembler.
Assemblers are further divided into two types: One Pass Assembler andTwo Pass Assembler. One pass assembler is the assembler which assignsthe memory addresses to the variables and translates the source code intomachine code in the first pass simultaneously. A Two Pass Assembler is theassembler which reads the source code twice. In the first pass, it reads allthe variables and assigns them memory addresses. In the second pass, itreads the source code and translates the code into object code.
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
5/27
Linker & Loader
Linker
A linkeror link editoris a computer program that
takes one or more object files generated by
a compiler and combines them into asingle executable program.
Loader
ALoader
is a program which accepts the objectprogram as input, makes them executable by the
computer and initiates execution.
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
6/27
Assembler
Design and Implementation ofAssembler
Source
Program Assembler
Object
Code
Loader
Executable
Code
Linker
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
7/27
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
8/27
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
9/27
Advantages & Disadvantages of Assembly Language
Advantages Assembly Language:
1.The symbolic programming of Assembly Language is easierto 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.
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
10/27
Functions of a Basic Assembler
Convert mnemonic operation codes tomachine 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
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
11/27
1/2002 JNM 11
Basic Elements of Assembly Language
Integer Constants
If no radix is given, the integer is assumed to bedecimal.
Int 21h Int 21
A hexadecimal constant beginning with a letter
must have a leading 0
Mov ax, 0A3C9h
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
12/27
1/2002 JNM 12
Basic Elements of Assembly Language
Character and String Constants
A single or a string of characters
Enclosed in either single or double quotes
Embedded quotes are permittedAn embedded quote within quotation marks
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
13/27
1/2002 JNM 13
Basic Elements of Assembly Language
Reserved Words
Instruction Mnemonics
Directives
Attributes (BYTE or WORD)
Operators
Predefined Symbols
A complete list of MASM reserved wordsare in Appendix D (4thEdition)
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
14/27
1/2002 JNM 14
Names
(Identifiers)
Used to identify variables, constants, procedures, orcode labels. Max 247 characters
Not case-sensitive
First character can be a letter, _, $, @ Subsequent characters may also be digits.
Cannot use an assembler reserved word.
Should make identifier names descriptive and easy to
understand Avoid @ as first character.
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
15/27
1/2002 JNM 15
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
Note difference between data names and code
labels in the data section (no colon after the name)
In the code section (colon after the name)
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
16/27
1/2002 JNM 16
Assembly Language
Statements
Generally fall into two classes:
Instructions- Executable StatementsDirectivesstatements that provide
information to the assembler
about how to generateexecutable code
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
17/27
1/2002 JNM 17
General Format of a Statement
[name] [mnemonic] [operands] [;comment]
Statements are free-formthey can be
written in any column with any number ofspaces between each operand
Blank lines are permitted
Must be written on a single line and notpass column 128 (use / for continuation of
line)
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
18/27
1/2002 JNM 18
Directives
A statement that is recognized and acted upon by
the assembler as the programs source code isbeing assembled.
Used for defining logical segments, choosing amemory module, defining variables, creatingprocedures,
.data (directive to identify the area of a programthat contains the data variables)
Count db 50 (db is a directive that tells theassembler to create storage for a byte namedcount and initialize it to 50.
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
19/27
1/2002 JNM 19
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
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
20/27
1/2002 JNM 20
Instructions
A statement that is executed by the processorat 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)
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
21/27
1/2002 JNM 21
General Format of an Instruction
Label (optional)
Instruction mnemonic (required)
Operand(s) (usually required)
Comment (optional)
A source code line may have only a label orcomment.
Label: Mnemonic ;CommentOperand(s)
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
22/27
1/2002 JNM 22
Labels
An identifier that acts as a place marker for either instructions ordata
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
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
23/27
1/2002 JNM 23
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
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
24/27
1/2002 JNM 24
Formatting of Instructions and
Number of Operands Needed
Mnemonicdestination operand, source operand
HLT ; zero operands
INC AX ; one operandMOV AX, 100 ; two operands
SHLD DX, AX, 4 ; three operands
Different instructions use different numbers of operands as shown above.
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
25/27
No operandsstc ; set carry flag
One operandinc eax ; increment register eax
call Clrscr ; call procedure Clrscrjmp L1 ; jump to instruction with label L1
Two operandsadd ebx, ecx ; register ebx = ebx + ecx
sub var1, 25 ; memory variable var1 = var1 - 25 Three operands
imul eax,ebx,5 ; register eax = ebx * 5
Instruction Examples
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
26/27
1/2002 JNM 26
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
-
8/11/2019 Introduction to Assembly Language(Chapter 1)
27/27
1/2002 JNM 27
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 commentsBlock comments begin with the comment directive a user-specified
symbolCOMMENT &This line is a comment.So is this line. The ampersand symbol is personal choice.
&