8051 Assembly language programming

8051 Register Set

In 8051, the most widely used registers are A(Accumulator), B, R0, R1, R2, R3, R4, R5, R6, R7, DPTR(Data PoinTeR)  and PC(Program Counter). Out of these registers, DPTR and PC are of 16 bits each and the rest are 8 bits wide. Due to this, data in DPTR is broken into two registers of size 8 bits each, these registers are termed as DPH, for holding higher 8 bits, and DPL, for holding lower 8 bits.

MOV instruction

The format for this instruction is as shown below:

MOV destination, source; copying source to destination

This is to note that this instruction doesn’t affect the data of source. It just moves or copies the data of source to destination.

If the value 66 is to be moved in the accumulator, then the instruction required to be passed is

MOV A, #66;

Notice the use of “#” symbol int the above instruction.This tells us that we are moving the value 66 in the accumulator.

MOV R1, A; moving the value(here 66) inside A to R1 register.

Moving hexadecimal values that are have number of bits greater than 8 will cause an error because of 8051 has 8 bit microprocessor which cannot operate on data that have more than 8 bits.

MOV A, #56H; Here “H”  indicates that 56 is a hexadecimal value.

Moreover, MOV R0, #965; is an illegal statement because 965>255, the highest decimal value that the microcontroller can operate on.

ADD instruction

The format for this instruction is as follows:

ADD A,source;

Note that the destination for this instruction has to be only Accumulator because all the Arithmetic operations usually take place in the accumulator.

MOV A, 19H;

MOV R2, 23H;

ADD A, R2;

The above instructions add 23H to 19H.

This can also be done by the following instructions:

MOV A, 19H;

ADD A, 23H;

Here, 23H is called as immediate operand.

Therefore, the usual format for any assembly instruction is

[label:] mnemonic [operands] [;comment]

  • The label field allows the program to refer to any instruction or group of instructions by name.
  • Comment filed begins with a ; (semicolon) indicator.

DB directive

It is used to define the 8-bit data. For defining

  • Decimal number, “D” after the decimal number can be used, though it is optional for decimal number.
  • binary number, use of “B” after that particular binary number is important.
  • hexadecimal number, use of “H” is important.
  • It also helps in converting any string into its ascii equivalent code.

HERE: DB “electronicsforgeeks”     ; ASCII characters

HERE1: DB 00110011B     ; BINARY (51 in decimal)

ORG directive

It is used to indicate the beginning of the address.

EQU directive

It is used to define a constant without occupying any address.

COUNT EQU 50

… … … … … … …

MOV R1, #COUNT

END directive

This indicates to the assembler the end of source (.src/.asm) file.

Note: Some assemblers use .ORG to define origin directive and .END to define the end of program. Check out the assembler that you use if this is the case with them or not.

Rules for labels in assembly language

The names used for labels in Assembly language programming consist of alphabetic letters in both uppercase as well as in lowercase, numbers from 0-9,special characters ?,  . ,@, _and $. The names usually start with an alphabetic letter and then other characters can be added.

CONDITIONAL LOOP AND JUMP instructions

JZ (Jump if A=0)

In this instruction, the content of A is checked. If it is equal to 0, it jumps to the target address.

MOV A, R0  ;A=R0

JZ OVER

If the content of A comes of to be zero, then assembler jumps to the instruction that has the label OVER.

JNZ( Jump if A!=0)

works in the same way as JZ works. The only difference is that the content of A having non-zero data leads to the jump.

DJNZ( Decrement and jump if register!=0)

Keeps on decrementing the value of register and jumps to the target address if data in that register is not equal to zero.

MOV R0, #5

MOV R1, #0

HERE: INC R1   ;increment register R1

MOV A, R1         ;moving incremented data of R1 into accumulator

DJNZ R0, HERE ; repeating the loop till R0 data becomes zero.

CJNE A, data1( Jump if A!=data1)

This instruction compares the data in A with data1 and jumps to the target location if data of A is not equal to data1.

JC(Jump if carry flag CY=1)

This condition leads to the jump to the target address if CY is equal to 1.

JNC(Jump if carry flag CY=0)

This condition leads to the jump to the target address if CY is equal to 0.

JB(Jump if bit=1)

This condition leads to the jump to the target address if bit is equal to 1.

JNB(Jump if bit=0)

This condition leads to the jump to the target address if bit is equal to 0.

JBC(Jump if bit=1 and clear bit)

This condition leads to the jump to the target address if bit is equal to 1 and then clears that bit.

Unconditional jump instructions

These jump instructions do not require any condition to get associated with them for the jump operation to take place.

In the 8051, there are two unconditional jump instructions: LJMP(Long jump) and SJMP(Short jump).

LJMP

It is a 3-byte instruction out of which first byte is the opcode and the second and third byte represent the address of the target location.This allows the jump to occur between any byte from 0000H to FFFFH.

SJMP

It is a 2-byte instruction in which first byte is the opcode and the second byte is the relative address of the target location.

The relative address of 00H to FFH can be divided into forward and backward jumps from -128 to 127 bytes of the memory relative to the address of the current PC. To calculate the target address, the second byte is added to the PC of the next instruction immediately below the jump.

CALL INSTRUCTIONS

LCALL(long call)

It is a 3-byte instruction out of which first byte is opcode and the other two bytes constitute the taget address of the subroutine with the subroutine located in any part of the 64K bytes address space.

RET instruction indicates the end of a sub-routine. Hence, it has to be placed at the the end of each sub-routine.

ACALL(Absolute Call)

It is a 2-byte instruction in contrast to LCALL. Since, it is a 2-byte instruction, the target address of the sub-routine must be within 2K bytes because 11 bits of the  2 bytes are used for the address.

This is not the end. Too much is left in assembly, just wait for my upcoming posts.

Advertisements

1 Comment

Filed under Embedded Systems

Hello world!


images


Are you an E.C.E. student who wants to prepare for GATE, learn about microcontrollers and embedded systems, want to give yourself a push to achieve higher. Then your wait is over. From this post onwards, I am all set to give my tutorials that will not only clear your basics concepts but will also help you in applying them. This blog will prove to be ideal for all beginners in the field of engineering and will help you in fast and accurate learning.

So guys, Happy Blogging and Happy learning!

Feeling privileged,

Jyoti Dixit

Leave a comment

Filed under Embedded Systems