Assembly Language Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

PROGRAM FOR ADDITION:

1)ADDITION OF TWO 8 BIT NUMBERS


A] AIM: Add two numbers. First number is 50H
and second number is 60H.
Algorithm:
Step 1: Start
Step 2: Load the first number in A Register
Step 3: Add the 2nd number with 1st number and store the
result.
Step 4: Stop
Flowchart:

PREPARED BY: SNEHAL


GORDE
Program:
ORG 00H
MOV A, #50H
ADD A, #60H
END

B] AIM: Suppose two bytes are 66H and 95H


stored in register R2 and R3. Add two numbers.
Store the result in R4.
Algorithm:
Step 1: Start
Step 2: Load the first number in R2 Register
Step 3: Load the second number in R3
Register.
Step 4: Add the 2nd number with 1st number and store
the result in R4.
Step 5: Stop
Flowchart:

Program:
ORG 00H
MOV A, R2
ADD A, R3
MOV R4, A
END
C] AIM: Suppose two bytes are stored in memory
locations 3000H and 3001H. Add two numbers
from external memory locations and store the
result in memory location 3002H of external
memory.
Algorithm:
Step 1: Start
Step 2: Initialize memory pointer using DPTR register
with 3000H
Step 3: Load the 1st number from memory
location 3000H.
Step 4: Increment memory pointer by 1.
Step 5: Load the 2nd number from memory
location 3001H.
Step 6: Add both numbers
Step 7: Store the result in memory location 3002H by
incrementing memory pointer
Step 8: Stop.
Flowchart:
Program:
ORG 0000H
MOV DPTR, #3000H
MOVX A, @DPTR
MOV R0, A
INC DPTR
MOVX A, @DPTR
ADD A, R0
INC DPTR
MOVX @DPTR, A
END

D] AIM: Suppose two bytes are 66H and 95H


stored in register R2 and R3. Add two numbers.
Assume the result is greater than 8 bit, hence
LSB of result is stored in R4 and MSB of result
is stored in R5.
Algorithm:
Step 1: Select Bank 1
Step 2: Initialize MSB counter with 00H.
Step 3: Load the 1st number from R2 to Accumulator.
Step 4: Add 2nd number in R3 with Accumulator.
Step 5: If result < 8 bit, then goes to step 7.
Step 6: Increment MSB counter by 1.
Step 7: Store the result i.e. LSB to R4 and MSB to R5
Step 8: Stop.
Flowchart:
Program:
ORG 00H
MOV R5, #00H
MOV A, R2
ADD A, R3
JNC DN
INC R5
DN: MOV R4, A
END

2)ADDITION OF TWO 16 BIT NUMBERS


A] AIM: Add two 16 bit numbers 1234H and
4321H stored in internal bank registers R0,
R1, R2 and R3. Store the result in R4 and
R5.
Program:
ORG 00H
MOV R6, #00H
MOV A, R0
ADD A, R2
MOV R4, A
MOV A, R1
ADDC A, R3
JNC DOWN
INC R6
DOWN: MOV R5, A
END

SUBTRACTION OF TWO 8 BIT NUMBERS


A] AIM: Suppose two data bytes are
60H and 50H. Subtract 2nd number from 1st
number.
Program:
ORG 00H
MOV A, #60H
SUBB A, #50H
END

B] AIM: Suppose two bytes are 96H and 65H


stored in register R2 and R3 of bank 1.
Subtract two numbers. Store the result in
R4 of bank 1.
Program:
ORG 00H
MOV A, R2
SUBB A, R3
MOV R4, A
END
C] AIM: Suppose two bytes are stored in
memory locations 3000H and 3001H. Subtract
two numbers from external memory locations
and store the result in memory location
3002H of external memory.
Program:
ORG 0000H
MOV DPTR, #3000H
MOVX A, @DPTR
MOV R0, A
INC DPTR
MOVX A, @DPTR
SUBB A, R0
INC DPTR
MOVX @DPTR, A
END

3) SUBTRACTION OF TWO 16
BIT NUMBERS
AIM: Assume two 16 bit numbers 1234H and
4321H stored in internal bank registers
R0,R1,R2 and R3. After Subtraction, store
the result in R4 and R5.
Program:
ORG 00H
MOV A, R0
SUBB A, R2
MOV R4, A
MOV A, R1
SUBB A, R3
MOV R5, A
END

4) SUM OF SERIES OF 8 BIT NUMBERS


AIM:
Write an ALP for sum of array of ten bytes stored in internal
RAM location from 50H. Assume sum is 8 bit only. Store the
result in memory location 5AH.

PROGRAM:
ORG 00H
MOV R0, #0AH
MOV R1, #50H
MOV A, #00H
UP: ADD A, @R1
INC R1
DJNZ R0, UP
MOV @R1, A
END

AIM:
Write an ALP for performing addition of three 8
bit numbers stored in internal RAM location from
60H, 61H and 62H. Store the carry and sum at
63H and 64H location. (Assume data)

PROGRAM:
ORG 00H
MOV R0, #03H
MOV R1, #60H
MOV A, #00H
MOV R7, #00H
UP: ADD A, @R1
JNC NEXT
INC R7
NEXT: INC R0
DJNZ R2, UP
MOV 63H, A
MOV 64H, R7
END
AIM:
Write an ALP to calculate sum of five consecutive
numbers stored in internal RAM location from
20H. Store the lower byte at memory location 25H
and higher byte at 26H.

PROGRAM:
ORG 00H
MOV R0, #05H
MOV R1, #20H
MOV A, #00H
MOV R7, #00H
UP: ADDC A, @R1
JNC NEXT
INC R7
NEXT: INC R1
DJNZ R0, UP
MOV @R1, A
INC R1
MOV A, R7
MOV @R1, A
END

6) MULTIPLICATION OF TWO 8 BIT


NUMBERS
A] AIM: Suppose two bytes are 78H and 48H.
Multiply the two numbers stored in A and B
register.
Program:
ORG 00H
MOV A, #78H
MOV B, #48H
MUL AB
END

B] AIM: Suppose multiplicand is stored in R0


and multiplier is stored in R1. Multiply the two
numbers. Store LSB of result in R2 and MSB
in R3.
Program:
ORG 00H
MOV R0, #78H
MOV R1, #48H
MOV A, R0
MOV B, R1
MUL AB
MOV R2, A
MOV R3, B
END

C] AIM: Suppose multiplicand is stored in


internal memory location 40H and
multiplier is stored in memory location 41H.
Multiply the two numbers. Store LSB of
result in 42H and MSB in 43H.
Program:
ORG 00H
MOV A, 40H
MOV B, 41H
MUL AB
MOV 42H, A
MOV 43H, B
END

D] AIM: Suppose multiplicand is stored in


external memory location 3000H and
multiplier is stored in memory location
3001H. Multiply the two numbers. Store LSB
of result in 3002H and MSB in 3003H.
Program:
ORG 00H
MOV DPTR, #3000H
MOVX A, @DPTR
MOV B, A
INC DPTR
MOVX A, @DPTR
MUL AB
INC DPTR
MOVX @DPTR, A
MOV A, B
INC DPTR
MOVX @DPTR, A
END
7) DIVISION OF TWO 8 BIT NUMBERS
A] AIM: Suppose two bytes are 78H and 48H.
Divide the two numbers stored in A and B
register.
Program:
ORG 00H
MOV A, #78H
MOV B, #48H
DIV AB
END
B] AIM: Suppose dividend is stored in R0 and
divisor is stored in R1. Divide the two numbers.
Store Quotient of result in R2 and
remainder in R3.
Program:
ORG 00H
MOV R0, #78H
MOV R1, #48H
MOV A, R0
MOV B, R1
DIV AB
MOV R2, A
MOV R3, B
END

C] AIM: Suppose dividend is stored in internal


memory location 40H and divisor is stored
in memory location 41H. Divide the two
numbers. Store Q in 42H and R in 43H.
Program:
ORG 00H
MOV A, 40H
MOV B, 41H
DIV AB
MOV 42H, A
MOV 43H, B
END

