Download Lecture 05 - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lesson 05
ASSEMBLY LANGUAGE
FUNDAMENTALS
1
Assembly Language Fundamentals
•
•
•
•
•
•
•
•
Contents
Basic Elements of Assembly Language
Assembling
Linking
Running Programs
Defining Data
Symbolic Constants
Real-Address Mode Programming
2
BASIC ELEMENTS OF ASSEMBLY LANGUAGE









Integer constants
Integer expressions
Character and string constants
Reserved words and identifiers
Directives and instructions
Labels
Mnemonics and Operands
Comments
Examples
3
BASICS OF ASSEMBLY LANGUAGE
• Assembly Language code is NOT case sensitive but use of upper
case is preferred.
• Mnemonic
• All instructions are represented in numbers by processor.
Mnemonic is easy word (symbol) used instead of instruction,
which will be converted into numbers.
• Assembler
• Convert mnemonic into numbers (instructions)
4
ASSEMBLY LANGUAGE SIMPLE PROGRAM
Write a simple program in assembly language which show
character ‘A’. (newFile.asm)
.model small
.stack 256
.code
start:
mov dl, ‘A’
mov ah, 2h
int 21h
mov ax, 4c00h
int 21h
end start
5
INTEGER CONSTANTS
• Integer constants: (Also called Integer Literal)
• Elements within […] are optional.
• Elements within {…} require a choice of one of the enclosed
elements (Separated by | sign)
• Elements in italics have known description.
• It is made up of three parts
1. Optional leading + or – sign
2. One or More digits
3. Optional Suffix indicating base (Called Radix)
• Syntax:
• [{+|-}] digits [radix]
6
INTEGER CONSTANTS
• Radix:
• It is used to denote base of number.
• By default base is Decimal.
• Radix may be binary(b), decimal(d), hexadecimal(h), or octal
digits(o or q).
• Examples: 30d, 6Ah, 42, 1101b
• A hexadecimal constant beginning with a letter must have
leading zero to prevent the assembler from interpreting it as an
identifier.
• 0A5h
7
INTEGER EXPRESSIONS
• Integer Expressions:
• Mathematical expressions involving integer values and
arithmetic operators.
• Can be stored in 32 bits (0 to FFFFFFFFh)
• Arithmetic Operators:
Operator
Name
Precedence Level
()
Parentheses
1
+, -
Unary plus, Minus (+5 -6)
2
*, /
Multiply, Divide
3
MOD
Modulus
3
+,-
Add, subtract
4
8
REAL NUMBER CONSTANTS
• Real Number Constants:
• Include decimal reals or (encoded) hexadecimal reals.
• Syntax:
• [sign]integer.[integer][exponent]
• Example:
• 2.
• +3.0
• -44.2E+05
• 26.E5
• Note: At least one digit and a decimal point are
required.
9
CHARACTER CONSTANTS
• A character constant is a single character enclosed in
single or double quotes.
• MASM stores the value in memory as the character’s
binary ASCII code.
• Example
• ‘A’
• ‘B’
• ‘C’
10
STRING CONSTANTS
• A string constant is a sequence of characters (including
spaces) enclosed in single or double quotes.
• Example
• 'ABC'
• 'X'
• "Good night, Gracie"
• '4096'
• Embedded quotes are permitted when used in the manner
shown by the following examples:
• "This isn't a test"
• 'Say "Good night," Gracie'
11
RESERVED WORDS
• Reserved words have special meaning in MASM and can
only be used in their correct context.
• There are different types of reserved words:
• Instruction mnemonics, such as MOV, ADD, and MUL
• Register names, such as AH, CR0, EAX, EBX.
• Attributes, which provide size and usage information for variables
and operands such as, BYTE and WORD
• Operators, used in constant expressions such as +, -.
• Predefined symbols, such as @data, which return constant integer
values at assembly time
12
IDENTIFIERS
• Identifier is a programmer chosen name.
• It might identify a variable, a constant, a procedure, or a code
label.
• Keep the following in mind when creating identifiers:
• They may contain between 1 and 247 characters.
• They are not case sensitive.
• The first character must be a letter (A..Z, a..z), underscore (_), @ ,
?, or $. Subsequent characters may also be digits.
• An identifier cannot be the same as an assembler reserved word.
• Eg. var1, count, $first, _main, MAX, _1234
13
DIRECTIVES
• It is a command embedded in the source code, used by assembler.
• Not case sensitive
• Do not execute at run time.
• Perform the work of assigning names to memory segments and other tasks for
MASM.
• Assembler for Intel processors share the same instruction set, but different
directives.
• Directives play a role to define program sections or segments.
• .data (It identifies the area of a program containing variables.)
• .Code (Identifies the area of a program containing executable instructions.
• .STACK 100h (Identifies area of the program holding the runtime stack
and its size.
• The DWORD directive tells the assembler to reserve space in the program for a
double word variable.
• myVar DWORD 26 ;
DWORD directive
14
INSTRUCTIONS
• Statement that becomes executable when a program is
assembled. (Run time)
• Assembler translate instructions into machine language, they
are loaded and executed by CPU at runtime.
• Parts of instruction
1.
2.
3.
4.
Label (optional)
Instruction Mnemonic (required)
Operand(s) (usually required)
Comments (optional)
• Syntax
• [label:] mnemonic [operands] [;comment]
• The MOV instruction, executes at runtime, copying the
contents of myVar to the EAX register:
• mov eax,myVar ;
MOV instruction
15
INSTRUCTIONS (1. LABEL)
• Label is an identifier that acts as a place marker for
instructions and data.
• Label placed before instructions denotes instruction’s address.
• Label placed before variable denotes variable address.
16
INSTRUCTIONS (1. LABEL)
• Data Labels:
• Identifies the location of the variable (Unique)
• Example: count DWORD 100
• Assembler assigns a numeric address to each label.
• Multiple data items label
• It is possible to define multiple data items following a label.
• Array defines the location of the first number (1024)
• Other numbers following in memory immediately afterward.
• Array
DWORD
1024, 2048
DWORD
4096, 8192
(Ends without colon)
17
INSTRUCTIONS (1. LABEL)
• Code Labels:
• A label in the code area of a program must end with colon (:) character.
• Code area: Where instructions are located.
• Code Labels are used as target of jumping and looping instructions.
• E.g.
Target:
mov ax,bx
….
Jump target
(Ends with colon)
; jump instruction transfer the control to label target
 Note: Within any procedure, name of label must be unique.
 Procedure is equivalent to function in modern languages.
 Naming rules are applied on label.
18
INSTRUCTIONS (2. MNEMONIC)
• Mnemonic: A device that assist memory. It is a word used
instead of simple instructions.
• There are four types of instructions:
1. Data Movement Instructions
• Move data from one place to another
• Eg: Data Movement
– Add
ax,
1234
; AND 1234 with ax
– Add
bx,
0534
; ADD 0534 to bx
– Add
bx,
[1200] ; ADD data at address 1200 to bx
– Add
ax,
[1234] ; ADD data from address 1234 to ax
19
INSTRUCTIONS (2. MNEMONIC)
2. Program control instruction
• Controls pointer of instruction. As, sometime sequence of
execution is executed and in between if new sequence of
instruction needs to be executed than this is the job of program
control instruction.
• Eg:
• cmp
• jne
• Jne
ax, 0
;
1234
;
[1234] ;
compare ax with 0
jump if not equal
jump if not equal to 1234 address
20
INSTRUCTIONS (2. MNEMONIC)
3. Arithmetic / Logic Instructions
• Add, sub, mul, div, and, or, not,etc
• Eg: Arithmatic
• Mov ax,
• Lda 0234
bx
; Move data from bx to ax
; Load 0234 into accumulator
4. Special instructions
• These are not common, used to get direct access of
microprocessor.
• Eg:
• Cli
;
• Sti
;
Clear the interrupt flag
(means no interrupt is allowed)
Set the interrupt flag (allow interrupts to processor)
21
INSTRUCTIONS (3. OPERANDS)
• Assembly language instructions can have between zero and
three operands.
• Operands can be:
• Register, memory operand, constant expression, input output
port.
• Memory Operand can be specified by:
• Name of variable
• Address of a variable(placed in one or more registers)
Example
Operand Type
96
Constant (Immediate Value)
2+4
Constant Expression
Eax
Register
22
INSTRUCTIONS (3. OPERANDS)
• Examples of instructions having operands
• Example:
 STC statement without operand
stc
; set Carry flag
 INC statement has one operand:
inc
eax
; add to EAX
 MOV instruction has two operands:
mov count, ebx
; move EBX to count
23
INSTRUCTIONS (3. OPERANDS)
• In two-operand instruction:
• First operand is called the destination. (modified by instruction)
• Second operand is called the source.
• In three-operand instruction:
• First is called as destination, remaining two are called as source.
imul
eax, ebx, 5
In above instruction EBX is multiplied by 5 and the product is stored
in EAX register.
24
INSTRUCTIONS (4. COMMENTS)
• Comments are description of the code written in assembly
language, ignored by assembler.
• Purpose of comments
•
•
•
•
Description of the program’s purpose
Name of programmer
Program dates
Technical details
• Comments could be of following type:
• Single-line comments: Beginning with a semicolon character (;).
• Block comments: Starts with COMMENT directive and a userspecified symbol (e.g. & or !).
25
INSTRUCTIONS (4. COMMENTS)
• For Example:
COMMENT !
THIS IS A FIRST LINE OF COMMENT.
THIS IS SECOND LINE OF COMMENT.
!
OR
COMMENT &
THIS IS A FIRST LINE OF COMMENT.
THIS IS SECOND LINE OF COMMENT.
&
26