Ex.
No: 1 Write an assembly language program for 8 bit and 16 bit addition with and without
carry
Aim: To write an assembly language program to add two 8 bit and 16 bit numbers with and without
carry
Apparatus required: 1. Microcontroller 8051 kit with clock frequency 12MHz.
                    2. Microcontroller kit manual
a) Addition of two 8 bit numbers without carry
Algorithm:
1. Load the first data from memory to accumulator using DPTR register as address pointer.
2. Move the accumulator content into R0 register.
3. Load the second data from memory to accumulator using DPTR register as address pointer
4. Add the content of R0 register with Accumulator
5. Store the result in memory
6. Stop.
Flowchart:
Program:
 Addres                     Mnemonics
             Label                               Hexcode                   Comment
   s                   Opcode Operand
  4000                 MOV     DPTR, #4200         90      Move the given 16 bit data to Data Pointer
  4001                                             42
  4002                                             00
                                                           Move the contents whose address stored in
  4003                 MOVX      A, @DPTR          E0
                                                           DPTR to A register
  4004                 MOV       R0, A                     Move the contents of A register to R0
  4005                 INC       DPTR              A3      Increment Data pointer
                                                           Move the contents whose address stored in
  4006                 MOVX      A, @DPTR          E0
                                                           DPTR to A register
  4007                 ADD       A, R0                     Add the contents of R0 with Accumulator
  4008                 INC       DPTR              A3      Increment Data pointer
                                                           Move the result from A register to the
  4009                 MOVX      @DPTR, A          F0
                                                           memory address stored in DPTR
  400A       Halt:     SJMP         Halt              80      Short jump here (HLT)
  400B                                                FE
                                    I/P and O/P Verification Table
                                    Input                    Output
                           Address          Data     Address          Data
                             4200           49H       4202            BEH
                             4201           75H
Result:
The assembly language programs for 8 bit and 16 bit addition with and without carry are written and
executed successfully with the given test data.
b) Addition of two 8 bit numbers with carry
Algorithm:
1. Load the first data from memory to R0 through accumulator using DPTR register as address
pointer.
2. Load the second data from memory to accumulator using DPTR register as address pointer
3. Add the content of R0 register with Accumulator.
4. Check if carry arrives or not. If so store the carry in R1 register
4. Store the result and carry in memory.
5. Stop.
Flowchart:
Program:
 Addres                    Mnemonics
            Label                                 Hexcode   Comment
   s                  Opcode Operand
                      MOV     R1, #00H
                      MOV     DPTR, #4200
                      MOVX A, @DPTR
                      MOV     R0, A
                      INC     DPTR
                      MOVX A, @DPTR
                      ADD     A, R0
                      JNC     Loop1
                      INC     R1
           Loop1:     INC     DPTR
                      MOVX @DPTR, A
                      INC     DPTR
                      MOV     A,R1
                      MOVX @DPTR, A
            Halt:     SJMP    Halt
c) Addition of two 16 bit numbers without carry
 Addres                    Mnemonics
            Label                              Hexcode   Comment
   s                  Opcode Operand
                      MOV     DPTR, #4200
                      MOVX A, @DPTR
                      MOV     R0, A
                      MOV     DPTR, #4202
                      MOVX A, @DPTR
                      ADD     A, R0
                      MOV     DPTR, #4250
                      MOVX @DPTR, A
                      MOV     DPTR, #4201
                      MOVX A, @DPTR
                      MOV     R0, A
                      MOV     DPTR, #4203
                      MOVX A, @DPTR
                      ADDC A, R0
                      MOV     DPTR, #4251
                      MOVX @DPTR, A
             Halt:    SJMP    Halt
d) Addition of two 16 bit numbers with carry
 Addres                    Mnemonics
            Label                              Hexcode   Comment
   s                  Opcode Operand
                      MOV     R1, #00H
                      MOV     DPTR, #4200
                      MOVX A, @DPTR
                      MOV     R0, A
                      MOV     DPTR, #4202
                      MOVX A, @DPTR
                      ADD     A, R0
                      MOV     DPTR, #4250
                      MOVX @DPTR, A
                      MOV     DPTR, #4201
                      MOVX A, @DPTR
                      MOV     R0, A
                      MOV     DPTR, #4203
                      MOVX A, @DPTR
                      ADDC A, R0
                      JNC     Loop1
                      INC     R1
            Loop1:    MOV     DPTR, #4251
                        MOVX     @DPTR, A
                        INC      DPTR
                        MOV      A,R1
                        MOVX     @DPTR, A
             Halt:      SJMP     Halt
