Instruction is nothing but a command to perform certain task. As we have seen in our previous post the MOV instruction , it is used to move(copy) the data to any register or any address in the memory. We have seen ADD , SUBB in our previous posts. There are 255 such instructions as a whole but only few of them will be used frequently to write a program in Assembly level language.

###

Destination and source can be any address locations or any registers

Mov R0, #25 ; moving 25 into R0

Mov R1,R5 ; moving data in R5 to R1, infact it copies data from R5 to R1.

After this operation R5 remains its previous value

Mov R0,# 10 ; R0=10

Mov R1,# 12 ; R1=12

Mov R0, R1 ; moving(copying) R1 into R0

i.e now new value of R0= 12 and R1= 12 remains same

Mov R0, 25h

Notice that we dint included # , it means 25h is the RAM address location.we are moving the data in the address location of 25h into the R0 register

Mov R0, #25h

Here # is included it means we are moving 25h value into the register R0

source can be A, C or any address

CLR A ; now A=0

CLR C ; Carry flag will be zero

CLR 25h ; the data in the 25h address will be cleared,

note we dint included # indicates that it is an address

INC A ; if the previous value of A is some x then new value will be x+1

INC R0 ; R0 value will be incremented by one

Mov A,# 10 ; A=10

INC A ; now A will be 11

INC A ; now A will be 12

source can be address or register

DEC A ; if A contains value some x then new value will be x-1

Mov A,#12 ; A=12

DEC A ; A=11

DEC A ; A=10

Compliment of 1001 is 0110.

Mov A, # 97H

CPL A

Now the result will be 01101000

