Microprocessor: What Isassembly Language?
Microprocessor: What Isassembly Language?
It is most suitable for writing interrupt service routines and other memory resident
programs.
An assembly program can be divided into three sections:
Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor
The bss section is used for declaring variables. The syntax for declaring bss section is:
section .bss
The textsection
The text section is used for keeping the actual code. This section must begin with the
declaration global main, which tells the kernel where the program execution begins.
The syntax for declaring text section is:
section .text
global main
main:
8085 program to add two 8 bit numbers
Problem – Write an assembly language program to add two 8 bit numbers stored at address
2050 and address 2051 in 8085 microprocessor. The starting address of the program is taken
as 2000
Example –
Algorithm
Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor
Explanation
LDA 2050 moves the contents of 2050 memory location to the accumulator.
MOV H, A copies contents of Accumulator to register H to A
LDA 2051 moves the contents of 2051 memory location to the accumulator.
ADD H adds contents of A (Accumulator) and H register (F9). The result is stored in
A itself. For all arithmetic instructions A is by default an operand and A stores the
result as well
MOV L, A copies contents of A (34) to L
MVI A 00 moves immediate data (i.e., 00) to A
ADC A adds contents of A(00), contents of register specified (i.e A) and carry (1). As
ADC is also an arithmetic operation, A is by default an operand and A stores the
result as well
MOV H, A copies contents of A (01) to H
Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor
SHLD 3050 moves the contents of L register (34) in 3050 memory location and
contents of H register (01) in 3051 memory location
HLT stops executing the program and halts any further execution
The 8086 microprocessor supports 8 types of instructions −
These instructions are used to transfer the data from the source operand to the destination
operand. Following are the list of instructions under this group −
MOV − Used to copy the byte or word from the provided source to the provided
destination.
PPUSH − Used to put a word at the top of the stack.
POP − Used to get a word from the top of the stack to the provided location.
PUSHA − Used to put all the registers into the stack.
POPA − Used to get words from the stack to all registers.
XCHG − Used to exchange the data from two locations.
XLAT − Used to translate a byte in AL using a table in the memory.
IN − Used to read a byte or word from the provided port to the accumulator.
OUT − Used to send out a byte or word from the accumulator to the provided port.
LEA − Used to load the address of operand into the provided register.
LDS − Used to load DS register and other provided register from the memory
Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor
LES − Used to load ES register and other provided register from the memory.
LAHF − Used to load AH with the low byte of the flag register.
SAHF − Used to store AH register to low byte of the flag register.
PUSHF − Used to copy the flag register at the top of the stack.
POPF − Used to copy a word at the top of the stack to the flag register.
Arithmetic Instructions
These instructions are used to perform arithmetic operations like addition, subtraction,
multiplication, division, etc.
Following is the list of instructions under this group −
Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor
DIV − Used to divide the unsigned word by byte or unsigned double word by word.
IDIV − Used to divide the signed word by byte or signed double word by word.
AAD − Used to adjust ASCII codes after division.
CBW − Used to fill the upper byte of the word with the copies of sign bit of the
lower byte.
CWD − Used to fill the upper word of the double word with the sign bit of the lower
word.
These instructions are used to perform operations where data bits are involved, i.e.
operations like logical, shift, etc.
Following is the list of instructions under this group −
SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in LSBs.
SHR − Used to shift bits of a byte/word towards the right and put zero(S) in MSBs.
SAR − Used to shift bits of a byte/word towards the right and copy the old MSB into
the new MSB.
ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and to
Carry Flag [CF].
ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB and to
Carry Flag [CF].
Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor
RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF and CF to
MSB.
RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to CF and CF to
LSB.
String Instructions
String is a group of bytes/words and their memory is always allocated in a sequential order.
Following is the list of instructions under this group
REP − Used to repeat the given instruction till CX ≠ 0.
REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero flag ZF
= 1.
MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to
another.
COMS/COMPSB/COMPSW − Used to compare two string bytes/words.
INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the
provided memory location.
OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the provided
memory location to the I/O port.
SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a byte in
AL or string word with a word in AX.
LODS/LODSB/LODSW − Used to store the string byte into AL or string word into
AX.
PROGRAM STRUCTURE
Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor
These instructions are used to control the processor action by setting/resetting the flag
values.
Following are the instructions under this group −
STC − Used to set carry flag CF to 1
CLC − Used to clear/reset carry flag CF to 0
CMC − Used to put complement at the state of carry flag CF.
STD − Used to set the direction flag DF to 1
CLD − Used to clear/reset the direction flag DF to 0
STI − Used to set the interrupt enable flag to 1, i.e., enable INTR input.
CLI − Used to clear the interrupt enable flag to 0, i.e., disable INTR input.
These instructions are used to execute the given instructions for number of times. Following
is the list of instructions under this group −
LOOP − Used to loop a group of instructions until the condition satisfies, i.e., CX =
0
Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor
Interrupt Instructions
These instructions are used to call the interrupt during program execution.
INT − Used to interrupt the program during execution and calling service specified.
INTO − Used to interrupt the program during execution if OF = 1
IRET − Used to return from interrupt service to the main program
MACROS:
A Macro is a group of instructions with a name. When a macro is invoked, the
associated set of instructions is inserted in place in to the source, replacing the macro name.
This“macro expansion” is done by a Macro Preprocessor and it happens before assembly.
Thus the actual Assembler sees the “expanded” source Writing a macro is another way of
ensuring modular programming in assembly language.A macro is a sequence of instructions,
assigned by a name and could be used anywhere in the program.
In NASM, macros are defined with %macro and %endmacro directives. The macro begins
with the %macro directive and ends with the %endmacro directive.
The Syntax for macro definition −
%macro macro_name number_of_params
<macro body>
%endmacro
Where, number_of_params specifies the number parameters, macro_name specifies the name
of the macro.The macro is invoked by using the macro name along with the necessary
parameters. When you need to use some sequence of instructions many times in a program,
you can put those instructions in a macro and use it instead of writing the instructions all the
time. For example, a very common need for programs is to write a string of characters in the
screen. For displaying a string of characters, you need the following sequence of instructions
Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor
Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor
The ASSUME directive is used tell the assembler the name of the logical segment it should
use for a specified segment. The statement ASSUME CS: CODE, for example, tells the
assembler that the instructions for a program are in a logical segment named CODE. The
statement ASSUME DS: DATA tells the assembler that for any program instruction, which
refers to the data segment, it should use the logical segment called DATA.
DB (DEFINE BYTE)
The DB directive is used to declare a byte type variable, or a set aside one or more storage
locations of type byte in memory. PRICES DB 49H, 98H, 29H Declare array of 3 bytes
named PRICE and initialize them with specified values. NAMES DB “THOMAS” Declare
array of 6 bytes and initialize with ASCII codes for the letters in THOMAS.
TEMP DB 100 DUP (?) Set aside 100 bytes of storage in memory and give it the name
TEMP. But leave the 100 bytes un-initialized.
PRESSURE DB 20H DUP (0) Set aside 20H bytes of storage in memory, give it the name
PRESSURE and put 0 in all 20H locations.
DD (DEFINE DOUBLE WORD)
The DD directive is used to declare a variable of type double word or to reserve memory
locations, which can be accessed as type double word. The statement ARRAY DD
25629261H, for example, will define a double word named ARRAY and initialize the double
word with the specified value when the program is loaded into memory to be run. The low
word, 9261H, will be put in memory at a lower address than the high word.
DQ (DEFINE QUADWORD)
The DQ directive is used to tell the assembler to declare a variable 4 words in length or to
reserve 4 words of storage in memory. The statement BIG_NUMBER DQ
243598740192A92BH, for example, will declare a variable named BIG_NUMBER and
initialize the 4 words set aside with the specified number when the program is loaded into
memory to be run.
DT (DEFINE TEN BYTES)
The DT directive is used to tell the assembler to declare a variable, which is 10 bytes in
length or to reserve 10 bytes of storage in memory. The statement PACKED_BCD DT
11223344556677889900 will declare an array named PACKED_BCD, which is 10 bytes in
length. It will initialize the 10 bytes with the values 11, 22, 33, 44, 55, 66, 77, 88, 99, and 00
when the program is loaded into memory to be run.
The statement RESULT DT 20H DUP (0) will declare an array of 20H blocks of 10
bytes each and initialize all 320 bytes to 00 when the program is loaded into memory to be
run.
DW (DEFINE WORD)
Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor
The DW directive is used to tell the assembler to define a variable of type word or to reserve
storage locations of type word in memory. The statement MULTIPLIER DW 437AH, for
example, declares a variable of type word named MULTIPLIER, and initialized with the
value 437AH when the program is loaded into memory to be run.
WORDS DW 1234H, 3456H Declare an array of 2 words and initialize them with the
specified values.
STORAGE DW 100 DUP (0) Reserve an array of 100 words of memory and initialize all 100
words with 0000. Array is named as STORAGE.
STORAGE DW 100 DUP (?) Reserve 100 word of storage in memory and give it the name
STORAGE, but leave the words un-initialized.
EQU (EQUATE)
EQU is used to give a name to some value or symbol. Each time the assembler finds the
given name in the program, it replaces the name with the value or symbol you equated with
that name. Suppose, for example, you write the statement FACTOR EQU 03H at the start of
your program, and later in the program you write the instruction statement ADD AL,
FACTOR. When the assembler codes this instruction statement, it will code it as if you had
written the instruction ADD AL, 03H.
CONTROL EQU 11000110 B Replacement
MOV AL, CONTROL Assignment
DECIMAL_ADJUST EQU DAA Create clearer mnemonic for DAA
ADD AL, BL Add BCD numbers
DECIMAL_ADJUST Keep result in BCD format.
Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai