Professional Documents
Culture Documents
----------------------------------------------------------------------------------------------------------------------------------------
Mekelle University
Chapter Four
8051-Assembly Language
--------------------------------------------------------------------
Assembly language is converted into executable machine code by a utility program referred to as
an assembler; the conversion process is referred to as assembly, or assembling the code. Assembly
language uses a mnemonic to represent each low-level machine instruction or operation. Typical
operations require one or more operands in order to form a complete instruction, and most
assemblers can therefore take labels, symbols and expressions as operands to represent addresses
and other constants, freeing the programmer from tedious manual calculations. Macro assemblers
include a macroinstruction facility so that (parameterized) assembly language text can be
represented by a name, and that name can be used to insert the expanded text into other code. Many
assemblers offer additional mechanisms to facilitate program development, to control the assembly
process, and to aid debugging.
Mnemonics assembly level instructions are called mnemonic like MOV R5, 34;
this instruction hasnt take any memory space. It is used to show the
starting address of program.
Instructions (statements) in assembly language are generally very simple, unlike those in high-
level languages. Generally, a mnemonic is a symbolic name for a single executable machine
language instruction (an opcode), and there is at least one opcode mnemonic defined for each
machine language instruction. Each instruction typically consists of an operation or opcode plus
zero or more operands. Most instructions refer to a single value, or a pair of values. Operands can
be immediate (value coded in the instruction itself), registers specified in the instruction or implied,
or the addresses of data located elsewhere in storage. This is determined by the underlying
processor architecture: the assembler merely reflects how this architecture works. Extended
mnemonics are often used to specify a combination of an opcode with a specific operand.
--------------------------------------------------------------------
Extended mnemonics are often used to support specialized uses of instructions, often for purposes
not obvious from the instruction name. For example, many CPU's do not have an explicit NOP
instruction, but do have instructions that can be used for the purpose. In 8086 CPUs the instruction
xchg ax,ax is used for nop, with nop being a pseudo-opcode to encode the instruction xchg
ax,ax. Some disassemblers recognize this and will decode the xchg ax,ax instruction as nop.
Steps of ASSEMBLY AND RUNNING 8051 PROGRAM comprises the steps depicted in figure 4.2.
--------------------------------------------------------------------
logic, data transfer, Boolean variables, and machine control instructions. Given are below some of
the instructions of 8051 Assembly Language.
ORL: Bitwise OR
POP: Pop Value From Stack
PUSH: Push Value Onto Stack
RET: Return From Subroutine
RETI: Return From Interrupt
RL: Rotate Accumulator Left
RLC: Rotate Accumulator Left Through Carry
RR: Rotate Accumulator Right
RRC: Rotate Accumulator Right Through Carry
SETB: Set Bit
SJMP: Short Jump
SUBB: Subtract From Accumulator With Borrow
SWAP: Swap Accumulator Nibbles
XCH: Exchange Bytes
XCHD: Exchange Digits
XRL: Bitwise Exclusive OR
Undefined: Undefined Instruction
The instruction set of microcontroller 8051 can be divided into such groups as Data transfer
instructions, Data Processing Instructions (Arithmetic instructions, Logical Instructions /Boolean,
Bit manipulation instructions), and Program Flow/Control Instructions/jumps. Each group is
explained in details in what follows.
These instructions are designed to perform data movement between registers, memory and ports
of the 8051 microcontroller. A typical example is the Mov instruction, which moves or transfers
data from one location to another.
--------------------------------------------------------------------
Data movement in the internal RAM. This type of instruction is supported by virtually all forms
of addressing scheme: direct, indirect, and recording and immediate.
MOVX: Data movement in the external RAM (XRAM). This type of motion only supports
indirect addressing, register 8bit by R0 or R1 and 16-bit register via the DPTR.
MOV DPTR,#2000H ;
MOVX A,@DPTR ;
MOVC: Allows movement of the accumulator ROM. By this statement can make the
manipulation or movement of tables from the program memory.
XCH: Swaps the contents of the accumulator and the internal RAM.
XCHD: Swaps the contents of the first 4 bits of the Accumulator with internal RAM.
In direct addressing, you specify the address to operate in absolute terms. For the family of 8051
microcontrollers, there are 256 available direct addresses, corresponding to both internal + RAM
Records SFR. Always, The OpCode is followed by a byte representing the address.
MOV A,30H ;
ADD A,31H ;
--------------------------------------------------------------------
Rn: For the record addressing, the instruction code (OpCode) contains the registration information
to operate the selected register bank.
MOV R1, A
11111001
@: It specifies a register containing the address of the data to be operated. R0, R1 when accessing
the internal memory of 256 Bytes indirection. DPTR to access external memory 64K bytes.
MOV R0,#30H ;
MOV A,@R0 ;
--------------------------------------------------------------------
4.3.4. Implicit
#: The opcode is followed by a constant that represents the value that will be operated.
4.3.6. Indexed
Two registers are used to point to the address containing the data. The sum of the contents of DPTR
and the accumulator determines the direction of trade. This routing is limited to only two
instructions that move data from ROM to the accumulator.
MOVC A, @A+DPTR
--------------------------------------------------------------------
Arithmetic instructions of these microcontrollers are very limited, can add, subtract, increment,
decrement and exceptionally has been integrated product and division of whole numbers.
ADD A, byte. Sum of 8 bits. Add contents of the accumulator with a byte whose address
indicated, the result will be stored in the accumulator.
ADD A, R0; A= (A)+(R0) it adds the value of Register R0 to the Accumulator value
ADDC A, byte. Addition with Carry. Carry flags when sum is greater than 8 bits.
Example: Add two 16-bit integer variables that are at memory locations 30H and 32H. The result
should be stored in a third variable in memory location/position 34H.
The following figure shows an outline of the contents of memory to perform the addition of two
integers of 16 bits. It is assumed that the data of the sum (35FEH +00003 H) have been previously
established and are being studied in this example.
--------------------------------------------------------------------
SUBBED A byte: Subtract with Carry. Subtract the contents of a byte accumulator with the
address indicated, the result is in the tank.
MOV A, #5 ; A<-5
CLR C; C<-0
SUBB A, #03H ; A<-(A)-3
INC byte: Increase. Unit increases the contents of a byte (Acc, register, memory), with the address
indicated.
DEC byte: Decrease. Decrease of one unit the contents of a byte (Acc, register, memory), with
the address indicated.
DA A: Adjust decimal accumulator. This instruction performs the setting in BCD format (0-9)
nibbles of the accumulator (b0-b3 and least significant bits b4-b7 bits) the result of the sum with
the ADD or ADDC instruction. This instruction is used when working with digit 7-segment BCD
is necessary to work.
--------------------------------------------------------------------
MUL AB: Multiplication of 8 bits. Performs a multiplication of integers of 8 bits unsigned values
contained in the register A and B. The result is an integer of 16 bits, leaving the least significant
part in the accumulator and the most significant in the register B.
DIV AB: Division 8-bit integer. Performs integer division 8-bit unsigned accumulator between
the register B. The accumulator is the result (quotient), while the remainder of integer division
is in the register B.
ANL destination, origin: AND (AND) logic. It does logical operation between the source byte
and destination byte.
AND logic.
ORL destination, origin: OR (O) logic of bits. So it does logical operation OR between the source
byte and destination byte.
XRL destination, origin: XOR (exclusive OR) logic bits. So do it exclusive OR logical operation
between the source byte and destination byte.
Accumulator:
--------------------------------------------------------------------
Bit rotation:
ANL C, bit_Source. AND (Y) between the carry logic and the bit indicated.
ORL C, bit_Source. OR (O) and carry logic between the specified bit.
--------------------------------------------------------------------
If you use an assembler, jumps can be expressed in its generic term for unconditional jump JMP
and CALL to jump to subroutine, the software assembler will replace the jump for the specific
generic microprocessor suitable for each case.
In the following snippet of code in JMP SALTO1 an unconditional jump is made from the current
position to the line that is labeled SALTO1, the assembler will replace the label SALTO1 by the
value of the corresponding direction.
...
...
JMP SALTO1 ; Jumps to Salto1 if condition is met
...
...
SALTO1: MOV A,#05H
...
Jump to subroutine
Indirect jump
JMP @ A + DPTR: Salto position indirectly resulting from the sum of the data contained in the
DPTR registers and accumulator.
--------------------------------------------------------------------