13
Physics 413 Chapter 4

Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Embed Size (px)

Citation preview

Page 1: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Physics 413Chapter 4

Page 2: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

A Fork in the Road

LDAB # $ 13here : ADDA # $ 24

DEC BBNE here WAI

BNE is the new instruction. Branch if not equal to zero.

Page 3: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Branch Address Calculation

LDAB # $ 13 C6 13here : ADDA # $ 24 8B 24

DEC B 5ABNE here 26 ?? WAI 3E

Page 4: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Backward and Forward

FF Go back 1 stepFE Go back 2 stepsFB Go back 5 steps80 Go back 128 steps01 Go forward 1 step05 Go forward 5 steps7F Go forward 127 steps

Page 5: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Branch Address Calculation

LDAB # $ 13 C6 13here : ADDA # $ 24 8B 24

DEC B 5ABNE here 26 FB WAI 3E

Note: PC (Program Counter) is pointing at the next instruction so count backward from 3E. Incidentally, CPU uses the Z - bit (zero flag) in CCR to ascertain whether accumulator B is indeed zero.

Page 6: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Branches Galore

Mnemonic Description of Criteria

BCC Branch if Carry Clear

BCS Branch if Carry Set

BEQ Branch if Equal to Zero

BNE Branch if Not Equal to Zero

BRA Branch AlwaysNote: Familiarize yourself with other related instructions like JMP, BSR, and JSR

Page 7: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Dare to Compare !

here: LDAA # $ E3CMPA $ 50BNE hereWAI

Temperature sensor reading is stored at memory location 0050 and compared with a danger limit of E3 .

Page 8: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Delay Loop Subroutine

here: JSR DELAYLDAA # $ E3CMPA $ 50BNE hereWAI

.

.

.DELAY: LDX # $ FFFFAGAIN : DEXBNE AGAINRTS

Page 9: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Indexed Addressing Mode

LDX # $ 0050CLRA

up : ADDA $ 00, XINXCPX # $ 0070BNE upWAI

Program adds numbers stored in memory locations 0050 through 006F. Index register is a “pointer” which is incremented. The offset, 00, can be changed if necessary.

Page 10: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Push and Pull

PSHA A MSP SP - 1 SP

PULA SP + 1 SP MSP A

Page 11: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Predict the Outcome!

PSHAPSHBPULAPULB

Page 12: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Solution

Congratulations, if you said the contents of A and B will be swapped and , perhaps more importantly, the value of the stack pointer will be restored to its original value before this program segment was run.

00D6

00D5

00D4

00D3

Page 13: Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero

Detailed Explanation

Suppose that SP was pointing at 00D6 (stack). PUSHA stores A into 00D6. Then PUSHB stores B into 00D5. At this point SP = 00D4. Then PULA pulls 00D5 (which contains B) and stores it into A. Finally, PULB pulls 00D6 (which contains A) and stores it into B. We end up swapping A and B. At this point SP = 00D6, its original value.

00D6

00D5

00D4

00D3