0% found this document useful (0 votes)
83 views

Microprocessor: What Isassembly Language?

The document discusses assembly language and its advantages. It provides examples of assembly code for the 8085 microprocessor that adds two 8-bit numbers stored in memory locations. Assembly language uses symbolic codes that are easier for humans to understand compared to machine language. It allows accessing hardware features and writing time-critical and interrupt programs more easily. An assembly program has three sections - data for initialized constants, bss for uninitialized variables, and text for code. The example program loads the numbers from memory, adds them using registers, and stores the result back in memory.

Uploaded by

Sheba Parimala
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Microprocessor: What Isassembly Language?

The document discusses assembly language and its advantages. It provides examples of assembly code for the 8085 microprocessor that adds two 8-bit numbers stored in memory locations. Assembly language uses symbolic codes that are easier for humans to understand compared to machine language. It allows accessing hardware features and writing time-critical and interrupt programs more easily. An assembly program has three sections - data for initialized constants, bss for uninitialized variables, and text for code. The example program loads the numbers from memory, adds them using registers, and stores the result back in memory.

Uploaded by

Sheba Parimala
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Microprocessor

What isAssembly Language?

 Each personal computer has a microprocessor that manages the computer's


arithmetical, logical and control activities.
 Each family of processors has its own set of instructions for handling various
operations like getting input from
 keyboard, displaying information on screen and performing various other jobs. These
set of instructions are called 'machine language instruction'.
 Processor understands only machine language instructions which are strings of 1s and
0s. However machine language is too obscure and complex for using in software
development. So the low level assembly language is designed for a specific family of
processors that represents various instructions in symbolic code and a more
understandable form.
Advantages of Assembly Language

An understanding of assembly language provides knowledge of:

 Interface of programs with OS, processor and BIOS;


 Representation of data in memory and other external devices;
 How processor accesses and executes instruction;
 How instructions accesses and process data;
 How a program access external devices.

Other advantages of using assembly language are:

 It requires less memory and execution time;


 It allows hardware-specific complex jobs in an easier way;
 It is suitable for time-critical jobs;

 It is most suitable for writing interrupt service routines and other memory resident
programs.
An assembly program can be divided into three sections:

 The data section


 The bss section
 The text section
The data Section
The data section is used for declaring initialized data or constants. This data does not change
at runtime. It can declare various constant values, file names or buffer size etc. in this section.
The syntax for declaring data section is:
section .data
The bss Section

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

 Load the first number from memory location 2050 to accumualtor.


 Move the content of accumulator to register H.
 Load the second number from memory location 2051 to accumaltor.
 Then add the content of register H and accumulator using “ADD” instruction and
storing result at 3050
 The carry generated is recovered using “ADC” command and is stored at memory
location 3051
Program –

MEMORY ADDRESS MNEMONICS COMMENT


2000 LDA 2050 A<-[2050]

Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor

2003 MOV H, A H<-A


2004 LDA 2051 A<-[2051]
2007 ADD H A<-A+H
2006 MOV L, A L←A
2007 MVI A 00 A←00
2009 ADC A A←A+A+carry
200A MOV H, A H←A
200B SHLD 3050 H→3051, L→3050
200E HLT

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 −

 Data Transfer Instructions


 Arithmetic Instructions
 Bit Manipulation Instructions
 String Instructions
 Program Execution Transfer Instructions (Branch & Loop Instructions)
 Processor Control Instructions
 Iteration Control Instructions
 Interrupt Instructions
Let us now discuss these instruction sets in detail.

Data Transfer 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 −

Instruction to transfer a word

 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.

Instructions for input and output port transfer

 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.

Instructions to transfer the address

 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.

Instructions to transfer flag registers

 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 −

Instructions to perform addition

 ADD − Used to add the provided byte to byte/word to word.


 ADC − Used to add with carry.
 INC − Used to increment the provided byte/word by 1.
 AAA − Used to adjust ASCII after addition.
 DAA − Used to adjust the decimal after the addition/subtraction operation.

Instructions to perform subtraction

 SUB − Used to subtract the byte from byte/word from word.


 SBB − Used to perform subtraction with borrow.
 DEC − Used to decrement the provided byte/word by 1.
 NPG − Used to negate each bit of the provided byte/word and add 1/2’s complement.
 CMP − Used to compare 2 provided byte/word.
 AAS − Used to adjust ASCII codes after subtraction.
 DAS − Used to adjust decimal after subtraction.

Instruction to perform multiplication

 MUL − Used to multiply unsigned byte by byte/word by word.


 IMUL − Used to multiply signed byte by byte/word by word.
 AAM − Used to adjust ASCII codes after multiplication.

Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor

