Assembly Language Programming
By:- Mritunjay B Singh
Agenda
● Introduction
●
Advantages and Disadvantages.
● Assembly language format
● Pseudo Op Codes
● Source Code
● Software design Steps
● Flowchart
● Example of flowchart
● Problem as example
● Structured programming
● Program looping
● Problem specification on loop
● Problem specification on counter and timing delays
● Nested loops
●
Subroutines
● Parameter passing
●
Recursive routine
● Summary
Why?
● Machine level language for Machine.
● User/ programmer has high level language.
– Generally english is comfortable
– High level language: Basic, fortran,c
● Mnemonic machine language. Eg MOV B,A
● Assembly language(ext to Mnemonic)
Advantages
●
It is better for human understanding.
●
Use of Mnemonics for OP Code.
●
Use of symbolic name for Operand address. Eg LDA 003A
●
Easy to write, modify and debug. (assembler takes care for
modification)
●
Self Documenting.
●
Checks syntax errors (software to convert into machine language).
●
Reserve memory location.
●
More efficient program.(high level language converted by compiler
is not so efficient)
Disadvantages
● Assembler(softw) converts Assembly language
(source code) to machine lang (object code).
● Source code must be written in well defined
format to be understand by assembler.
● Complexity of assembler should be brought
down by well defined format.
Assembly language format
●
Label: Operation Oprands ; Comments
●
Labels:- provide symbolic name of address.
Variable number of character.
Sometimes not present
●
Operation:- Nmemonic of the opcode
pseudo opcodes, comments for assembler
macro(a part repetitive code represented by a single code)
●
Operands:-operands seperated by commas ‘,’
● Comments:- for easy understanding and self documenting
for future maintenance
Pseudo Op codes
● ORG: eg ORG E000 H
a directive to assembler to start ML at E000 H address
● EQU: eg ADDR EQU 003A H
symbolic representation of Addr/Data equals to a
value
● DB: eg DATA: DB A1H,6FH
allocating memory space to series of data
● Note: pseudo opcode varies from assemblers
Source Code
● Location object code ; simple program example
<003A> OP1 EQU 003AH
<003B> OP2 EQU OP1+1
<003C> OP3 EQU OP1+2
ORG 3000H ; start writing program from memory location 3000H
<3000> 3A 3A00 LDA OP1; load acc with OP1
<3003> 47 MOV B,A; move data from acc to reg B
<3004> 3A 3B00 LDA OP2; load OP2 to acc
<3007> 80 ADD B; add value in reg B with acc and store in acc
<3008> 32 3C00 STA OP3; store result from acc to OP3 location
<300B> 76 HLT ; halt(end) the program
● Above is program listing
Software Design steps
● Problem specification.(english)
● Design Steps (obtain the structure of Program)
top-down approach
Hierarchical diagram (modular program)
● Coding (machine and language dependent)
● Testing and debugging.
● Documentation and maintenance.
Flowchart
● Symbols Meaning
Start/end :termination
Input/ Output
Normal program sequence
Decision making
Continuation
Direction of program execution
Example
Start
Load operands from memory to registers
Add Operands
Store result in memory
End
Problem Specification
● Read data from temperature sensor (port-1)
and make a relay(port-2) ON (output 01) if the
temp data is <= 7F.
● Design : Start
Output 01 to Port 2
Read data from port-1
Is data <=7F N
Y
Stop
Output 01
Port-2
Coding
IN PORTS ; Read Temp data
CPI 7F ; compare
JC L1 ; jump
MVI 00H ; move imm 00H
JMP L2 ;
L1: MVI 01H ; output 01
L2: OUT PORT2 ; to make relay ON
HLT
Structured Programming
● Each block in structured programming will have
“ONE Entry” and “ONE Exit” points
● Types of blocks:
– Strainght line code. Eg always executed in same
Seq1
sequence of appearance, line by line. Seq2
Seq3
.
.
– Do- While OR Repeat-Until: are types of loop .
Input
While Condition Yes
statement Sequence to exe
No
seq_statement
Yes
Conditional statement
● If-Then-Else:
No
Conditional statement YES
NO
seq_statement_1 seq_statement_2
● Go-To Jump is prohibited in structed programming :
unconditional jump can lead to ambiguity.
Program Looping
● Entire loop program in divided into four
components
Start
– Initialization of loop paramenters
– Process of subtask which may be repeated
Seq_1
– Modify loop parameters Seq_2
.
– Perform test and branch .
.
Modify loop param
Conditional block YES
NO
Example: problem specification
● Write a program to input 100 bytes of data from
an I/O device and store in memory.
● Design: flowchart
Start
Initialize count register with some value
And also initialize location register provide
the address where data from I/O will be
Input one byte of data from I/O and store
Modify the loop parameter(modify location and count registers)
ON Is Count Reg
=0? YES
Stop
Source Code
MVI B,64H ; move immediately to reg B 100
LXI H,2000H ; load HL pair reg with some location
LOOP: IN 10H ; take input from I/O ports
MOV M, A ; move accumulator data to a memory location store in HL pair
reg
INX H ; increment the HL pair count
DCR B ; decrement reg B
JNZ LOOP ; jump if non-zero to level LOOP
HLT ; stop
Counter And Timing Delays
● Problem Specification: generate time delay by
software technique. Start
● Design: flowchart Initialize a register R
Decrement R
ON
Is reg R = 0?
Yes
Stop
States
MVI B, NUM 7
LOOP: DCR B 4
JNZ LOOP 7/10
HLT 4
● Delay Time: (7+(4+10)NUM+7+4)multiplied by time period
of clock (micro sec).
● To reduce the delay time we can use instead of single
register use register pair.
Time delay using a register-pair
States
LXI D, NUM 10
DEL:DCX D 6
MOV A,D 4
ORA E 4
JNZ DEL 7/10
HLT 4
Delay time=(10 +24xNUM+21) x T (micro sec)
Time Delay With Two Loops
Start
● Flowchart
Load Reg R1
Load Reg R2
Decrement R2
Is R2=0?
ON
Y
Decrement R1
ON
Is R1=0? Y
Stop
Sorce Code
MVI B, N1
L1:MVI C, N2
L2:DCR C
JNZ L2
DCR B
JNZ L1
HLT
Delay time =?
Subroutine Linkage
● Involves three process: a) invoke sub-routine
b) execute sub-routine, and c) return
● Inside subroutine,use Push and Pop in balance
manner.
● Necessity to save General Purpose Registers.
● Develop suitable mechanism for parameter passing.
– Using Registers
– Use Main memory
– Through stack
Example
● Write a software delay routine by passing parameter using
GPRs.
● Design: Start LXI D, NUM
Load Delay parameter CALL DEL
In Regoster
DCX D
Call Delay Routine MOV A, D
Modify Delay Parameters
ORA E
JNZ DEL
ON
Test to Branch
RET
Return
Nested Subroutine Call
● Calling a new subroutine within a subroutine.
● Number of subroutine depends upon size stack.
● Chances of stack over flow.
Recursive Routine
● Write a recursive subroutine to compute the sum of first N natural
number.
RECA PUSH B; save working reg
MOV A, B; move n to working reg
DCR A; n=n-1
JZ LAST; if n-1=0, f(n)=0
CALL RECA; else f=f(n-1)
LAST ADD B; f(n)=n+f(n-1)
POP B; restore working reg
RET; returns
Summary
● Introduction of assembly language.
● Assembly language format.
● Concepts of pseudo Op-Code.
● Software design steps.
● Flowchart.
● Concept of structured programming
● Conditional jumps and looping
● Counter and timing delays
● Nested loops
● Subroutines
● Parameter passing
● Recursive routine