0% found this document useful (0 votes)
12 views

Lecture Topic 1.3.4

microcontroller
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture Topic 1.3.4

microcontroller
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Assembly Language Programs

2. Write a program to store data FFH into RAM memory locations 50H to 58H using direct
addressing mode.
ORG 0000H ; Set program counter 0000H
MOV A, #0FFH ; Load FFH into A
MOV 50H, A ; Store contents of A in location 50H
MOV 51H, A ; Store contents of A in location 5IH
MOV 52H, A ; Store contents of A in location 52H
MOV 53H, A ; Store contents of A in location 53H
MOV 54H, A ; Store contents of A in location 54H
MOV 55H, A ; Store contents of A in location 55H
MOV 56H, A ; Store contents of A in location 56H
MOV 57H, A ; Store contents of A in location 57H
MOV 58H, A ; Store contents of A in location 58H
END
3. Write a program to subtract a 16 bit number stored at locations 51H-52H from 55H-56H and
store the result in locations 40H and 41H. Assume that the least significant byte of data or the
result is stored in low address. If the result is positive, then store 00H, else store 01H in 42H.

ORG 0000H ; Set program counter 0000H


MOV A, 55H ; Load the contents of memory location 55 into A
CLR C ; Clear the borrow flag
SUBB A,51H ; Sub the contents of memory 51H from contents of A
MOV 40H, A ; Save the LS Byte of the result in location 40H
MOV A, 56H ; Load the contents of memory location 56H into A
SUBB A, 52H ; Subtract the content of memory 52H from the content A
MOV 41H,A ; Save the MSbyte of the result in location 41H.
MOV A, #00 ; Load 00 into A
ADDC A, #00 ; Add the immediate data and the carry flag to A
MOV 42H, A ; If result is positive, store00H, else store 0lH in 42H
END
4. Write a program to add two 16 bit numbers stored at locations 51H-52H and 55H- 56H and store
the result in locations 40H, 41H and 42H. Assume that the least significant byte of data and
the result is stored in low address and the most significant byte of data or the result is stored in
high address.
ORG 0000H ; Set program counter 0000H
MOV A,51H ; Load the contents of memory location 51H into A
ADD A,55H ; Add the contents of 55H with contents of A
MOV 40H,A ; Save the LS byte of the result in location 40H
MOV A,52H ; Load the contents of 52H into A
ADDC A,56H ; Add the contents of 56H and CY flag with A
MOV 41H,A ; Save the second byte of the result in 41H
MOV A,#00 ; Load 00H into A
ADDC A,#00 ; Add the immediate data 00H and CY to A
MOV 42H,A ; Save the MS byte of the result in location 42H
END
5. Write a program to store data FFH into RAM memory locations 50H to 58H using
indirect addressing mode.

ORG 0000H ; Set program counter 0000H


MOV A, #0FFH ; Load FFH into A
MOV RO, #50H ; Load pointer, R0-50H
MOV R5, #08H ; Load counter, R5-08H
Start: MOV @RO, A ; Copy contents of A to RAM pointed by R0
INC R0 ; Increment pointer
DJNZ R5, start ; Repeat until R5 is zero
END
6. Write a program to add two Binary Coded Decimal (BCD) numbers stored at locations 60H and
61H and store the result in BCD at memory locations 52H and 53H. Assume that the least significant
byte of the result is stored in low address.

ORG 0000H ; Set program counter 00004


MOV A,60H ; Load the contents of memory location 60H into A
ADD A,61H ; Add the contents of memory location 61H with contents of A
DA A ; Decimal adjustment of the sum in A
MOV 52H, A ; Save the least significant byte of the result in location 52H
MOV A,#00 ; Load 00H into .A
ADDC A, #00H ; Add the immediate data and the contents of carry flag to A
MOV 53H,A ; Save the most significant byte of the result in location 53H
END
7. Write a program to clear 10 RAM locations starting at RAM address 1000H.

ORG 0000H ; Set program counter 0000H


