Sie sind auf Seite 1von 19

JUMP, LOOP & CALL INSTRUCTIONS

Mc-2

Write a program to (a) Clear ACC, then (b) add 3 to ACC 10 times.

Write a program to (a) Clear ACC, then (b) add 3 to ACC 10 times.

; This program adds value 3 to ACC 10 times


MOV A,#0 ;A=0, clear ACC MOV R2,#10 ;load counter R2=10 ADD A,#03 ; add 03 to ACC DJNZ R2, AG ; rep. Until R2=0 MOV R5, A ; save A in R5

AG:

What is the max. no. of times the loop can be repeated?

What is the max. no. of times the loop can be repeated?

Since R2 holds the count and R2 is an 8 bit register, it can hold a maximum of FFH, therefore the loop Can be repeated max of 256 times.

Write a program to (a) load ACC with 55H, and (b) Complement the ACC 700 times.

Write a program to (a) load ACC with 55H, and (b) Complement the ACC 700 times.

Since 700 is larger then 255, two registers are reqd. to Hold the count. MOV A, #55H ;A=55H MOV R3,#10 ;R3=10, outer loop MOV R2, #70 ;R2=70, inner loop CPL A ;complement A DJNZ R2, AG ;repeat it 70 times DJNZ R3, NE

NE: AG:

Write a program to determine if R5 is 0. If so put 55H in it.

NE:

MOV A, R5 ;copy R5 to A JNZ NE ;jump if A is not zero MOV R5, #55H -----------------------

Find sum of 79H, F5H, and E2H. Store sum in R0 (low byte) and R5 (high byte)

Find sum of 79H, F5H, and E2H. Store sum in R0 (low byte) and R5 (high byte)

N_1:

N_2:

OV:

MOV A, #0 MOV R5,A ADD A,#79H JNC N_1 INC R5 ADD A, #0F5H INC N_2 INC R5 ADD A,#0E2H JNC OV INC R5 MOV R0,A

;clear A ;clear R5 ;A=0+79H=79H ;if no carry add next ;if CY=1, increment R5 ;A=79+F5=6E , CY=1 ;jump if CY=0 ;if CY=1, increment R5 ;A=6E+E2=50, CY=1 ;jump if CY=0 ;if CY=1, increment R5 ;R0=50H, and R5=02H

ALL CONDITIONAL JUMPS ARE SHORT JUMPS (TWO BYTE) ADDRESS OF TARGET MUST BE WITHIN

-128 TO +127

UNCONDITIONAL SHORT JUMPS (TWO BYTE) (SJMP)

THE RELATIVE ADDRESS RANGE IS

00 TO FFH

UNCONDITIONAL LONG JUMP (THREE BYTE INST.) (LJMP)

ADDRESS RANGE
0000 TO FFFFH

8051 Conditional Jump Instructions

Instruction
JZ JNZ DJNZ CJNE A,byte CJNE reg,# data JC JNC JB JNB JBC

Action
Jump if A=0 Jump if A0 Decrement and jump if A0 Jump if A byte Jump if byte #data Jump if CY=1 Jump if CY=0 Jump if bit = 1 Jump if bit = 0 Jump if bit = 1 and clear bit

; MAIN program calling subroutines ORG 0 Main: LCALL SUBR_1 LCALL SUBR_2 LCALL SUBR_3 HERE: ; ; SUBR_1: ; ; SUBR_2: SJMP end of MAIN HERE

.... .... end of subroutine 1 .... .... RET end of subroutine 2 .... .... RET end of subroutine 3 END ;end of the asm file

8051 Assembly Main Program That Calls Subroutines

; SUB_3:

CALL

INSTRUCTION

LCALL

Long Call

ACALL

Absolute CALL

LCALL

ACALL

3 BYTE

2 BYTE

16 bit address

11 bit address

The subroutine Could be located Anywhere in 64K address space

The subroutine Could be located Anywhere in 2K address space

ORG 0 BACK: MOV A,#55H ;load A with 55H MOV P1,A ;send 55H to port 1 MOV R4,#99H MOV R5,#67H LCALL DELAY ;time delay MOV A,#0AAH ;Load A with AA MOV P1,A ;send AAH to port 1 LCALL DELAY SJMP BACK ;keep doing this ;________this is the delay subroutine ORG 300H DELAY: PUSH 4 ;PUSH R4 PUSH 5 ;PUSH R5 MOV R4,#0FFH ;R4=FFH NEXT: MOV R5,#0FFH ;R5=255 AGAIN: DJNZ R5,AGAIN DJNZ R4,NEXT POP 5 ;POP INTO R5 POP 4 ;POP INTO R4 RET ;return to caller END ;end of asm file

After Ist LCALL

After PUSH 4

After PUSH 5

0B 0A 09 00

0B 0A R4 99 00 0B

0B 0A

67

R5

99 00 0B

R4

PCH 09
PCL 08

PCH 09
PCL 08

PCH

08

0B

PCL

Das könnte Ihnen auch gefallen