D] AIM: Suppose dividend is stored in


external memory location 3000H and divisor
is stored in memory location 3001H. Divide
the two numbers. Store Q in 3002H and R
in 3003H.
Program:
ORG 00H
MOV DPTR, #3000H
MOVX A, @DPTR
MOV B, A
INC DPTR
MOVX A, @DPTR
DIV AB
INC DPTR
MOVX @DPTR, A
MOV A, B
INC DPTR
MOVX @DPTR, A
END

LARGEST NUMBER FROM ARRAY


AIM:
Write an ALP for finding largest no. among given array of ten
bytes stored in internal RAM location from 20H. Store the
largest no. in memory location 60H.

PROGRAM:
ORG 00H
MOV R0, #0AH ; Initialize byte counter
PREPARED BY: SNEHAL
GORDE
MOV R1,#20H ; Initialize memory pointer
DEC R0 ; Decrement byte counter by 1
MOV 60H, @R1 ; Store the no. in memory location 60H
UP: INC R1 ; Increment memory pointer by 1
MOV A, @R1 ; Read the next no.
CJNE A,60H,HERE ; If 1st no.is not = next no.,then go to
HERE
AJMP NEXT ; Orelse go to next
HERE: JC NEXT ; If next no. is < than 1st no. then go to
NEXT
MOV 60H,A ; Orelse replace next no.with 1st no.
NEXT: DJNZ R0,UP ; Decrement byte counter by 1,if
byte counter is not = 0,then go to UP
END

LARGEST NUMBER FROM ARRAY IN EXTERNAL


MEMORY
AIM:
Write an ALP for finding largest no. among given array of ten
bytes stored in external memory location from 3000H. Store
the largest no. in memory location 300AH.

PROGRAM:
ORG 00H
MOV R0, #0AH ; Initialize byte counter
MOV DPTR, #3000H ; Initialize memory pointer
DEC R0 ; Decrement byte counter by 1
MOVX A, @DPTR ; Load the number in accumulator
MOV 40H, A ; Store the no. in memory location 40H
PREPARED BY: SNEHAL
GORDE
UP: INC DPTR ; Increment memory pointer by 1
MOVX A, @DPTR ; Read the next no.
CJNE A,40H,HERE ; If 1st no.is not = next no.,then go to
HERE
AJMP NEXT ; Orelse go to next
HERE: JC NEXT ; If next no. is < than 1st no. then
go to NEXT
MOV 40H,A ; Or else replace next no.with 1st no.
NEXT: DJNZ R1,UP ; Decrement byte counter by 1,if
byte counter is not=0,then go to UP
INC DPTR ; Increment memory pointer by 1
MOV A, 40H
MOVX @DPTR, A ; Store the result in external memory
END

SMALLEST NUMBER FROM ARRAY


AIM:
Write an ALP for finding smallest no. among given array of
ten bytes stored in internal RAM location from 20H. Store
the smallest no. in memory location 60H.

PROGRAM:
ORG 00H
MOV R0, #0AH ; Initialize byte counter
MOV R1, #20H ; Initialize memory pointer
DEC R0 ; Decrement byte counter by 1
MOV 60H, @R1 ; Store the no. in memory location 60H
UP: INC R1 ; Increment memory pointer by 1
PREPARED BY: SNEHAL
GORDE
MOV A, @R1 ; Read the next no.
CJNE A,60H,HERE ; If no.is not = next no.,then go to
HERE
AJMP NEXT ; Or else go to next
HERE: JNC NEXT ; If next no. > than 1st no. then go
to NEXT
MOV 60H,A ; Or else replace next no. with 1 no.
st

NEXT: DJNZ R0,UP ; Decrement byte counter by 1,if


byte counter is not = 0,then go to UP
END

SMALLEST NUMBER FROM ARRAY IN EXTERNAL


MEMORY
AIM:
Write an ALP for finding largest no. among given array of ten
bytes stored in external memory location from 3000H. Store
the largest no. in memory location 300AH.