MOV DPTR, #1000H ; Copy address 1000H to DPTR
CLR A ; Clear A
MOV R6, #0AH ; Load 0AH to R6
again: MOVX @DPTR, A ; Clear RAM location pointed by DPTR

INC DPTR ; Increment DPTR


DJNZ R6, again ; Loop until counter R6=0
END
8. Write a program to compute 1 + 2 + 3 + N (say N=15) and save the sum at 70H.

ORG 0000H ; Set program counter 0000H


N EQU 15
MOV R0, #00 ; Clear R0
CLR A ; Clear A
again: INC R0 ; Increment R0
ADD A, R0 ; Add the contents of R0 with A
CJNE R0, #N, again ; Loop until counter, R0, N
MOV 70H, A ; Save the result in location 70H
END
9. Write a program to multiply two 8 bit numbers stored at locations 70H and 71H and store the
result at memory locations 52H and 53H. Assume that the least significant byte of the result is
stored in low address.

ORG 0000H ; Set program counter 0000H


MOV A, 70H ; Load the contents of memory location 70h into A
MOV B, 71H ; Load the contents of memory location 71H into B
MUL AB ; Perform multiplication
MOV 52H, A ; Save the least significant byte of the result in location 52H
MOV 53H, B ; Save the most significant byte of the result in location 53
END
10.Ten 8 bit numbers are stored in internal data memory from location 5oH. Write a program to
increment the data.
Assume that ten 8 bit numbers are stored in internal data memory from location 50H, hence R0 or R1
must be used as a pointer. The program is as follows.

OPT 0000H
MOV R0, #50H
MOV R3, #0AH
Loopl: INC @R0
INC R0
DJNZ R3, loopl END
END
11.Write a program to find the average of five 8 bit numbers. Store the result in H. (Assume that after
adding five 8 bit numbers, the result is 8 bit only).

ORG 0000H DJNZ R5, Loop

MOV 40H, #05H DIV AB

MOV 41H, #55H MOV 55H, A


MOV 42H, #06H END
MOV 43H, #1AH
MOV 44H, #09H
MOV R0, #40H
MOV R5, #05H
MOV B, R5
CLR A
Loop: ADD A, @R0
INC R0
12. Write a program to find the cube of an 8 bit number.

ORG 0000H ADD A, 51H


MOV R1, #N MOV 51H, A
MOV A, R1 MOV 52H, B
MOV B, R1 MOV A, #00H
MUL AB //SQUARE IS COMPUTED ADDC A, 52H
MOV 52H, A //CUBE IS STORED IN 52H,51H,50H
MOV R2, B
END
MOV B, R1
MUL AB
MOV 50, A
MOV 51, B
MOV A, R2
MOV B, R1
MUL AB
13. Write a program to exchange the lower nibble of data present in external memory 6000H and 6001H.

ORG 0000H ; Set program counter 00h


MOV DPTR, #6000H ; Copy address 6000H to DPTR
MOVX A, @DPTR ; Copy contents of 6000H to A
MOV R0, #45H ; Load pointer, R0=45H
MOV @R0, A ; Copy content of A to RAM pointed by 80
INC DPL ; Increment pointer
MOVX A, @DPTR ; Copy contents of 6001H to A
XCHD A, @R0 ; Exchange lower nibble of A with RAM pointed by R0
MOVX @DPTR, A ; Copy contents of A to 6001H
DEC DPL ; Decrement pointer
MOV A, @R0 ; Copy content of RAM pointed by R0 to A
MOVX @DPTR, A ; Copy content of A to RAM pointed by DPTR
END
14. Write a program to count the number of and o's of 8 bit data stored in location 6000H.

ORG 00008 ; Set program counter 00008


MOV DPTR, #6000h ; Copy address 6000H to DPTR
MOVX A, @DPTR ; Copy number to A
MOV R0, #08 ; Copy 08 in R0
MOV R2, #00 ; Copy 00 in R2
MOV R3, #00 ; Copy 00 in R3
CLR C ; Clear carry flag
BACK: RLC A ; Rotate A through carry flag