Instructions to perform division

 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.

Bit Manipulation Instructions

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 −

Instructions to perform logical operation

 NOT − Used to invert each bit of a byte or word.


 AND − Used for adding each bit in a byte/word with the corresponding bit in another
byte/word.
 OR − Used to multiply each bit in a byte/word with the corresponding bit in another
byte/word.
 XOR − Used to perform Exclusive-OR operation over each bit in a byte/word with
the corresponding bit in another byte/word.
 TEST − Used to add operands to update flags, without affecting operands.

Instructions to perform shift operations

 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.

Instructions to perform rotate operations

 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

Program Execution Transfer Instructions (Branch and Loop Instructions)

These instructions are used to transfer/branch the instructions during an execution. It


includes the following instructions −
Instructions to transfer the instruction during an execution without any condition −
 CALL − Used to call a procedure and save their return address to the stack.
 RET − Used to return from the procedure to the main program.
 JMP − Used to jump to the provided address to proceed to the next instruction.
Instructions to transfer the instruction during an execution with some conditions −
 JA/JNBE − Used to jump if above/not below/equal instruction satisfies.
 JAE/JNB − Used to jump if above/not below instruction satisfies.

Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor

 JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.


 JC − Used to jump if carry flag CF = 1
 JE/JZ − Used to jump if equal/zero flag ZF = 1
 JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.
 JGE/JNL − Used to jump if greater than/equal/not less than instruction satisfies.
 JL/JNGE − Used to jump if less than/not greater than/equal instruction satisfies.
 JLE/JNG − Used to jump if less than/equal/if not greater than instruction satisfies.
 JNC − Used to jump if no carry flag (CF = 0)
 JNE/JNZ − Used to jump if not equal/zero flag ZF = 0
 JNO − Used to jump if no overflow flag OF = 0
 JNP/JPO − Used to jump if not parity/parity odd PF = 0
 JNS − Used to jump if not sign SF = 0
 JO − Used to jump if overflow flag OF = 1
 JP/JPE − Used to jump if parity/parity even PF = 1
 JS − Used to jump if sign flag SF = 1

Processor Control Instructions

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.

Iteration Control Instructions

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

 LOOPE/LOOPZ − Used to loop a group of instructions till it satisfies ZF = 1 & CX


=0
 LOOPNE/LOOPNZ − Used to loop a group of instructions till it satisfies ZF = 0 &
CX = 0
 JCXZ − Used to jump to the provided address if CX = 0

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

mov edx,len ;message length


mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)

Notes Prepared by
A.Bathsheba Parimala
Assistant Professor, Dept. Of BCA
St. John’s College, Palayamkottai
Microprocessor

mov eax,4 ;system call number (sys_write)


int 0x80 ;call kernel
In the above example of displaying a character string, the registers EAX, EBX, ECX and
EDX have been used by the INT 80H function call. So, each time you need to display on
screen, you need to save these registers on the stack, invoke INT 80H and then restore the
original value of the registers from the stack. So, it could be useful to write two macros for
saving and restoring data.
observed that, some instructions like IMUL, IDIV, INT, etc., need some of the information to
be stored in some particular registers and even return values in some specific register(s). If
the program was already using those registers for keeping important data, then the existing
data from these registers should be saved in the stack and restored after the instruction is
executed.
ASSEMBLER DIRECTIVES
SEGMENT
The SEGMENT directive is used to indicate the start of a logical segment. Preceding the
SEGMENT directive is the name you want to give the segment. For example, the statement
CODE SEGMENTindicates to the assembler the start of a logical segment called CODE. The
SEGMENT and ENDS directive are used to “bracket” a logical segment containing code of
data.
Additional terms are often added to a SEGMENT directive statement to indicate some
special way in which we want the assembler to treat the segment. The statement CODE
SEGMENT WORD tells the assembler that we want the content of this segment located on
the next available word (even address)when segments ate combined and given absolute
addresses. Without this WORD addition, the segment will be located on the next available
paragraph (16-byte) address, which might waste as much as 15 bytes of memory. The
statement CODE SEGMENT PUBLIC tells the assembler that the segment may be put
together with other segments named CODE from other assembly modules when the modules
are linked together.
ENDS (END SEGMENT)
This directive is used with the name of a segment to indicate the end of that logical segment.
CODE SEGMENT Start of logical segment containing code instruction statements
CODE ENDS End of segment named CODE
END (END PROCEDURE)
The END directive is put after the last statement of a program to tell the assembler that this is
the end of the program module. The assembler will ignore any statements after an END
directive, so you should make sure to use only one END directive at the very end of your
program module. A carriage return is required after the END directive.
ASSUME

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

You might also like