e) 8 bit Subtraction
 Addres                      Mnemonics
            Label                             Hexcode                    Comment
   s                    Opcode Operand
                        MOV     R1, #00H
                        MOV     DPTR, #4200
                        MOVX A, @DPTR
                        MOV     R0, A
                        INC     DPTR
                        MOVX A, @DPTR
                        SUBB    A, R0
                        INC     DPTR
                        MOVX @DPTR, A
             Halt:      SJMP    Halt
8 bit Subtraction (alternative method)
 Addres                      Mnemonics
            Label                             Hexcode                    Comment
   s                    Opcode Operand
                        MOV     R1, #00H
                        MOV     DPTR, #4200
                        MOVX A, @DPTR
                        MOV     R0, A
                        INC     DPTR
                        MOVX A, @DPTR
                        SUBB    A, R0
                        JNC     Loop1
                        CPL     A                       Take one’s complement
                        INC     A                       Find two’s complement
                        INC     R1                      Make R1 as 01 to represent a negative result
            Loop1:      INC     DPTR
                        MOVX @DPTR, A
                        INC     DPTR
                        MOV     A,R1                    Store the sign value
                        MOVX @DPTR, A
             Halt:      SJMP    Halt
f) 16 bit Subtraction
 Addres                   Mnemonics
            Label                           Hexcode     Comment
   s                 Opcode Operand
                     MOV     DPTR, #4200
                     MOVX A, @DPTR
                     MOV     R0, A
                     MOV     DPTR, #4202
                     MOVX A, @DPTR
                     SUBB    A, R0
                     MOV     DPTR, #4250
                     MOVX @DPTR, A
                     MOV     DPTR, #4201
                     MOVX A, @DPTR
                     MOV     R0, A
                     MOV     DPTR, #4203
                     MOVX A, @DPTR
                     SUBB    A, R0
                     MOV     DPTR, #4251
                     MOVX @DPTR, A
            Halt:    SJMP    Halt
2) Waveform Generator
a) Stair Case Generator
 Addres                    Mnemonics
            Label                             Hexcode    Comment
   s                 Opcode Operand
                     MOV    A, #80H
                     MOV    DPTR, CWR
                     MOVX @DPTR, A
                     MOV    DPTR, #Port A
            Again:   MOV    R0, #0AH
                     CLR    A
           Repeat:   MOV    @DPTR, A
                     INC    A
                     ACALL Delay1
                     DJNZ   R0, Repeat
                     SJMP   Again
            Halt:    SJMP   Halt
b) Sine Wave Generator
 Addres                    Mnemonics
            Label                             Hexcode    Comment
   s                 Opcode Operand
                     MOV    A, #80H
                     MOV    DPTR, CWR
                     MOVX      @DPTR, A
           Again:    MOV       R1, #00
                     MOV       R2, #42
                     MOV       R0, #46
           Repeat:   MOV       DPL, R1
                     MOV       DPH, R2
                     MOVX      A, @DPTR
                     INC       DPTR
                     MOV       R1, DPL
                     MOV       R2, DPH
                     MOV       DPTR, #Port A
                     MOV       @DPTR, A
                     ACALL     Delay1
                     DJNZ      R0, Repeat
                     SJMP      Again
            Halt:    SJMP      Halt
c) Square Wave Generator
 Addres                    Mnemonics
           Label                               Hexcode   Comment
   s                 Opcode Operand
                     MOV    A, #80H
                     MOV    DPTR, CWR
                     MOVX @DPTR, A
           Again:    MOV    DPTR, #Port A
                     MOV    A, #00H
                     MOV    @DPTR, A
                     ACALL Delay1
                     MOV    A, #01H
                     MOV    @DPTR, A
                     ACALL Delay1
                     SJMP   Again
            Halt:    SJMP   Halt
d) Triangular Wave generator
 Addres                    Mnemonics
           Label                               Hexcode   Comment
   s                 Opcode Operand
                     MOV    A, #80H
                     MOV    DPTR, CWR
                     MOVX @DPTR, A
                     MOV    DPTR, #Port A
            Start:   MOV    A, #00H
Again:    MOV    @DPTR, A
          INC    A
          JNZ    Again
          MOV    A, #0FFH
Repeat:   MOV    @DPTR, A
          DEC    A
          JNZ    Repeat
          SJMP   Start
 Halt:    SJMP   Halt