0% found this document useful (0 votes)
80 views76 pages

Lecture-2 (Partial) - (Class - 7 & 8) (Introduction To Assembly Language)

The document discusses low-level programming languages. It defines machine language and assembly language as the two main types of low-level languages. Machine language uses binary instructions while assembly language uses mnemonics. Assembly language is translated to machine code by an assembler. Low-level languages provide direct hardware access but are less portable and more difficult for programmers. The document also outlines some basic syntax rules for assembly language programs.

Uploaded by

AlFayedArnob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views76 pages

Lecture-2 (Partial) - (Class - 7 & 8) (Introduction To Assembly Language)

The document discusses low-level programming languages. It defines machine language and assembly language as the two main types of low-level languages. Machine language uses binary instructions while assembly language uses mnemonics. Assembly language is translated to machine code by an assembler. Low-level languages provide direct hardware access but are less portable and more difficult for programmers. The document also outlines some basic syntax rules for assembly language programs.

Uploaded by

AlFayedArnob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 76

CSE 4621

Microprocessor and Interfacing

Lecture-2
Class-7-8

Introduction to Assembly Language

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

 Programs developed using LLL are machine dependent and


are not portable.
 difficult to develop, debug and maintain.
 Low level programs are more error prone.
 Low level programming usually results in poor programming
productivity.
 Programmer must have additional knowledge of the computer
architecture of particular machine, for programming in LLL.
11
Programming Language
Advantages of low level languages

 Programs developed using LLL are fast and memory efficient.


 Better utilization of processor and memory
 No need of any compiler or interpreters to translate the source to
machine code.
 Thus, cuts the compilation and interpretation time.
 Provide direct manipulation of computer registers and storage.
 Can directly communicate with hardware devices.

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

 Most assemblers are not case sensitive


14
Assembly Language Syntax
Rules
 Only one statement is written per line
 Each statement consists of a sequence of ASCII characters ending with a
carriage return.
 Each statement is either

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

 Name field is used for:


 instruction
label: if present, a label must be
followed by a colon (:)
 procedure names
 variable names.

18
Assembly Language Syntax
Name Field

 Assembler translates names into memory addresses.


 Names can be from 1 to 31 characters long: (letters,
digits, and special characters: ?, ., _, $, @, %)
 Embedded blanks are not allowed, names may not
begin with a digit,
 period (if used) must be the first character

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

 The operation field contains a symbolic operation


code (opcode) to describe the operation’s function
 opcodes are translated into machine code.

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

For an assembler directive

 operation field consists of a pseudo-operation code (pseudo-op)


 pseudo-ops are not translated into machine code.
 pseudo-ops simply tell assembler to do something.eg,
MAIN PROC
Main proc ; Both statements are same and are not case sensitive
‘Proc’ pseudo-op is used to create a procedure

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

If array starts at offset address 0200h, it will look like this:

Symbol Address Contents


B_ARRAY 0200H 10
B_ARRAY+1 0200H+1 25
B_ARRAY+2 0200H+2 20

32
Variables
Array
W_ARRAY DW 0FFFFh, 789Ah, 0BCDEh

If array starts at offset address 0100h, it will look like this:

Symbol Address Contents


W_ARRAY 0100H FFFFH
W_ARRAY+2 0102H 789AH
W_ARRAY+4 0104H BCDEH
33
Variables
Character strings
Examples:
1)
LETTERS DB ‘AaBCbc‘
Is equivalent to
LETTERS DB 41H,61H,42H,43H,62H,63H
2)
MSG DB ‘ABC‘,0AH,0DH,‘$‘
Is equivalent to
MSG DB 41H,42H,43H,0AH,0DH,24H
34
Variables
Named Constant
 In an assembly language program, constants are defined through the
use of the EQU directive/pseudo-op.
 Syntax:
Name EQU constant

 The EQU directive is used to assign a name to a constant.


 Use of constant names makes an assembly language easier to
understand.
 No memory is allocated for a constant.
 The symbol on the right of EQU cab also be a string
35
Variables
Named Constant
Examples:
1)
LF EQU 0AH ; LF can be used in place of 0Ah
MOV DL LF
Have the same machine code
MOV DL 0AH
2)
PMT EQU ‘TYPE YOUR NAME’
instead of
MSG DB ‘TYPE YOUR NAME’
We can use
MSG DB PMT
36
Basic Instructions
MOV
 Is used to transfer data :
 between registers,
 between a register & a memory location.
