Saturday, April 30, 2016

8051 Program – lcm of two numbers

; LCM of two numbers
; input: 4000H
; output: 4002H
ORG 0H
DATA_ADDR EQU 4000H
MAIN:
MOV DPTR,#DATA_ADDR
MOV A,#05
MOVX @DPTR,A
INC DPTR
MOV A,#02
MOVX @DPTR,A
LCALL LCM
SJMP MAIN
LCM:
MOV DPTR,#DATA_ADDR ;load dptr with #DATA_ADDR
MOVX A,@DPTR;move data from external memory location to a
MOV R0,A ;move data from a to r0
MOV B,R0 ;move data from r0 to b
INC DPTR ;increment dptr
MOVX A,@DPTR ; move data from external memory location to a
MOV R2,A ;move data from a to r2
loop: PUSH 0E0H ;push a onto the stack
DIV AB ;divide a by b
MOV R1,B ;move data from b to r1
CJNE R1,#00,l1 ;compare r1 with 0, if not equal, jump to label l1
POP 0E0H ;pop a from satck
INC DPTR ;increment dptr
MOVX @DPTR,A ;move data from a to external memory location
SJMP return
l1: POP 0E0H ;pop a from stack
ADD A,R2 ;add a and r2
MOV B,R0 ;move data from r0 to b
SJMP loop
return:
RET
END



Related topics:
8051 Program - smallest element in an array   |   8051 Program - largest element in an array   |   8051 Program - factorial of a given number   |   8051 Program - fibonacci series   |   8051 Program - gcf of two numbers   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment