MODULE 2
ASSEMBLY LANGUAGE PROGRAMMING
STRUCTURE of a PROGRAM
An assembly language program is a series of statements, which are either assembly
language instructions such as ADD and MOV, or statements called directives.
[ label: ] mnemonics [ operands ] [;comment ]
A square bracket [ ] indicates that the field is optional.
The label field allows the program to refer to a line of code by name. The label fields
cannot exceed a certain number of characters. Any label which refers to an
instruction should be followed by a colon ‘:’ During assembly the label will be
translated into a program address which can be used as a target for "jump" or
"branch" instructions.
Mnemonic - mnemonics are the instructions that the assembler translates into
machine code. Eg: MOV, ADD
Operands - Instructions may require one, two, or no operands which must
immediately follow the instructions/mnemonic
The comment field begins with a semicolon ‘;’ which is a comment indicator.
ADDRESSING MODES:
The term addressing modes refers to the way in which the operand (data) of an
instruction is specified. The addressing mode specifies a rule for interpreting the address
location of the data or operand in an instruction before the operand is actually executed.
Immediate Addressing Mode
Direct Addressing Mode
Register Addressing Mode
Register Indirect Addressing Mode
Indexed Addressing Mode
IMMEDIATE ADDRESSING MODE
In this Immediate Addressing Mode, the data is provided in the instruction itself. The
data is provided immediately after the opcode. In these instructions, the # symbol is used to
represent immediate data. In the instruction, if the immediate data is starting with A to F, the data
should be preceded by 0.
Eg: MOV A, #25H
ADD A, #0F5H
DIRECT ADDRESSING MODE
In the Direct Addressing Mode, the source or destination address is specified by
using 8-bit data in the instruction. Only the internal data memory can be used in this mode.
Eg: MOV A, 30H
ADD A, 35H
REGISTER ADDRESSING MODE
In the register addressing mode the source or destination of data will be a register
(R0 to R7).
Eg: MOV A, R2
ADD A, R3
REGISTER INDIRECT ADDRESSING MODE
In this mode, the source or destination address is given in the register. By using
register indirect addressing mode, the internal or external addresses can be accessed. The
R0 and R1 are used for 8-bit addresses, and DPTR is used for 16-bit addresses, no other
registers can be used for addressing purposes.
Eg: MOV A, @R0
MOVX A, @DPTR (in this instruction, the X in MOVX indicates the external data
memory.)
INDEXED ADDRESSING MODE
In the indexed addressing mode, the code memory accessed. The destination
operand is always the register A.
Eg: MOVC A, @A+DPTR (The C in MOVC instruction refers to code byte.)
8051 INSTRUCTION SET
8051 Microcontroller have set of instruction to perform different operations. There
are five group of instruction which are listed below.
Data Transfer Instructions
Arithmetic Instructions
Logic Instructions
Program Branching Instructions
Bit-oriented Instructions
ARITHMETIC INSTRUCTIONS:
Arithmetic instructions perform several basic operations such as addition,
subtraction, division, multiplication etc. After execution, the result is stored in the first
Operand / A register.
ADD - Adds the two data and the result will be in A register
ADDC – Adds the two data and carry and the result will be in A register
SUBB - Subtracts the second data from the accumulator with a borrow and the result will be in A.
INC – Increment the register content by 1
DEC - Decrement the register content by 1
MUL AB - Multiplies content in A and B register
Instruction multiplies the value in the accumulator with the value in the B
register. The low-order byte of the 16-bit result is stored in the accumulator, while the
high byte remains in the B register. If the result is larger than 255, the overflow flag is
set.
DIV AB - Divides the content in accumulator (A register) by the register B
Instruction divides the value in the accumulator by the value in the B register. After
division the 8-bit quotient is stored in the accumulator (A register) and the 8-bit remainder is
stored in the B register.
LOGICAL INSTRUCTIONS:
Logic instructions perform logic operations upon corresponding bits of two registers. After
execution, the result is stored in the first operand.
ANL – logical AND operation
ORL – logical OR operation
XRL – logical XOR operation
CLR A - Clears the accumulator A: accumulator
CPL A - Complements the accumulator
SWAP A - Swaps nibbles within the accumulator
RL A - Rotates the content of accumulator (A register) one bit left
RR A - Rotates the content of accumulator (A register) one bit right
RLC A - Rotates the accumulator one bit left through the carry flag A: accumulator
All eight bits in the accumulator and carry flag are rotated one bit left. After this
operation, the bit 7 is rotated into the carry flag position and the carry flag is rotated into
the bit 0 position.
RRC A - Rotates the accumulator one bit right through the carry flag A: accumulator
All eight bits in the accumulator and carry flag are rotated one bit right. After this
operation, the carry flag is rotated into the bit 7 position and the bit 0 is rotated into the
carry flag position.
DATA TRANSFER INSTRUCTION
These instructions are used to copy the content of source operand to Destination
operand. These instructions are used to move data immediately, from one register to
another or between memory and registers
MOV A,Rn - Moves the Rn register to the accumulator
MOVC A, @A+DPTR - Moves the code byte relative to the DPTR to the accumulator
Instruction first adds the 16-bit DPTR register to the accumulator. The result of
addition is then used as a memory address from which the 8-bit data is moved to the
accumulator.
MOVX – move data between accumulator and external memory.
PROGRAM BRANCHING INSTRUCTION
A microcontroller sequentially executes instructions but, in some cases, transferring
this control to another block of code becomes essential. The branching instructions in the
8051 microcontrollers are responsible for performing this operation. Tasks like looping,
calling delays, and conditional execution of code can be performed using these branching
instructions. There are two kinds of branch instructions:
Unconditional jump instructions: upon their execution a jump to a new location from where
the program continues execution is executed.
LJMP – unconditional long jump within 64K
AJMP - unconditional absolute jump within 2K
SJMP – unconditional short jump within 128byte
LCALL – unconditional long call within 64K
ACALL – unconditional absolute call within 2K
RET – return from call
RETI - return from interrupt
NOP – No operation
Conditional jump instructions: a jump to a new program location is executed only if a
specified condition is met. Otherwise, the program normally proceeds with the next
instruction.
JZ- jump on zero accumulator
JNZ – jump on non-zero accumulator
CJNE – compare and jump if not equal
DJNZ – decrement and jump if non-zero.
BOOLEAN VARIABLE MANIPULATION
These are instructions capable of manipulating bit variables.
CLR bit – CLEAR (0) the specified bit.
SETB bit - SET (1) a specified bit.
CPL bit - Complement the bit indicated.
MOV - Transfer or move a bit.
ANL C, bit - AND between the carry logic and the bit indicated.
ORL C, bit - OR carry logic between the specified bit.
Bit oriented program branching instructions are;
JC – jump on carry
JNC – jump on no carry
JB – jump on bit
JNB – jump on no bit