Lecture 4 (Introduction To Assembly)
Lecture 4 (Introduction To Assembly)
Reference: Yu/Marut
Chapter: 1,
Types of Programming Language
• Machine Language:
It contains bit strings. A CPU can only execute machine
language instructions.
• Assembly Language:
It uses symbolic names to represent operations,
registers, and memory locations.
A program written in assembly language must be
converted to machine language before the CPU can
execute it. Assembler translates assembly language
statements into machine language instructions.
• High-Level Language:
It is more like natural language text than assembly
language. Compiler translates high-level language
into machine language instructions.
Assembly vs. HLL
Advantages of HLL Advantages of Assembly
• Easier to write • Closer to Machine code
• Easier to • Access to specific
understand memory location
• Requires fewer • Helps understand how
statements computer actually
• Better control works
• Doesn’t depend on
particular machine
Remember
• ASSEMBLY language program is machine
specific
• ASSEMBLER converts it into machine
code
• Not case-sensitive
Syntax
• Each line of an ASSEMBLY program
contains a statement
• A statement can either be an instruction
or an assembler directive
• Instruction: MOV, SHL, JMP etc
• Assembler Directive: ORG, ASSUME,
END, PROC etc (Pseudo Op-Code)
Syntax
General syntax for a statement is
Name: Instruction Operand(s) ; Comment
Example:
MYLABEL : MOV AX,BX ; move the
contents of BX into AX
Name Field
• Used for instruction labels, procedure
names and variable names
• Assembler translates names into memory
addresses.
• 1 to 31 characters long
• Can contain letters, digits and following
special characters
. ? _ @ $ %
• Period(.) is used at the beginning only
• Blanks are not allowed
• Names may not begin with a digit
Name Field
Examples of legal names: Examples of illegal names:
Numbers:
• Binary: Bit string followed by “B” or “b”. For
example: 1010B, 1000b.
• Decimal: String of decimal digits. May or may not end
with “D” or “d”. For example: 1500, 3500D, 5000d.
• Hex: Must begin with a decimal digit and end with
“H” or “h”. For example: 0ABCH, 58B0h.
Legal & Illegal Numbers
Number Type
11011 Decimal
11011B binary
64223 decimal
-21843D decimal
1,234 Illegal; contains a non-digit character
1B4DH hex
1B4D Illegal hex number, doesn’t end in H
FFFFH Illegal hex number, doesn’t begin with a decimal digit
0FFFFH hex
Characters
• Characters & character strings must be enclosed in
single or double quotes; for example: “A” or ‘hello’.
• Characters are translated into their ASCII codes by
assembler. For example, the ASCII code for “A” is 41 h.
Data-defining Pseudo-ops:
Pseudo-op Stands for
DB Define byte
DW Define word
DD Define doubleword (two consecutive
words)
DQ Define quadword (four consecutive words)
CORRECT
MOV AX, ‘ABCD’
MOV AX,0ABCDH
INCORRECT
MOV AX, ABCD
MOV AX, ABCDH
Variables
Byte 8 bits DB
Word 16 bits DW
Double Word 32 bits DD
Quad Word 64 bits DQ
Ten Bytes 80 bits DT
MYBYTE DB 15
MYWORD DW ?
MYARRAY DB 4,5,6,?,?,?
MYSTRING DB ‘abrakadabra’
Named Constant
• To make assembly code easier to understand, a
symbolic name may be given for a constant quantity.
For this, EQU pseudo-op is used.
Name EQU constant
MYCONSTANT EQU 5
Source Destination
Operand Operand
General Memory
Register Location
General
Register
Memory X
Location
INC AX ; ax++
DEC MYBYTE; mybyte - -
NEG
• Single operand instruction
• NEG destination
• Destination is either a register or a memory
location.
• Replaces the contents by 2’s complement.
• Updates all flags
CF=1 unless result is 0
OF=1 if word destination is 8000h or byte
destination is 80h
• NEG AX
Type Agreement of Operands
• The operands of preceding two-operand
instructions must be of the same type;
both bytes or both words.
ORG 1000H
ORG $+1000H