Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following programs in the assembly language of 8051. 1. Find the secon

ID: 3552322 • Letter: W

Question

Write the following programs in the assembly language of 8051.
1. Find the second largest of an array of 100 numbers stored in external memory from location 9000H onwards. The number be stored at location 9500H. Show the contents of memory locations before and after running the program to establish correctness of it
2. Compute the LCM of two numbers. Assume the numbers to be stored at location 9000H and 9001H. Store the result at 9002H.
3. Check whether a NULL-terminated string stored from location 9000H is a palindrome or not. If the string is a palindrome, store 1 at location 9500H, else clear the location.

Explanation / Answer

1. Software Code

ORG 8100H

MOV DPTR,#9000H

MOV R0,#99

MOVX A,@DPTR

MOV R1,A

INC DPTR

MOVX A,@DPTR

MOV 60H,R1

CJNE A,60H,NEXT

MOV R2,A

SJMP LOOP

NEXT: JNC SHIFT

MOV R2,A

SJMP LOOP

SHIFT: MOV R3,A

MOV A,R1

MOV R2,A

MOV A,R3

MOV R1,A

LOOP: DJNZ R0,MAIN

SJMP FINISH

MAIN: INC DPTR

MOVX A,@DPTR

MOV 60H,R1

CJNE A,60H,NEXT2

MOV R2,A

SJMP LOOP

NEXT2: JNC SHIFT

MOV 60H,R2

CJNE A,60H,NEXT3

SJMP LOOP

NEXT3: JNC MOV R2A

SJMP LOOP

MOVR2A: MOV R2,A

SJMP LOOP


FINISH: MOV DPTR,#9500H

MOV A,R2

MOVX @DPTR,A

HERE: SJMP HERE

END




2. Software Code

ORG 8100H

MOV DPTR,#9000H

MOVX A,@DPTR

MOV R1,A

INC DPTR

MOVX A,@DPTR

MOV R2,A

MOV 60H,R1

CJNE A,60H, NEXT1

SJMP LEAVE

NEXT1: JNC SWITCH

MOV A,R1

MOV B,R2

SJMP LOOP

SWITCH: MOV A,R1


MOV R3,A

MOV A,R2

MOV R1,A

MOV A,R3

MOV R2,A

MOV A,R1

MOV B,R2

LOOP: MOV R4,B

DIV AB

MOV A,B

JZ GCDOUT

MOV A,R4

SJMP LOOP

GCDOUT: MOV A,R1

MOV B,R4

DIV AB

MOV B,R2

MUL AB

LEAVE: INC DPTR

MOVX @DPTR,A

INC DPTR

MOV A,B

MOVX @DPTR,A

HERE: SJMP HERE

END



3. Software Code

ORG 8100H

MOV R2,#0H

MOV DPTR,#8FFFH ;1 less than starting value

STRING:

INC R2

INC DPTR

MOVX A,@DPTR

CJNE A,#0H,STRING

DEC DPL

MOV R0,DPL

MOV A,R2

MOV B,#2H

DIV AB

MOV R2,A

MOV DPTR,#9000H

LOOP:

MOVX A,@DPTR

MOV R4,DPL

MOV 70H,A

MOV DPL,R0

MOVX A,@DPTR

CJNE A,70H,LEAVE

DEC DPL

MOV R0,DPL

MOV DPL,R4

INC DPTR

DJNZ R2,LOOP

MOV DPTR,#9500H

MOV A,#01H

MOVX @DPTR,A

LJMP NEXT

LEAVE:

MOV DPTR,#9500H

MOV A,#0H

MOVX @DPTR,A

NEXT: SJMP NEXT

END