on Saturday, 14 September 2013
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.


MOV:  It is used to move the data. In fact it copies the data.
Mov Destination, source

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

CLR: CLR instruction is used to clear the data.
CLR source

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

Increment:  INC instruction increments the value by 1
INC source

source may be any register or address location
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

Decrement: DEC it decrements the value by one
DEC source  

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: CPL is used to compliment the value. Compliment is nothing but changing 1 to 0 and 0 to 1.
Compliment of 1001 is 0110.

Example: compliment the binary value 10010111 i.e 97 hexadecimal value
Mov A, # 97H
Now the result will be 01101000

Example: Perform 2's compliment on 97h
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


Addition: There are 2 addition instructions we have they are ADD and ADDC
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

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

Example: Adding 10 and 12 decimal numbers
Mov A,#10
Mov R1,#12
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 8D

                      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 R4

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

Multiplication: MUL instruction is used to perform multiplication

Result MSB will be stored in B and LSB in A

Example:Multiply 63h and Dh
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

Division : To perform this operation we use DIV

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

Example: Divide 24 by 12
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.



dhulipala saigopal said...

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 ?

kateyy said...

The Bestreplica watches. Here you can find almost swiss brand replica
watches.Replica watches,one of the most famous brands,best
,Specialities watch for sale,Fast delivery and free shipping!

madin said...

Every time you stop outside that glass window showing those glittering, stunning Rolex watches. This is a heartfelt desire to wrap your wrist with this most well- known name,best wathes uk. There is good news for you. Now, you need not to spend those big bucks for acquiring your dream watch. Your answer is Replica watches. Yes come here buy replica watches UK

Sophie Grace said...

Thank you for sharing your knowledge and experience with me. Nonton Drama Korea

Post a Comment