2's compliment = 1's compliment + 1 (1's compliment is nothing but just CPL)

Mov A, #97h

CPL A ; 1's compliment

INC A ; 1's compliment +1 i.e 2's compliment

Here the content of 'A' and the content of 'source' register will be added and result will be saved in 'A' i.e

A=A+source

Note that the destination register should be 'A' and source can be any general register or any value.

Mov A,#10

Mov R1,#12

ADD A,R1

Here 10 and 12 will be added and the result 22 will be saved in A

Above program can be rewrite as follows

Mov A,#10

ADD A,#12

Result 22 will be saved in register A

This perform the addition of data in 'A' and the 'Source' along with the previous generated 'carry'.

The destination should be register A and source may be any value or any general register or any address location.

1

3C E7

+ 3B 8D

---------

78 74

---------

Note that general registers of 8051 are all 8 bits wide. so we first add LSB(E7 + 8D) first and then MSB(3C + 3B). When we add LSB a carry will be generated , which will be stored in psw register in 'CY' flag(psw.7).

Mov R0,#E7H ;R0=E7

Mov A,#8DH ;A=8D

Add A,R0 ;A+R0 = E7+8D =74, result will be saved in A

and carry will be generated so CY=1

Mov R5,A ;We need to move LSB into R5 as question says

Mov R0,#3CH ; R0=3C

Mov A,#3BH ;A=3B

Addc A,R0 ;A+R0+C=3C + 3B+1= 78 ,note adding with previous carry

Mov R4,A ;Moving MSB into register R4 as question says

As we have seen two instructions for additions i.e for normal addition and for addition with carry, but in subtraction we have only one instruction to subtract with borrow and without borrow.

It performs something like this A- source -CY and result will be saved in A

Mov R0,#12H ;copy 12h into register R0

Mov A,#20H ; copy 20h into register A

CLR C ; If previous any carry/borrow was generated then clear it

SUBB A,R0 ; 20h-12h-0( as cleared carry) and result saved in A. No carry is generated

Mov R5,A ; move(copy) result in R5

Important note , subtraction is actually done in 2's compliment method. i.e if CY =0 then the result will be positive and if CY=1 then result will be negative. For example if 15-10=5 then CY=0 so result 5 is positive and if 10-15=-5 then CY=1 so result 5 is negative. In order to know the result is positive or negative we need to check the carry flag of psw register(psw.7 or C).

When we get negative result i.e CY=1 then we need to perform 2's compliment on the result to get the actual answer. 2's compliment is nothing but 1's compliment plus one as we have seen before.

Mov A, #1Ah ; A=1A hexadecimal value

SUBB A, # 5Ch ; 1A-5C , it obvious that borrow will be generated i.e CY=1

CPL A ; As i said before, if CY=1 then we need to

perform 2's compliment on result. CPL A i.e compliment A

nothing but 1's compliment

INC A ; increment A i.e it adds 1 to the A i.e 1's compliment +1= 2's compliment

Result MSB will be stored in B and LSB in A

Mov A, # 0DH ; Note here '0' is inbetween # and D in order to make known that D is a hex value not letter

Mov B, # 63H

MUL AB ; result will be 0507 so B= 05 and A=07

Divide A by B , result quotient will be saved in A and remainder in B

Mov A, # 24 ;dividend

Mov B, # 12 ;divisor

DIV AB ; result quotient '2' saved in A and remainder '0' saved in B i.e A=2 and B=0

In our next post we will look at the jump , loop instructions along with compare instructions.

###
**GENERAL INSTRUCTIONS:**

__It is used to move the data. In fact it copies the data.__**MOV:****Mov Destination, source**Destination and source can be any address locations or any registers

__Example:__Mov R0, #25 ; moving 25 into R0

Mov R1,R5 ; moving data in R5 to R1, infact it copies data from R5 to R1.

After this operation R5 remains its previous value

__Example:__Mov R0,# 10 ; R0=10

Mov R1,# 12 ; R1=12

Mov R0, R1 ; moving(copying) R1 into R0

i.e now new value of R0= 12 and R1= 12 remains same

__Example:__Mov R0, 25h

Notice that we dint included # , it means 25h is the RAM address location.we are moving the data in the address location of 25h into the R0 register

Mov R0, #25h

Here # is included it means we are moving 25h value into the register R0

__CLR instruction is used to clear the data.__**CLR:****CLR source**source can be A, C or any address

__Example:__CLR A ; now A=0

CLR C ; Carry flag will be zero

CLR 25h ; the data in the 25h address will be cleared,

note we dint included # indicates that it is an address

__INC instruction increments the value by 1__**Increment:****INC source****source may be any register or address location**

__Example:__INC A ; if the previous value of A is some x then new value will be x+1

INC R0 ; R0 value will be incremented by one

__Program Example:__Mov A,# 10 ; A=10

INC A ; now A will be 11

INC A ; now A will be 12

__DEC it decrements the value by one__**Decrement**:**DEC source**source can be address or register

DEC A ; if A contains value some x then new value will be x-1

__Example:__Mov A,#12 ; A=12

DEC A ; A=11

DEC A ; A=10

**CPL is used to compliment the value. Compliment is nothing but changing 1 to 0 and 0 to 1.**__Compliment:__Compliment of 1001 is 0110.

**CPL A**

__Example:__compliment the binary value 10010111 i.e 97 hexadecimal valueMov A, # 97H

CPL A

Now the result will be 01101000

__Example:__Perform 2's compliment on 97h2's compliment = 1's compliment + 1 (1's compliment is nothing but just CPL)

Mov A, #97h

CPL A ; 1's compliment

INC A ; 1's compliment +1 i.e 2's compliment

### ARITHMETIC INSTRUCTIONS:

**There are 2 addition instructions we have they are ADD and ADDC**__Addition:____ADD:__As we have seen it before, it is used to add source and destination without carry.**ADD A, source**Here the content of 'A' and the content of 'source' register will be added and result will be saved in 'A' i.e

A=A+source

Note that the destination register should be 'A' and source can be any general register or any value.

__Example:__Adding 10 and 12 decimal numbersMov A,#10

Mov R1,#12

ADD A,R1

Here 10 and 12 will be added and the result 22 will be saved in A

Above program can be rewrite as follows

Mov A,#10

ADD A,#12

Result 22 will be saved in register A

