Lecture-2 (Partial) - (Class - 7 & 8) (Introduction To Assembly Language)
Lecture-2 (Partial) - (Class - 7 & 8) (Introduction To Assembly Language)
Lecture-2
Class-7-8
1
Introduction to Programming the microprocessor
Programming Language
2
Programming Language
Low level languages
close to the machine level instruction set.
provide less or no abstraction from the hardware.
interacts directly with the registers and memory.
Programs developed using LLL are machine dependent and
are not portable.
does not require any compiler or interpreter to translate
the source to machine code.
An assembler may translate the source code written in low
level language to machine code.
3
Programming Language
Low level languages
Programs written in low level languages are fast and memory
efficient.
However, it is nightmare for programmers to write, debug and
maintain low-level programs.
mostly used to develop operating systems, device drivers,
databases and applications that requires direct hardware access.
4
Programming Language
Low level languages
Low level languages are further classified in two more
categories
5
Programming Language
Low level languages: Machine language
It consists set of instructions that are executed directly by the
computer.
These instructions are a sequence of binary bits.
Instructions written in machine language are machine dependent
and varies from computer to computer.
6
Programming Language
Low level languages: Machine language
Example:
SUB AX, BX
= 00001011 00000001 00100010
is an instruction set to subtract values of two registers AX and BX.
A Programmer must have additional knowledge about the
architecture of the particular machine, before programming in
machine language.
7
Programming Language
Low level languages: Assembly language
Assembly language is an improvement over machine language.
Similar to machine language, assembly language also interacts
directly with the hardware.
Instead of binary sequence, assembly language uses mnemonics to
represent an instruction set.
8
Programming Language
Low level languages: Assembly language
Mnemonics gave relief to the programmers from remembering
binary sequence for specific instructions.
As English words like ADD, MOV, SUB are easy to remember, than
binary sequence 10001011.
Still have to remember various mnemonics for different computer
architectures.
9
Programming Language
Low level languages: Assembly language
Assembly language uses a special program called assembler.
Assembler translates mnemonics to specific machine code.
Assembly language is used for developing
operating systems,
device drivers,
compilers and
other programs that requires direct hardware access.
10
Programming Language
Disadvantages of low level languages
12
Assembly Language Syntax
Rules
An Assembly language program
consists of a sequence of statements.
Assembly language instructions are so basic that,
Input/output are harder than High level Language
DOS functions are used for I/O which are fast enough to
invoke
13
Assembly Language
Assembler
Instruction is translated into machine code by an assembler,
MASM :
Microsoft Macro Assembler
NASM :
Netwide Assembler is an assembler and disassembler for the Intel
x86 architecture,
One of the most popular assemblers for Linux
1. an Instruction
An instruction executes an operation during the run-time of the program
2. an Assembler directive
Assembler directive instructs the assembler during program assemble time
to perform some specific task. Such as,
allocating memory space for a variable
creating a procedure 15
Assembly Language Syntax
Program Statement
Both instruction and assembler directive may have
up to four (4) fields.
Name Operation Operand’(s) Comment
Example of Instruction:
MOV CX , v1 ; initialize counter
Example of Assembler Directive :
V1 DB 10 ; initialize a 1 byte variable V1 by
10
MAIN PROC ; creates a procedure called MAIN
16
Do not have to be aligned but must appear in the above order
Assembly Language Syntax
Program Statement
An assembly language statement contains the following
fields.
Name Field can be used to define a symbol
Operation Field defines the operation code or pseudo-op
Operand Field specifies either the address or the data.
Comment Field allows the programmer to document the
software
17
Assembly Language Syntax
Name Field
18
Assembly Language Syntax
Name Field
19
Assembly Language Syntax
Name Field
Examples:
Legal names Illegal names
COUNTER1 2ABC
@CHARACTER TWO WORDS
$500 A45.26
SUM_OF_DIGITS YOU&ME
.TEST
DONE?
20
Assembly Language Syntax
Operation Field
Operation is a predefined or reserved word
for instruction:
mnemonic [symbolic operation code (opcode)]
for assembler directive :
pseudo-operation code (pseudo-op)
Assembly Language Syntax
Operation Field: for instruction
For an instruction
22
Assembly Language Syntax
Operation Field: for instruction
Assemblylanguage uses two, three or four letter mnemonics
to represent a opcode.
A mnemonic is a symbolic name for a single opcode
A mnemonic usually represents the initials or shortened form
of the English word of the operation performed by the
instruction.
Mnemonic/opcode for subtract is : SUB
Mnemonic/opcode for instruction to copy data from one
location to another is : MOV 23
Assembly Language Syntax
Operation Field: for assembler directive
24
Assembly Language Syntax
Operand Field: for an instruction
Examples of instructions with different operand fields
NOP ; Instruction with no operand field
INC AX ; Instruction with one operand fields
ADD AX, 2 ; Instruction with two operand fields
If 2 operands:
the first is destination : it is register or memory location where the
result is stored
the second is the source operand, usually not modified by instruction
25
Assembly Language Syntax
Operand Field: for an assembler directive
Usually contains more information about the
assembler directive
26
Assembly Language Syntax
Comment Field
Optional field used for documentation purposes.
Begins with semicolon marks and assembler ignores any
thing after (;)
Importance of Comments in low-level language
Comments in each line: Good programming practice
Don't say obvious, say the context
MOV CX,0 ;move 0 to CX
;full line comment
;
MOV CX,0 ;CX counts terms, initially 0 27
Program Data
Numbers
Examples:
number type
1010 decimal (default)
1010B binary
-2134D decimal
ABFFH illegal
1ABFFH hex
1BHD illegal
1BFFh hex
1,23 illegal
28
Program Data
Characters
Characters and character segments must be enclosed in
single or double quotes; ‘A' , “hello“.
Assembler translates characters to their ASCII code
29
Variables
Byte variables
Assembler directive for defining byte variable:
Name DB initial value ;comments if any
Examples:
ALPHA DB 4 ; 1 byte variable
;named ALPHA is initialized to 4
Gama DB ? ; an uninitialized byte variable
30
Variables
Word variables ( 2 bytes)
Assembler directive for defining word variable:
Syntax:
Name DWinitial 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:
WDDW1234H
low byte WD contains 34h, high byte contains 12h 31
Variables
Array
Assembler directive for defining 3 byte array:
B_ARRAY DB 10, 25, 20
32
Variables
Array
W_ARRAY DW 0FFFFh, 789Ah, 0BCDEh
37
Basic Instructions
MOV
MOV destination , source
Example:
MOV AX , WORD1
This reads “ Move WORD1 to AX “
The contents of register AX are replaced by the
contents of the memory location WORD1.
38
Basic Instructions
MOV
MOV destination , source
MOV AX , WORD1
Before After
0006 0008
AX AX
0008 0008
WORD1 WORD1 39
Basic Instructions
MOV
MOV AH , ‘A’
This is a move of the 041h ( the ASCII code of “A” ) into register AH.
CAP221 01/03/2021 40
Basic Instructions
XCHG
(Exchange) operation is used to exchange the contents of
two registers, or
a register and a memory location
CAP221 01/03/2021 41
Basic Instructions
XCHG
CAP221 01/03/2021 42
Basic Instructions
XCHG
XCHG AH , BL
Before After
1A 00 05 00
AH AL AH AL
00 05 00 1A
BH BL BH BL
43
Basic Instructions
XCHG
XCHG AX , WORD1
CAP221 01/03/2021 44
Basic Instructions
Restrictions on MOV and XCHG
Memory to memory move is illegal in Intel architecture
Example :
ILLEGAL : MOV WORD1 , WORD2
ILLEGAL : XCHG WORD1 , WORD2
LEGAL:
MOV AX , WORD2
MOV WORD1 , AX
XCHG AX , WORD2
45
XCHG WORD1 , AX
Basic Instructions
Restrictions on MOV
46
Basic Instructions
Restrictions on XCHG
47
Basic Instructions
ADD & SUB
Are used to add & subtract the contents of
two registers,
a register & memory location , or
a register and a number
memory location and a number.
CAP221 01/03/2021 48
Basic Instructions
ADD & SUB
ADD destination , source
SUB destination , source
Example:
ADD WORD1 , AX
Thisinstruction , “ Add AX to WORD1 “ , causes the
contents of AX & memory word WORD1 to be
added, and the sum is stored in WORD1.
AX is unchanged.
49
Basic Instructions
ADD & SUB
ADD BL , 5
This is an addition of the number 5 to the contents of
register BL.
SUB AX , DX
This instruction , “ Subtract DX from AX “ , the
value of DX is subtracted from the value of AX , with
the difference being stored in AX.
DX is unchanged.
01/03/2021 50
Basic Instructions
ADD & SUB: ILLEGAL
Solution :
move BYTE2 to a register before adding
DEC destination
example: DEC BYTE1
subtracts 1 to the variable BYTE1
53
Basic Instructions
NEG
Is used to negate the contents of the destination.
It does this by replacing the contents by its two’s
complement.
NEG destination
The destination may be a
register or
memory location.
54
Basic Instructions
NEG
NEG BX
Before After
0002 FFFE
BX BX
55
Basic Instructions
Type Agreement of Operands
Operands of the two-operand instruction must be of same
type, that is, both bytes or words.
MOV AX, Byte1 ; illegal
Statement Translation
B = A MOV AX , A ; moves A into AX
MOV B , AX ; and then into B
WHY
Because direct memory – memory move is illegal in Intel
Architecture
we must move the contents of A into a register before
moving it to B.
57
Translation of HLL to Assembly Language
Statement Translation
58
Translation of HLL to Assembly Language
Statement Translation
59
Program Structure
Machine language programs consist of 3 segments/parts:
1. Codes,
2. Data, and
3. Stack.
Each part occupies a memory segment.
Same organization is reflected in an Assembly Language Program (ALP)
ALP also consist of 3 segments/parts:
1. codes,
2. data and
3. stack
are structured as program segments and begin with directive
60
.CODE, .DATA, .STACK
Program Structure
Each
program segment is translated into a memory
segment by the Assembler.
We will use simplified segment definitions introduced by
MASM (Version 5.0)
61
Memory Models
.MODEL memory_mode1
62
Memory Models
.MODEL memory_mode1
LARGE
• Code in more than
SMALL MEDUIM COMPACT one segment
63
Memory Models
64
Data Segment
65
Data Segment
Example:
.DATA
WORD1 DW 2
WORD2 DW 5
MSG DB ‘ This is a message ‘
MASK EQU 10010010B
CAP221 01/03/2021 66
Stack Segment
67
Stack Segment
68
Code Segment
It contains a program’s instructions.
.CODE name
Optional name for the
segment
69
Code Segment
Inside the code segment
Inside the code segment the Instructions are organized as
procedures.
The simplest procedure definition is :
procedure_name PROC
; body of the procedure
procedure_name ENDP
CAP221 01/03/2021 71
Program Structure
A program has always the following general structure:
RET
procedure_name ENDP
Procedures
https://www.tutorialspoint.com/assembly_programming/index.htm
Microprocessor & Interfacing, Author. V. Hall Chapter-2