0% found this document useful (0 votes)
9 views73 pages

Assembly Lang Details Unit1

Uploaded by

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

Assembly Lang Details Unit1

Uploaded by

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

Subject Introduction to System Programming

Assembly Language
Subject Introduction to System Programming

 Assembler is used to convert programs written in assembly language


to machine language.
 The assembler takes the source code as input which is written in
assembly language and converts it into machine level language or
machine language.
Subject Introduction to System Programming

• Assembly language is low level language.

• An assembly language is machine dependent.

• It differs from computer to computer.

• Writing programs in assembly language is very easy as


compared to machine(binary) language.
Subject Introduction to System Programming

Assembly language programming Terms:

• Location Counter: (LC) points to the next


instruction.

• Literals: constant values

• Symbols: name of variables and labels

• Procedures: methods/ functions


Subject Introduction to System Programming

Assembly language Statements:

• Imperative Statements:

• Declarative/Declaration Statements:

• Assembler Directive:
Subject Introduction to System Programming

Imperative Statements
• Imperative means mnemonics
• These are executable statements.
• Each imperative statement indicates an action to
be taken during execution of the program.
• E.g MOVER
. BREG, X
STOP
READ X
ADD AREG, Z
Subject Introduction to System Programming

Declarative Statements
• Declaration statements are for reserving
memory for variables.
• We can specify the initial value of a variable.
• It has two types:
• DS // Declare Storage

• DC // Declare Constant
Subject Introduction to System Programming

DS(Declare Storage):
• Syntax:
• [Label] DS <Constant specifying size>
• E.g. X DS 1
DC (Declare Constant):
Syntax:
[Label ] DC <constant specifying value>
E.g Y DC ‘5’
Subject Introduction to System Programming

Assembler Directive
• Assembler directive instruct the assembler to
perform certain actions during assembly of a
program.
• Some assembler directive are:
START <address constant>

END
Subject Introduction to System Programming

Advanced Assembler Directives


• 1. ORIGIN
• 2. EQU
• 3. USING
• 4. DROP
• 5. LTORG
Subject Introduction to System Programming

Sample Assembly Code


1. START 100 It is an AD statement as it has Assembler directive START
2. MOVER AREG, X It is an IS because it starts with mnemonic.
3. MOVER BREG, Y
4. ADD AREG, Y
5. MOVEM AREG, X
6. X DC ‘10’ It is an DS/ DL statement because it has DC
7. Y DS 1 It is an DS/ DL statement because it has DS
8. END
Subject Introduction to System Programming

Identify the types of statements


State.No IS DS AD

8
Subject Introduction to System Programming

Identify the types of statements


State.No IS DS AD

1 AD

2 IS

3 IS

4 IS

5 IS

6 DS

7 DS

8 AD
Subject Introduction to System Programming

Advanced Assembler Directives

• ORIGIN

• EQU

• LTORG
Subject Introduction to System Programming

Definitions

• LC:

• Symbol:

• Literals:

• Procedures:
Subject Introduction to System Programming

How LC Operates?
Sr. NO LC
1 START 100

2 MOVER AREG, X

3 MOVER BREG, Y

4 ADD AREG, BREG

5 MOVEM AREG, X

6 X DC ‘10’

7 Y DC ‘15’

8 END
Subject Introduction to System Programming

How LC Operates?
Sr. NO LC
1 START 100

2 MOVER AREG, X 100

3 MOVER BREG, Y 101

4 ADD AREG, BREG 102

5 MOVEM AREG, X 103

6 X DC ‘10’ 104

7 Y DC ‘15’ 105

8 END
Subject Introduction to System Programming

Identify symbol, literals, AD, IS, DS, Label


• START 100
• MOVER BREG, =‘2’
• LOOP MOVER AREG, N
• ADD BREG, =‘1’
• ORIGIN LOOP+5
• LTORG
• ORIGIN NEXT +2
• LAST STOP
• N DC ‘5’
• END
Subject Introduction to System Programming

Solution (From Previous Example)


Sr. No AD DS IS Symbol Literal Label

1
2
3
4
5
6
7
8
9
10
Subject Introduction to System Programming

Solution (From Previous Example)


Sr. No AD DS IS Symb Literal Label
ol
1 AD
2 IS =2
3 IS N LOOP
4 IS =1
5 AD
6 AD
7 AD
8 IS LAST
9 DS N
10 AD
Subject Introduction to System Programming