PROGRAM:
ORG 00H
MOV R0, #0AH ; Initialize byte counter
MOV DPTR, #3000H ; Initialize memory pointer
DEC R0 ; Decrement byte counter by 1
MOVX A, @DPTR ; Load the number in accumulator
MOV 40H, A ; Store the no. in memory location 40H
UP: INC DPTR ; Increment memory pointer by 1
MOVX A, @DPTR ; Read the next no.
CJNE A,40H,HERE ; If 1st no.is not = next no.,then go to
HERE
PREPARED BY: SNEHAL
GORDE
AJMP NEXT ; Or else go to next
HERE: JNC NEXT ; If next no. is > than 1st no. then
go to NEXT
MOV 40H,A ; Orelse replace next no.with 1st no.
NEXT: DJNZ R1,UP ; Decrement byte counter by 1,if
byte counter is not = 0,then go to UP
INC DPTR ; Increment memory pointer by 1
MOV A, 40H
MOVX @DPTR, A ; Store the result in externalmemory
END

BLOCK TRANSFER (IN INTERNAL MEMORY)


AIM:
Write an ALP for transferring block of 10 bytes stored in internal
RAM location from address 40H. Starting address of destination
block is 50H.
PROGRAM:
ORG 00H
MOV R3, #0AH ; Initialize byte counter
MOV R0, #40H ; Initialize memory pointer for source
array
MOV R1, #50H ; Initialize memory pointer for
destination array
UP: MOV A, @R0 ; Read first no.from source
array
MOV @R1, A ; Write first no.to dest. Array
INC R0 ; Increment source memory pointer
by 1
INC R1 ; Increment dest. memory pointer
PREPARED BY: SNEHAL
GORDE
by 1
DJNZ R3, UP ; Decrement byte counter, if byte
counter is not= 0, then go to UP
END ; Stop

BLOCK TRANSFER (IN EXTERNAL MEMORY)


AIM:
Write an ALP for tranfering block of 10 bytes stored in external
memory location from address 40H. Starting address of destination
block is 50H.
PROGRAM:
ORG 00H
MOV R3, #0AH ; Initialize byte counter
MOV R0, #40H ; Initialize memory pointer for source
array
MOV R1, #50H ; Initialize memory pointer for
Destination array
UP: MOV A, @R0 ; Read first no.from source array
MOVX @R1, A ; Write first no.to dest. Array
INC R0 ; Increment source memory pointer by 1
INC R1 ; Increment dest. memory pointer by 1
DJNZ R3, UP ; Decrement byte counter, if byte
counter is not= 0, then go to UP
END ; Stop

BLOCK EXCHANGE
AIM:
Write an ALP for exchanging block of 10 bytes stored in internal
RAM location from address 40H. Starting address of destination
block is 50H.
PREPARED BY: SNEHAL
GORDE
PROGRAM:
ORG 00H
MOV R3, #0AH ; Initialize byte counter
MOV R0, #40H ; Initialize memory pointer for source
array
MOV R1, #50H ; Initialize memory pointer for
destination array
UP: XCH A, @R0 ; Read first no.from source Array
XCH A, @R1 ; Read first no.from destination array
XCH A, @R0 ; Exchange source and dest. array
INC R0 ; Increment source memory pointer
by 1
INC R1 ; Increment dest. memory pointer
by 1
DJNZ R3, UP ; Decrement byte counter, if byte
counter is not= 0, then go to UP
END ; Stop

PREPARED BY: SNEHAL


GORDE
ARRANGE NUMBERS IN DESCENDING ORDER

PREPARED BY: SNEHAL