JC NEXT ; If CF=1, branch to next


INC R2 ; If CF=0, increment R2
AJMP NEXT2
NEXT: INC R3 ; If CF=1, increment R3
NEXT2: DJNZ R0, BACK ; Repeat until R0 is zero
END
15. Write a program to shift a 24-bit number stored at 57H-55H to the left logically four places. Assume
that the least significant byte of data is stored in lower address.

ORG 0000H ; Set program counter 0000h


MOV R1, #04 ; Set up loop count to 4
again: MOV A,55H ; Place the least significant byte of data in A
CLR C ; Clear the carry flag
RLC A ; Rotate contents of A (55h) left through carry
MOV 55H, A
MOV A, 56H
RLC A ; Rotate contents of A (56H) left through carry
MOV 56H, A
MOV A, 57H
RLC A ; Rotate contents of A (57H) left through carry
MOV 57H, A
DJNZ R1, again ; Repeat until R1 is zero
END
16.Two 8 bit numbers are stored in location 1000h and 1001h of external data memory. Write a
program to find the GCD of the numbers and store the result in 2000h.

ALGORITHM
● Step 1: Initialize external data memory with data and DPTR with address
● Step 2: Load A and TEMP with the operands
● Step 3: Are the two operands equal? If yes, go to step 9
● Step 4: Is (A) greater than (TEMP)? If yes, go to step 6
● Step 5: Exchange (A) with (TEMP) such that A contains the bigger number
● Step 6: Perform division operation (contents of A with contents of TEMP)
● Step 7: If the remainder is zero, go to step 9
● Step 8: Move the remainder into A and go to step 4
● Step 9: Save the contents of TEMP in memory and terminate the program
ORG 0000H ; Set program counter 0000H
TEMP EQU 70H
TEMPI EQU 71H
MOV DPTR, #1000H ; Copy address 1000H to DPTR
MOVX A, @DPTR ; Copy First number to A
MOV TEMP, A ; Copy First number to temp
INC DPTR
MOVX A, @DPTR ; Copy Second number to A
LOOPS: CJNE A, TEMP, LOOP1 ; (A) /= (TEMP) branch to LOOP1
AJMP LOOP2 ; (A) = (TEMP) branch to LOOP2
LOOP1: JNC LOOP3 ; (A) > (TEMP) branch to LOOP3
MOV TEMPI, A ; (A) < (TEMP) exchange (A) with (TEMP)
MOV A, TEMP
MOV TEMP, TEMPI
LOOP3: MOV B, TEMP
DIV AB ; Divide (A) by (TEMP)
MOV A, B ; Move remainder to A
CJNE A, #00, LOOPS ; (A)/=00 branch to LOOPS
LOOP2: MOV A, TEMP
MOV DPTR, #2000H
MOVX @DPTR, A ; Store the result in 2000H
END
A.TEXTBOOKS/REFERENCE BOOKS

i. TEXTBOOKS
T1 Kenneth Ayala, The 8051 Microcontroller, Cengage Learning India, 3rd Edition, Nov 2007
T2 B. Kanta Rao, Embedded Systems, PHI, 1st Edition, January 2011
T3 John Boxall, Arduino Workshop: A Hands-On Introduction with 65 Projects, No Starch Press, 1st
Edition, May 2013

ii. REFERENCE BOOKS


R1 B. Ram, Fundamentals of Microprocessors and Microcontrollers, DRP, 8th Edition, Nov 2021
R2 Massimo Banzi, Getting Started with Arduino, Make Community LLC, 3rd Edition, Jan 2015
R3 Jeremy Blum, Exploring Arduino: Tools and Techniques for Engineering Wizardry, Wiley, 1st Edition,
Aug 2013

B. Video Links:

1.https://www.youtube.com/watch?v=g7Gypl9zNz8
2.https://www.youtube.com/watch?v=nIZdkdjuLMc
3.https://www.youtube.com/watch?v=Vrazx7AsutM
4.https://www.youtube.com/watch?v=dsBR1rP2kmY

You might also like