COAL Lec 4 - Intoduction To Assembly Language - Chap 4
COAL Lec 4 - Intoduction To Assembly Language - Chap 4
LANGUAGE
1 02/25/2021
Assembly Language Syntax
2 02/25/2021
RULES
instruction
translated into machine code
assembler directive
instructs the assembler to perform some specific
task
3 02/25/2021
Program Statement
Examples:
MAIN PROC
4 02/25/2021
Name Field
• This field is used for:
procedure names
variable names.
5 02/25/2021
Name Field
Assembler translates names into memory addresses.
Embedded blanks are not allowed, names may not begin with
a digit, period (if used) must be the first character
6 02/25/2021
Name Field
Examples:
Legal names Illegal names
COUNTER1 2ABC
@CHARACTER TWO WORDS
$500 A45.26
SUM_OF_DIGITS YOU&ME
.TEST
DONE?
7 02/25/2021
Operation Field
For an instruction
8 02/25/2021
Operation Field
For an assembler directive
9 02/25/2021
Operand Field
For an instruction
• This field specifies data to be acted on. It may have one,
two or no operands at all.
10 02/25/2021
Operand Field
11 02/25/2021
Comment Field
12 02/25/2021
Key rules for the use of comments
13 02/25/2021
Comment Field
14 02/25/2021
Numbers
• Binary number is written as a bit string followed by the
letter `b`.
number type
1010 decimal
1010B binary
-2134D decimal
ABFFH illegal
0ABFFH hex
1BHH illegal
1BFFH hex
1,23 illegal
16 02/25/2021
Characters
17 02/25/2021
Variables
Declaring Integer Variables:
directives .
18 02/25/2021
Variables
19 02/25/2021
Syntax
20 02/25/2021
Variables
21 02/25/2021
Byte variables
• Syntax:
Name DBinitial value
Examples:
ALPHA DB4
BYT DB?
22 02/25/2021
Word variables
• Syntax:
Name DW initial value
Example:
WRD DW -2
• The assembler stores integers with the least
significant byte in the lowest address of the memory
area allocated to the integer
Example:
WD DW 1234H
low byte WD contains 34h, high byte contains 12h
23 02/25/2021
Array Declaration
24 02/25/2021
Array Examples
25 02/25/2021
Array Examples
26 02/25/2021
Character strings
27 02/25/2021
Character strings
Examples:
1)
LETTERS DB‘AaBCbc‘
Is equivalent to
LETTERS DB41H,61H,42H,43H,62H,63H
2)
MSG DB‘ABC‘,0AH,0DH,‘$‘
Is equivalent to
MSG DB41H,42H,43H,0AH,0DH,24H
28 02/25/2021
Constant Declaration
29 02/25/2021
Constant Declaration
Examples:
1)
LF EQU 0AH ; LF can be used in place of 0Ah
MOV DL LF
MOV DL 0AH Have the same machine code
2)
PMT EQU ‘TYPE YOUR NAME‘ ;
instead of
We can use
MSG DB PMT
30 02/25/2021
BASIC INSTRUCTIONS
31 02/25/2021
MOV instruction
32 02/25/2021
Syntax
33 02/25/2021
Mov AX , WORD1
Before After
0006 0008
AX AX
0008 0008
WORD1 WORD1
34 02/25/2021
MOV AX , BX
35 02/25/2021
’MOV AH , ‘A
36 02/25/2021
XCHG instruction
the contents of
– two registers, or
– a register and a memory location
37 02/25/2021
Syntax
38 02/25/2021
Example
XCHG AH , BL
39 02/25/2021
XCHG AH , BL
Before After
1A 00 05 00
AH AL AH AL
00 05 00 1A
BH BL BH BL
40 02/25/2021
Example
XCHG AX , WORD1
41 02/25/2021
Restrictions on MOV & XCHG
MOV Destination Operand
Source General Segment Memory Constant
Operand Register Register Location
General
Register yes yes yes no
Segment
Register yes no yes no
Memory
Location yes yes no no
Constant yes no yes no
42 02/25/2021
Restrictions on MOV & XCHG
XCHG Destination Operand
Source General Memory
Operand Register Location
General
Register yes yes
Memory
Location yes no
43 02/25/2021
Restrictions on MOV & XCHG
Example :
LEGAL:
MOV AX , WORD2
MOV WORD1 , AX
44 02/25/2021
ADD & SUB
– two registers,
– a register & memory location , or
– add ( subtract ) a number to ( from ) a register or a
memory location.
45 02/25/2021
Syntax
46 02/25/2021
Example
ADD WORD1 , AX
47 02/25/2021
ADD WORD1 , AX
Before After
01BC 01BC
AX AX
0523 06DF
WORD1 WORD1
48 02/25/2021
Example
SUB AX , DX
49 02/25/2021
SUB AX , DX
Before After
0000 FFFF
AX AX
0001 0001
DX DX
50 02/25/2021
Example
ADD BL , 5
51 02/25/2021
Legal combinations of operands for
ADD & SUB
Destination operand
Memory location General Register Source Operand
52 02/25/2021
ILLEGAL
53 02/25/2021
INC ( increment )
54 02/25/2021
DEC ( decrement )
55 02/25/2021
Syntax
INC destination
DEC destination
56 02/25/2021
Example
INC WORD1
57 02/25/2021
INC WORD1
Before After
0002 0003
WORD1 WORD1
58 02/25/2021
Example
DEC BYTE1
59 02/25/2021
DEC BYTE1
Before After
FE FD
BYTE1 BYTE1
60 02/25/2021
NEG
61 02/25/2021
Syntax
NEG destination
62 02/25/2021
NEG BX
Before After
0002 FFFE
BX BX
63 02/25/2021
Type agreement of operands
• For instruction with 2 operand, the two operands must
be of the same type; that is, both words or bytes.
• Illegal …. MOV AX , BYTE1 …. Is not allowed.
• Assembler will accept both the following instructions :
MOV AH , ‘A’ ….. moves 41h into AH
MOV AX , ‘A’ ….. moves 0041h into AX
64 02/25/2021
Translation of HLL to
Assembly Language
Statement Translation
B = A
65 02/25/2021
Translation of HLL to
Assembly Language
Statement Translation
B = A MOV AX , A ; moves A into AX
66 02/25/2021
Translation of HLL to
Assembly Language
Statement Translation
A = 5–A
67 02/25/2021
Translation of HLL to
Assembly Language
Statement Translation
A = 5–A
MOV AX , 5 ; put 5 in AX
SUB AX , A ; AX…. 5 – A
MOV A , AX; put it in A
68 02/25/2021
NEG A ; A = -A
ADD A , 5 ;A = 5 - A
69 02/25/2021
Translation of HLL to
Assembly Language
Statement Translation
A= B–2*A
70 02/25/2021
Translation of HLL to
Assembly Language
Statement Translation
A = B – 2 * A MOV AX , B ; AX has B
SUB AX , A ; AX has B – A SUB AX
, A ; AX has B – 2 * A
MOV A , AX ; move results to B
71 02/25/2021
Program Structure
• Machine language programs consist of :
– Codes,
– Data, and
– Stack.
Each part occupies a memory segment. They are
structured as program segments. Each program
segment is translated into a memory segment by the
assembler.
72 02/25/2021
Memory Models
73 02/25/2021
Syntax
. MODEL memory_mode1
LARGE
75 02/25/2021
Data Segment
• A program’s data segment contains all the variable
definitions. Constant definitions are made here as well,
but they may be placed elsewhere in the program since
no memory allocation is involved.
76 02/25/2021
Example
.DATA
WORD1 DW 2
WORD2 DW 5
MSG DB ‘ This is a message ‘
MASK EQU 10010010B
77 02/25/2021
Stack Segment
78 02/25/2021
Declaration Syntax
.STACK size
79 02/25/2021
Example
.STACK 100 H
80 02/25/2021
Code Segment
81 02/25/2021
Syntax
.CODE name
82 02/25/2021
Inside the code segment
83 02/25/2021
Example
.CODE
MAIN PROC
; main procedure body
MAIN ENDP
; other procedures go here
84 02/25/2021
Program Structure
• A program has always the following general structure:
85 02/25/2021
Reference
• Chap 4
86 02/25/2021