GORDE
PREPARED BY: SNEHAL
GORDE
AIM:
Write an ALP for arranging numbers in Descending order in
external memory location from 3000H.
PROGRAM:
ORG 00H
MOV R0, #05H ; Initialize pass counter
UP1:MOV DPTR, #3000H ; Initialize memory pointer
MOV R1, #05H ; Initialize byte counter
UP: MOV R2, DPL ; Save the lower byte address
MOVX A, @DPTR ; Read the number from array
MOV 0F0H, A ; Copy the number to B
INC DPTR ; Increment the memory pointer
MOVX A, @DPTR ; Read the next number from array
CJNE A,0F0, DN ; Compare no. with next no.
AJMP DW1
DN: JC DW1 ; If 2st no.< 1st no. then go to DW1
MOV DPL, R2 ; Else exchange the no. with next no.
MOVX @DPTR, A
INC DPTR
MOV A, 0F0H
MOVX @DPTR, A
DW1: DJNZ R1, UP ; Decrement byte counter, if not 0
go to UP
DJNZ R0, UP1 ; Decrement pass counter, if not 0
go to UP1
END ; Stop

PREPARED BY: SNEHAL


GORDE
ARRANGE NUMBERS IN ASCENDING ORDER
AIM:
Write an ALP for arranging numbers in Ascending order in external
memory location from 3000H.
PROGRAM:
ORG 00H
MOV R0, #05H ; Initialize pass counter
UP1:MOV DPTR, #3000H ; Initialize memory pointer
MOV R1, #05H ; Initialize byte counter
UP: MOV R2, DPL ; Save the lower byte address
MOVX A, @DPTR ; Read the number from array
MOV 0F0H, A ; Copy the number to B
INC DPTR ; Increment the memory pointer
MOVX A, @DPTR ; Read the next number from array
CJNE A,0F0, DN ; Compare no. with next no.
AJMP DW1
DN:JNC DW1 ; If 2nd no.> 1st no. then go to DW1
MOV DPL, R2 ; Else exchange the no. with next no.
MOVX @DPTR, A
INC DPTR
MOV A, 0F0H
MOVX @DPTR, A
DW1:DJNZ R1, UP ; Decrement byte counter, if not 0
go to UP
DJNZ R0, UP1 ; Decrement pass counter, if not 0
go to UP1
END ; Stop

PREPARED BY: SNEHAL


GORDE
ADDITION OF BCD NUMBERS
Add two BCD numbers. First number is 50H and
second number is 60H.
Program:
ORG 00H
MOV A, #50H
ADD A, #60H
DAA
END

Suppose two BCD bytes are 66H and 95H stored


in register R2 and R3. Add two numbers. Store
the result in R4.
Program:
ORG 00H
MOV A, R2
ADD A, R3
DAA
MOV R4, A
END

PREPARED BY: SNEHAL


GORDE
Suppose two BCD bytes are stored in memory
locations 3000H and 3001H. Add two numbers
from external memory locations and store the
result in memory location 3002H of external
memory.
Program:
ORG 0000H
MOV DPTR, #3000H
MOVX A, @DPTR
MOV R0, A
INC DPTR
MOVX A, @DPTR
ADD A, R0
DAA
INC DPTR
MOVX @DPTR, A
END

Suppose two BCD bytes are 66H and 95H stored in


register R2 and R3. Add two numbers. Assume the
result is greater than 8 bit, hence LSB of
result is stored in R4 and MSB of result is
stored in R5.
Program:
ORG 00H
MOV R5, #00H
MOV A, R2
ADD A, R3
DAA
JNC DN
INC R5
DN: MOV R4, A
END

Add two 16 bit BCD numbers 1234H and 4321H


stored in internal bank registers R0, R1, R2 and
R3. Store the result in R4 and R5.
Program:
ORG 00H
MOV R6, #00H
MOV A, R0
ADD A, R2
MOV R4, A
MOV A, R1
ADDC A, R3
DAA
JNC DOWN
INC R6
DOWN: MOV R5, A
END

FINDING 2’s COMPLEMENT OF NUMBER:


Suppose a byte is stored in R0 register, find its 2;s
complement and store the result in R3 register.
Program:
ORG 00H
MOV A, R0
CPL A
ADD A, #01H
MOV R3, A
END
Suppose a byte is stored in memory location 40H,
find its 2’s complement and store the result in
memory location 50H.
Program:
ORG 00H
MOV A, 40H
CPL A
ADD A, #01H
MOV 50H, A
END

You might also like