__ADDC :__Adding with previous generated carry.**ADDC A, source**This perform the addition of data in 'A' and the 'Source' along with the previous generated 'carry'.

The destination should be register A and source may be any value or any general register or any address location.

__Example:__We want to add two 16bit hexadecimal numbers 3C E7 and 3B 8D1

3C E7

+ 3B 8D

---------

78 74

---------

Note that general registers of 8051 are all 8 bits wide. so we first add LSB(E7 + 8D) first and then MSB(3C + 3B). When we add LSB a carry will be generated , which will be stored in psw register in 'CY' flag(psw.7).

__Example:__Write a program to add two 16bit hexadecimal numbers 3C E7 and 3B 8D and save the LSB in register R5 and MSB in register R4Mov R0,#E7H ;R0=E7

Mov A,#8DH ;A=8D

Add A,R0 ;A+R0 = E7+8D =74, result will be saved in A

and carry will be generated so CY=1

Mov R5,A ;We need to move LSB into R5 as question says

Mov R0,#3CH ; R0=3C

Mov A,#3BH ;A=3B

Addc A,R0 ;A+R0+C=3C + 3B+1= 78 ,note adding with previous carry

Mov R4,A ;Moving MSB into register R4 as question says

**Substraction:**As we have seen two instructions for additions i.e for normal addition and for addition with carry, but in subtraction we have only one instruction to subtract with borrow and without borrow.

__SUBB:__Subtract with borrow. For carry and borrow it uses the same flag bit of psw.7 i.e C . If borrow is generated then C=1 if not generated then C=0.**SUBB A, source**It performs something like this A- source -CY and result will be saved in A

__Example:__Subtract 12H from 20H and save the result in R5( Here carry or borrow will be zero obviously)Mov R0,#12H ;copy 12h into register R0

Mov A,#20H ; copy 20h into register A

CLR C ; If previous any carry/borrow was generated then clear it

SUBB A,R0 ; 20h-12h-0( as cleared carry) and result saved in A. No carry is generated

Mov R5,A ; move(copy) result in R5

Important note , subtraction is actually done in 2's compliment method. i.e if CY =0 then the result will be positive and if CY=1 then result will be negative. For example if 15-10=5 then CY=0 so result 5 is positive and if 10-15=-5 then CY=1 so result 5 is negative. In order to know the result is positive or negative we need to check the carry flag of psw register(psw.7 or C).

When we get negative result i.e CY=1 then we need to perform 2's compliment on the result to get the actual answer. 2's compliment is nothing but 1's compliment plus one as we have seen before.

__Example:__Subtract 5C (hex) from 1A (hex) (i.e 26-92 in decimal)Mov A, #1Ah ; A=1A hexadecimal value

SUBB A, # 5Ch ; 1A-5C , it obvious that borrow will be generated i.e CY=1

CPL A ; As i said before, if CY=1 then we need to

perform 2's compliment on result. CPL A i.e compliment A

nothing but 1's compliment

INC A ; increment A i.e it adds 1 to the A i.e 1's compliment +1= 2's compliment

__MUL instruction is used to perform multiplication__**Multiplication:****MUL AB**; AxBResult MSB will be stored in B and LSB in A

__Example:__Multiply 63h and DhMov A, # 0DH ; Note here '0' is inbetween # and D in order to make known that D is a hex value not letter

Mov B, # 63H

MUL AB ; result will be 0507 so B= 05 and A=07

**To perform this operation we use DIV**__Division :__**DIV AB**Divide A by B , result quotient will be saved in A and remainder in B

__Example:__Divide 24 by 12Mov A, # 24 ;dividend

Mov B, # 12 ;divisor

DIV AB ; result quotient '2' saved in A and remainder '0' saved in B i.e A=2 and B=0

In our next post we will look at the jump , loop instructions along with compare instructions.

AUTHOR: YOUSTRON SIC

## 1 comments:

In the above example for ADDC ,a value of 1 is carried after adding LSB and then CY became 1.Now where will the carried value 1 will be stored ?

## Post a Comment