Or
 Tomove a number directly into a register or
memory location.

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.

 The previous value of AH is overwritten ( replaced by new value )

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

XCHG destination , source


XCHG AH , BL

This instruction swaps the contents of AH and BL.

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

 This swaps the contents of AX and memory location


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

ADD BYTE1 , BYTE2

Solution :
move BYTE2 to a register before adding

MOV AL , BYTE2 ; AL gets BYTE2


ADD BYTE1 , AL ; add it to BYTE1
51
Basic Instructions
INC and DEC
 INC ( increment )
Is used to add 1 to the contents of a
Register or
Memory location
 DEC ( decrement )
Is used to subtract 1 from the contents of a
 Register or
 Memory location 52
Basic Instructions
INC and DEC
INC destination
example: INC WORD1
 adds 1 to the contents of WORD1

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

 However, the assembler will accept the followings,


MOV AH, ‘A’
MOV AX, ‘A’
56
Translation of HLL to Assembly Language

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

A = 5–A MOV AX , 5 ; put 5 in AX


SUB AX , A ; AX…. 5 – A
MOV A , AX ; put it in A

58
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

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

 The size of the code & data- a program can have


is determined by specifying a memory model
using the . MODEL directive.

.MODEL memory_mode1

62
Memory Models

.MODEL memory_mode1

LARGE
• Code in more than
SMALL MEDUIM COMPACT one segment

• Code in one • Code in more • Code in one • Data in more than


than one segment one segment
segment segment • Data in more • No array larger than
• Data in one • Data in one than one
segment 64K bytes.
segment segment

63
Memory Models

 Unless there is a lot of code or data, the appropriate model is


SMALL.
 . MODEL directive should come before any segment definition.

64
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.
 We use the . DATA directive followed by variable & constant
declarations.
 Variable addresses are computed as offsets from the start of
this 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

 Used to set aside storage for temporary storage of


address and data (stack)
 Stack addresses are computed as offsets into this
segment
 Use .stack followed by a value that indicates the size of
the stack

67
Stack Segment

Declaration Syntax: Example:


.STACK size STACK 100 H

 Sets aside 100h bytes for the stack area


An optional number that
specifies the stack area size ( a reasonable size for most applications ) .
in bytes.  If size is omitted , 1 KB is set aside for the
stack area.

68
Code Segment
 It contains a program’s instructions.

.CODE name
Optional name for the
segment

there is no need for a name in a


SMALL program
Why??
The assembler will generate an error

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

 procedure_name is the name of the procedure, PROC and


ENDP are pseudo-op that delineate the procedure
70
Code Segment
Inside the code segment
Example:
.CODE
MAIN PROC
; main procedure body
MAIN ENDP
; other procedures go here

CAP221 01/03/2021 71
Program Structure
 A program has always the following general structure:

.model small ;Select a memory model


.stack 100h ;Define the stack size
.data
; Variable and array declarations
; Declare variables at this level
.code
main proc
; Write the program main code at this level
main endp
;Other Procedures
; Always organize your program into procedures 72

end main ; To mark the end of the source file


Procedures

 Procedure is a part of code that can be called from


your program in order to make some specific task.
 Procedures make program more structural and easier
to understand.
 Generally procedure returns to the same point from
where it was called.
Procedures

 The syntax for procedure declaration:


procedure_name PROC

; here goes the code


; of the procedure ...

RET
procedure_name ENDP
Procedures

 procedure_name  - is the procedure name


 RET instruction is used to return to operating system.
 The same instruction is used to return from procedure
(actually operating system sees your program as a special
procedure).
 PROC and ENDP are compiler directives, so they are not
assembled into any real machine code.
 CALL instruction is used to call a procedure.
Reference Book:

 Assembly Language Programming and Organization of the


IBM PC, Author: Ythe Yu, Charles Marut Chapter-1
(Section1.4 & 1.5).
 Assembly Language Programming and Organization of the
IBM PC, Author: Ythe Yu, Charles Marut Chapter-4.

 https://www.tutorialspoint.com/assembly_programming/index.htm
 Microprocessor & Interfacing, Author. V. Hall Chapter-2

You might also like