190 likes | 431 Views
Microcontroller Fundamentals & Programming. Branch & Jump Instructions. Branch Instructions. Branch Instructions: allowed execution of a program to continue at another memory location by modifying the program counter value. 2 types of branch instructions :
E N D
Microcontroller Fundamentals & Programming Branch & Jump Instructions
Branch Instructions • Branch Instructions: • allowed execution of a program to continue at another memory location by modifying the program counter value. • 2 types of branch instructions : • Unconditional branch and • Conditional branch.
Unconditional Branch Instruction BRA (branch always). • There is only one Unconditional Branch instruction. • Flag bits in CCR will not be tested for a branch to take place. • The branch “distance” uses relative addressing. It ranges +12710 to -12810. • Sometimes the branch “distance” also known as “offset” .
Example: BRAnch Instruction MnemonicsExplanations BACKWARD: BRA FORWARD ; Branch to location LDAA #$10 ; FORWARD. - - - - ; FORWARD: LDAB #$39 ; BRA BACKWARD ; Branch to location ; BACKWARD
Conditional Branch Instructions • The Conditional Branch instructions allow CPU to make decision. • Flag bits in CCR are tested to decide whether a branch to take place. • The branch “distance” uses relative addressing. • It ranges from +12710 to -12810 .
START INIT IX; AccB CLR Memory BNE (No) AccB = 0? END Branch Not Equal (BNE) Instruction Example: To clear a block of memory from location ($00) to ($10) START LDX #$0010 ;Initialise LDAB #$11 ; IX , AccB CLRA REPEAT STAA $00,X ; Clr Mem DEX DECB BNE REPEAT WAI • IX is initialised as a POINTER and AccB is initialised as a COUNTER . • Memory location ($00,X) down to ($00) will be cleared.
Branch Carry Clear & Set (BCC & BCS) Instructions: • BCC => branch if Carry bit clear (C = “0”) • BCS => branch if Carry bit set (C = “1”) (Carry bit in CCR will be tested to determine whether a branch will to take effect).
Branch Carry Clear & Set (BCC & BCS) Example: BCC instruction START LDAA $0100 ; copy the content of memory STAA $50 ; $0100 into memory $50 - - - - - - - - - - ADDA #$20 BCC START ; after executing ADDA, ; if C = “0” branch to START STAA $80 ; else executes STAA $80. - - - - - - - - - -
Branch Equal & Not Equal (BEQ & BNE) Instructions: • BEQ branch if Zero bit set (Z = “1”) • BNE branch if Zero bit clear (Z = “0”) (Zero bit in CCR will be tested to determine whether a branch will to take effect). • BEQ = Branch if equal to zero • BNE = Branch if not equal to zero
Branch Equal & Not Equal (BEQ & BNE) Example: To clear memory location ($50 to $60) using BNEinstruction. LDX #$0010 ; initialise X as counter. LDAA #$00 ; ACCA = $00 REPEAT STAA $50,X ;clear memory $50 to $60 DEX BNE REPEAT ; if register X is not zero - - - - - ; then branch to REPEAT. BEGIN LDAA $80 - - - - -
Branch Minus & Plus (BMI & BPL) Instructions: • BMI branch if Negative bit set (N = “1”) • BPL branch if Negative bit clear (N = “0”) (Negative bit in the CCR will be tested to determine whether a branch will to take effect). • BMI = Branch if (result) minus • BPL = Branch if (result) plus
Example: Branch Plus (BPL) Add contents in memory ($0150) and ($0151). Store +ve result in memory ($0180) using BPL instruction. LDAA $0150 ; place contents ($0150) to ACCA ADDA $0151 ;ACCA = contents ($0150)+($0151) BPL SUM ; branch to SUM if ACCA bit-7 = “0”. LDAB #$10 - - - - - - SUM STAA $0180 (An 8-bit signed number is said to be positive if the most significant bit, MSB, is a “0” and negative if MSB is a “1”).
Jump Instruction • Only one jump instruction :JMP • Flag bits in CCR will not be tested for a jump to take place. • The difference between using a jump and a branch: • Jump can go to any 64K of memory location and start executing from there; whereas • Branch can go relative +12710 to -12810 of memory location.
Jump Instruction Example: Jump instruction