Machine Structure
Subject Introduction to System Programming

Machine Structure
Subject Introduction to System Programming

• Consider any hypothetical assembly language.


• It supports three registers:
• AREG
• BREG
• CREG
Subject Introduction to System Programming

• Machine instruction Format: It supports 11 different operations

• STOP
• ADD
• SUB
• MULT
• MOVER
• MOVEM
• First operand is always a CPU register. • COMP
• Second operand is always memory operand. • BC
• READ and PRINT instructions do not use first operand. • DIV
• READ
• The STOP instruction has no operand. • PRINT
Subject Introduction to System Programming

• Each symbolic opcode is associated with


machine opcode.
• These details are stored in machine opcode
table(MOT).
• MOT contains:
 Opcode in mnemonic form
 Machine code associated with the opcode.
Subject Introduction to System Programming

Symbolic Opcode Machine Code Size of


for instruction (in
opcode number of
words)
STOP 00 1
ADD 01 1
SUB 02 1
MULT 03 1
MOVER 04 1
MOVEM 05 1
COMP 06 1
BC 07 1
DIV 08 1
READ 09 1
PRINT 10 1
Subject Introduction to System Programming

Symbolic Opcode Machine Code for opcode

START 01

END 02

LTORG 03

ORIGIN 04

EQU 05
Subject Introduction to System Programming

Sr. NO Declarative Machine Opcode


Statement
01 DS 01

02 DC 02
Subject Introduction to System Programming

Sr. Symbolic opcode Machine opcode


No
1 AREG 01

2 BREG 02

3 CREG 03
Subject Introduction to System Programming

ASSEMBLER
• An assembly language program can be translated into
machine language.
It involves following steps:
1. Find addresses of variable.
2. Replace symbolic addresses by numeric
addresses.
3. Replace symbolic opcodes by machine
operation codes.
4. Reserve storage for data.
Subject Introduction to System Programming

Step 1
• We can find out addresses of variable using LC.
• First identify all variables in your program.

• START 100
• MOVER AREG, X
• MOVER BREG, Y
• ADD AREG, X
• MOVEM AREG, X
• X DC ‘10’
• Y DC ‘15’
• END
Subject Introduction to System Programming

Step 1
Sr. NO LC
1 START 100

2 MOVER AREG, X 100

3 MOVER BREG, Y 101

4 ADD AREG, X 102

5 MOVEM AREG, X 103

6 X DC ‘10’ 104

7 Y DC ‘15’ 105

8 END
Subject Introduction to System Programming

Sr. No Name of Address


Variable(Symbol)

1 X 104

2 Y 105
Subject Introduction to System Programming

Step2: Replace all symbolic address with numeric address.

• START 100
• MOVER AREG, 104
• MOVER BREG, 105
• ADD AREG, 104
• MOVEM AREG, 104
• X DC ‘10’ Memory is reserved but no
• Y DC ‘15’ code is generated.
• END
Subject Introduction to System Programming

Step3: Replace symbolic opcodes by machine operation codes.


LC Assembly Instruction Machine Code
101 MOVER AREG, 104 04 01 104

102 MOVER BREG, 105 04 02 105

103 ADD AREG, 104 01 01 104


104 05 01 104
MOVEM AREG, 104

105
106
107
Subject Introduction to System Programming

Question For U
START 102
READ X
READ Y
MOVER AREG, X
ADD AREG, Y
MOVEM AREG, RESULT
PRINT RESULT
STOP
X DS 1
Y DS 1
RESULT DS 1
END
Subject Introduction to System Programming

Question For u
START 101
READ N
MOVER BREG, ONE
MOVEM BREG, TERM
AGAIN MULT BREG, TERM
MOVER CREG, TERM
ADD CREG, ONE
MOVEM CREG, TERM
COMP CREG, N
BC LE, AGAIN
MOVEM BREG, RESULT
PRINT RESULT
STOP N
DS 1
RESULT DS 1
ONE DC ‘1’
TERM DS 1
Subject Introduction to System Programming

Forward Reference Problem

Using a variable before its definition is called as forward


reference problem.
• E.g.
• START 100
• MOVEM AREG, X
• MOVER BREG, Y
• ADD AREG, Y
• X DC ‘4’
• In example variable X, Y are making forward reference.
• Y DC ‘5’
• So, We can solve it by using back patching.
• END
Subject Introduction to System Programming

Consider another example


Subject Introduction to System Programming

Apply LC

Try to convert it into machine code


Subject Introduction to System Programming

Backpatching
Step 1: Construct TII(Table of incomplete instruction)

Step 2 :The operand field of instruction containing a forward reference is left blank initially.
Subject Introduction to System Programming

• Step 3: After encountering END statement symbol table would contain the address of all
symbols defined in the source program.

SYMBOL NAME ADDRESS

X 104

ONE 105

TEN 106
Subject Introduction to System Programming

• Now we can generate machine code…


Subject Introduction to System Programming

Assembler
• An Assembler is a translator which translates assembly
language code into machine language with help of data
structure.

• It has two types


• Pass 1 Assembler.
• Pass 2 Assembler.
Subject Introduction to System Programming
Subject Introduction to System Programming
Subject Introduction to System Programming

General design procedure of assembler

• Statement of Problem
• Data Structure
• Format of databases
• Algorithms
• Look for modularity.
Subject Introduction to System Programming

Statement of Problem
• We want to convert assembly language program into
machine language.
Subject Introduction to System Programming

Data Structure Used


Data Structure used are as follows:
• Symbol table
• Literal Table
• Mnemonic Opcode Table
• Pool Table
Subject Introduction to System Programming

Format of Databases
• Symbol Table:
Name of Symbol address

• Literal Table:
Literal address

MOT:
Mnemonic Machine Opcode Class Length

• Pool Table: Literal Number


Subject Introduction to System Programming

Look for Modularity


• If your program is too long…
• U can make modules of it.
Subject Introduction to System Programming
Subject Introduction to System Programming

Apply LC
Construct Symbol table

index Symbol Name Address

0 X 214

1 L1 202

2 NEXT 207

3 BACK 202

Here, the assembler directive EQU is used to assign


address of L1 to the label BACK
Subject Introduction to System Programming

Generation of Intermediate CODE/Machine Code

• For constructing intermediate code we need


MOT.
Subject Introduction to System Programming

Enhanced Machine opcode Table


Subject Introduction to System Programming
Subject Introduction to System Programming

INTERMEDIATE CODE
Format for intermediate code:
• For every line of assembly statement
• one line of intermediate code is generated.
• Each mnemonic field is represented as
• (statement class, and machine code)

IS (Imperative Statement) We can get it from


DS (Declarative Statement) MOT(Machine
AD (assembler Directive) Opcode Table)
Subject Introduction to System Programming

• So, IC for mnemonic field of above line is,


• (statement class, machine code)
• (IS, 04) …………………from MOT
Subject Introduction to System Programming
• Operand Field:
• Each operand field is represented as
(operand class, reference)
The operand class can be:
1. C: Constant
2. S: Symbol
3. L: Literal
4. RG: Register For a symbol or literal the
5. CC: Condition codes reference field contains the index
of the operand's entry in symbol
table or literal table.

(IS, 04) (RG,01) or 01 (S,0)


Subject Introduction to System Programming

So IC for above line is:


(IS,04) (RG, 01) (S,0)
Or
(IS,04) (01) (S,0)
Subject Introduction to System Programming
Subject Introduction to System Programming

Source Program
Subject Introduction to System Programming
Subject Introduction to System Programming
Subject Introduction to System Programming
Subject Introduction to System Programming
Subject Introduction to System Programming
Subject Introduction to System Programming

LTORG Statement assigns addresses to the


literals in the literal pool means here 2nd Pass
started from A DS 3 to SUB BREG, = ‘93’
Subject Introduction to System Programming

END statement is used to end the program


as well as give addresses to the literals
Subject Introduction to System Programming

Output of Pass 1

 Intermediate Code

 Symbol Table

 Literal Table
Subject Introduction to System Programming

Input for Pass 2

 Intermediate Code

 Symbol Table

 Literal Table
Subject Introduction to System Programming

Pass 2
Imp Notes :
1. For Assembler directive we don’t
write opcode.
2. Among Declarative Statements,
we write machine code only for
Declarative Constant(DC), we write
constant number.
Subject Introduction to System Programming

Output of Pass 2 :- Target code

You might also like