Conditional Branching Branch Instructions
Conditional Branching, BCD, Subroutines • Conditional branch instructions allow the CPU to follow a course of action dependent on the results of computations • They use the ALU (arithmetic logic unit) codes (Condition Code Register bits) to determine whether to branch or not
Week 4: Branch Instructions and BCD Operations
1
Week 4: Branch Instructions and BCD Operations
3
Branch Instructions
• The CPU normally executes instructions sequentially – as they appear in memory • The next instruction to be executed is the one in the next memory location after the current one • Branch instructions allow the sequence to be varied
Branch Instruction Format
Typical machine code format:
8bits 8bits
opcode
relative address
Each branch instruciton has a different opcode
Relative to the location of next instruction
Week 4: Branch Instructions and BCD Operations
2
Week 4: Branch Instructions and BCD Operations
4
Relative Address
• It is a 2’s complement signed integer (–128 ... +127) • Specifies the difference between the address of the next instruction if you do follow the branch, and that if you don’t • Using relative address rather than absolute means you’re not dependent on where program is loaded in memory
Typical Set of Branch Instructions
Mnemonic Condition Explanation BRA Branch always (unconditional) BEQ == Branch if equal BNE != Branch if not equal BGT > Branch if greater than BLT < Branch if less than BGE >= Branch if greater or equal BLE <= Branch if less than or equal
Week 4: Branch Instructions and BCD Operations
5
Week 4: Branch Instructions and BCD Operations
7
Relative Address Example
$FE (in 2's complement) = 2
Address (hex) brop C201 $22 C202 brop C203 $FE
C200 C224 C230
Typical Set of Branch Instructions
Jumps to $C202 + $22
mnemonic BHI BLO BHS BLS BCS BCC BVS BVC BPL BMI
6
condition unsigned > unsigned < unsigned >= unsigned <=
in words Branch if higher Branch if lower Branch if higher or same Branch if lower or same Branch if carry set Branch if carry clear Branch if overflow set Branch if overflow clear Branch if ‘plus’ Branch if ‘minus’
8
op
jumps to $C204 –2 (i.e. $C202)
$F2 (in 2's complement) = 14
brop C231 $F2
Week 4: Branch Instructions and BCD Operations
jumps to $C232 – 14 (i.e. $C224)
>=0 <0
Week 4: Branch Instructions and BCD Operations
Branch Code Interpretation
• Most branch codes are interpreted in the context of a subtraction
integer a integer b
Signed vs Unsigned Arithmetic
Let a = 11110000 b = 00001111
in 2s complement form a=16 If it's taken as unsigned, a=240

CCR flags are updated
CCR flags are updated considering a and b both signed and unsigned
Appropriate flags are checked to execute BGT properly
Week 4: Branch Instructions and BCD Operations 9
Use BHS, BLO if you assume a and b as unsigned numbers Use BGE, BLT if you assume a and b as signed numbers
11
Week 4: Branch Instructions and BCD Operations
Unsigned?
• If you use BHS, BLO etc. then the integers will be treated as unsigned • If you use BGE, BLT etc. then the integers will be treated as signed • The mathematical operation preceding the branch statement will have the same result whether you’re dealing with signed or unsigned integers. • It’s the branch statement you use that determines how they’re interpreted
Signed vs Unsigned Arithmetic Example 1
a = 11110000 b = 00001111 (16/240) (15) a–b = 11100001 (31/ 225)
11100001 is either –31 or 225 depending on how you interpret the source values
Z N C V 0 1 0 0 BGT will not branch since N=1 BHI will branch since C=0
12
Week 4: Branch Instructions and BCD Operations
10
Week 4: Branch Instructions and BCD Operations
Signed vs Unsigned Arithmetic Example 2
a = 11110000 b = 11110001 (16/240) (15/241) a–b = 11111111 (1/ 255)
Branch Conditions
• • • • • • BCS branches if BVC branches if BPL branches if BEQ branches if BLT branches if BHI branches if C=1 V=0 N = 0 (even if Z=1) Z=1 N=1 C = 0 AND Z = 0
if you assume a and b as signed numbers, you use BGE or BLT, otherwise BHS or BLO
Z N C V 0 1 1 0 BGT will not branch since N=1
BHI will not branch since C=1
13
Week 4: Branch Instructions and BCD Operations
Week 4: Branch Instructions and BCD Operations
15
Example
unsigned LDAA #%11110000 CMPA #%00001111 BHS SKIP INCA SKIP .... .... signed LDAA #%11110000 CMPA #%00001111 BGE SKIP INCA SKIP .... ....
Example
• You have a list of unsigned numbers starting at address $D101. • Address $D100 contains the length of the list. • Write a program segment (starting at address $C000) that returns how many numbers that are greater than or equal to 100 in Accumulator A.
240 >= 15, branches to address SKIP
16 < 15, it executes INCA
Week 4: Branch Instructions and BCD Operations
14
Week 4: Branch Instructions and BCD Operations
16
Example Solution
ORG SUBR LOOP LDAB CLR LDX LDAA CMPA BLO INC INX DECB BNE LDAA SWI RMB $C000 $D100 CTR #$D101 0,X #100 SKIP CTR LOOP CTR 1 sets starting address load length clear temp variable list head load from list compare less than? (unsigned) if >=, increment temp var. next element in the list decrement list counter if not EOL, goto beginning Accu A returns the counter return to monitor temporary variable
Week 4: Branch Instructions and BCD Operations 17
Fixing Limitations
• What if the destination is too far?
– Outside –128 .. +127 – e.g. BEQ TOOFAR
Reverse of BEQ must be used in the reverse logic
• Reverse the logic as follows:
SKIP
CTR
L1
BNE L1 JMP TOOFAR <<< continue program >>>
Week 4: Branch Instructions and BCD Operations 19
Solution (if numbers are signed)
ORG BEGIN LDAB CLR LDX LOOP LDAA CMPA BLT INC SKIP INX DECB BNE LDAA SWI CTR RMB $C000 $D100 CTR #$D101 0,X #100 SKIP CTR LOOP CTR 1 sets starting address load length clear temp variable list head load from list compare less than? (signed) if >=, increment temp var. next element in the list decrement list counter if not EOL, goto beginning Accu A returns the counter return to monitor temporary variable
Example of Use: If
signed!!! LDAA N1 CMPA N2 BLE LBL1 <<statement1>> LBL1
if (a > b) statement1;
Week 4: Branch Instructions and BCD Operations
18
Week 4: Branch Instructions and BCD Operations
20
Example of Use: Ifthenelse
signed!!! LDAA N1 CMPA N2 BLE LBL1 <<statement1>> BRA LBL2 LBL1 <<<statement2>> LBL2 do {
dowhile (test on exit)
DOWH <<statement>> LDAA A CMPA B BLE QUIT BRA DOWH QUIT
if (a > b) statement1; else statement2;
statement; } while (A>B);
Week 4: Branch Instructions and BCD Operations
21
Week 4: Branch Instructions and BCD Operations
23
while (test on entry)
while (A>B) { statement; } WHLE LDAA A CMPA B BLE QUIT <<statement>> BRA WHLE QUIT
Some Coding Hints
• LDAA and STAA WILL NOT affect carry • Most arithmetic operations WILL change carry • Logic operations typically WILL NOT change carry • If have two arithmetic processes in program, desired carry information can be DESTROYED if care not taken • There are many ways to implement a program to do something • You should plan out your coding before writing code • Use EQU to define constants
22 Week 4: Branch Instructions and BCD Operations 24
Week 4: Branch Instructions and BCD Operations
BitManipulation Branch Instructions
• The 68HC11 MPU has two conditional branch instructions that are much different than the twobyte branch instructions we have been using up to now. These special conditional branch instructions are: Mnemonic Condition for branching
– BRCLR Specific bits of operand are clear (0) – BRSET Specific bits of operand are set (1)
Week 4: Branch Instructions and BCD Operations 25
BRCLR
BRCLR
Instruction
0,X
$F0
$F2
JUMP Address Mask Data address
Week 4: Branch Instructions and BCD Operations
27
BRSET and BRCLR
• [<label>] BRCLR (opr) (msk) (rel) • [<label>] BRSET (opr) (msk) (rel) • where
– opr specifies the memory location to be checked and must be specified using either the direct or index addressing mode. – msk is an 8bit mask that specifies the bits of the memory location to be checked. The bits of the memory byte to be checked correspond to those bit positions that are 1s in the mask. – rel is the branch offset and is specified in the relative mode.
Week 4: Branch Instructions and BCD Operations 26
BRSET
• The BRSET instruction operates in the same way as BRCLR except it complements the operand byte before ANDing with the mask byte. • In this way, branching will occur only if there are 1s in the original operand in the same bit positions as the 1s in the mask byte.
Week 4: Branch Instructions and BCD Operations 28
Example
BRCLR 0,X $F0 $F2
• In executing this instruction, the CPU will fetch the operand byte from the operand address (in this case, 0,X). • Then it will AND that operand with the mask byte ($F0). • If the resulting byte is all zeros, the MPU will branch to address PC14 for its next instruction;
Week 4: Branch Instructions and BCD Operations 29
Example
• Write a program to compute the sum of the odd numbers in an array with 20 8bit elements. • The array is stored at $D000$D013. Save the sum at $D020$D021.
Week 4: Branch Instructions and BCD Operations
31
Example
• For example, the sequence
ldx brclr ldaa #$1000 $30,X %10000000 $31,X
Solution
N equ org sum rmb org clra staa staa ldx loop brclr ldd addb adca std chkend cpx bhs inx bra end $D013 $D020 2 $C000 ; number elements in the array ; result
here
here
• will force the 68HC11 continue to execute the second instruction until the bit 7 is set to 1.
exit
Week 4: Branch Instructions and BCD Operations 30
sum ; initialize sum to 0 sum+1 ; " #$D000 ; point X to array[0] 0,X $01 chkend ; is it an odd number? sum ; add the odd number to the sum 0,X ; " #0 ; " sum ; " #N ; compare the pointer to the address ; of the last element exit ; is this the end? loop ; not yet done, continue
Week 4: Branch Instructions and BCD Operations 32
Example
• Write a program to count the number of 1s in the 16bit number stored at $D000$D001. • Save the result in $D002. • Solution :
– The 16bit number is shifted to the right up to 16 time or until the shifted value becomes 0. – If the bit shifted out is a 1 then increment the 1s count by 1.
Binary Coded Decimal Operations
BCD addition, subtraction, adjustment after operations
Week 4: Branch Instructions and BCD Operations
33
Week 4: Branch Instructions and BCD Operations
35
Solution
org ldaa staa ldd lsrd bcc inc cpd bne swi $C000 #$00 $D002 $D000 tst0 $D002 #0 loop ; ; ; ; ; ; ; initialize the 1s count to 0 " place the number in D shift the lsb of D to the C flag is the C flag a 0? increment 1s count if the lsb is a 1 check to see if D is already 0
BCD Numbers and Additon
• each digit is encoded by 4 bits
– two digits are packed into one byte – the addition of two BCD numbers is performed by binary addition and an adjust operation using the DAA instruction
loop tst0
• the instruction DAA can be applied after the instructions ADDA, ADCA, and ABA
– simplifies I/O conversion
Week 4: Branch Instructions and BCD Operations 34 Week 4: Branch Instructions and BCD Operations 36
BCD Example
• For example, the instruction sequence
LDAA $D000 ADDA $D001 DAA STAA $D002
Decimal Adjust and Example
• 68HC11 has special instruction
– Decimal Accumulator Adjust A: DAA
AccA AccB $44 $23 $67 = = 0 1 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0 0 1 1 0 1 1 1 Both BCD values between 0 and 9 H=0, C=0 Dec. adjust =0
37 Week 4: Branch Instructions and BCD Operations 39
• adds the BCD numbers stored at $D000 and $D001 and saves the sum in the memory location at $D002.
Week 4: Branch Instructions and BCD Operations
Status of Flag bits
Temporary Bin. result
Upper 4 bits lower 4 bits
Decimal Adjust Add to upper/lower Including carries.
DAA: Example 2
correct value in this case, but not proper BCD
AccA AccB $52 $54 $1F $25 = = = + = 0 1 0 1 0 1 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 1 1 1 1 0 1 1 0 0 1 0 1 (the result of add) (correction: $06) (BCD number) lower BCD A…F H=0, C=0 Decimal adjust =$06
C 0 0 0 0 0 0 1 1 1
H 0 0 0 0 1 1 0 0 1
0–9 0–8 A–F 9–F 0–9 A–F 0–2 0–2 0–3
0–9 A–F 0–9 A–F 0–3 0–3 0–9 A–F 0–3
0 0 6 6 0 6 6 6 6
0 6 0 6 6 6 0 6 6
Week 4: Branch Instructions and BCD Operations
40
DAA: Example 3
correct value in this case, but not proper BCD
AccA AccB $18 $07 $1F C=1 $06 = = = + = 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 1 0 (the result of add) (correction: $60) (BCD number) higher BCD A … F H=0, C=0 Decimal adjust =$60
LDD LDX IDIV STAB XGDX LDX IDIV STAB XGDX LDX IDIV STAB XGDX LDX IDIV STAB XGDX STAB END $D000 #10 $D006 #10 $D005 #10 $D004 #10 $D003 $D002
Solution
; place the 16bit number in D ; compute the least significant digit ; save the least significant BCD digit ; place the quotient in D ; compute the second least significant BCD digit ; save the second least significant BCD digit ; place the quotient in D ; compute the middle BCD digit ; save the middle BCD digit
; compute the second most significant digit ; the second most significant BCD digit ; save the most significant BCD digit
Week 4: Branch Instructions and BCD Operations
41
Week 4: Branch Instructions and BCD Operations
43
Example
• Write a program to convert the 16bit number stored at $D000$D001 to BCD • format and store the result at $D002$D006. Each BCD digit is stored in one byte. • Solution:
– A binary number can be converted to BCD format by using repeated division by 10. – The largest 16bit binary number is 65535 which has five decimal digits. – The first division by 10 obtains the least significant digit, the second division by 10 obtains the second least significant digit, and so on.
Week 4: Branch Instructions and BCD Operations 42
Subroutines
Functions, passing arguments to subroutines
Week 4: Branch Instructions and BCD Operations
44
Subroutines
• A subroutine is a sequence of instructions that perform a specific task. It is written once and stored in a specific area of memory • Whenever a main program needs to perform that task, the µP simply jumps to the appropriate address for the subroutine, execute it, and then return • This allows reusability of the code. The subroutine can be called many times.
.... .... JSR SUBR LDAA #$01 .... .... .... PSHX LDX $D400 .... RTS .... .... .... ....
Subroutines
; push PC, jump to address SUBR ; this instr. will be executed after RTS
SUBR
; pull the return address from stack ; jump to that location
Week 4: Branch Instructions and BCD Operations
45
Week 4: Branch Instructions and BCD Operations
47
Subroutines
• JSR and RTS Instructions are used to direct the µP to jump to or return from a subroutine respectively • The Jump to SubRoutine, JSR, requires a subroutine address as an operand and will cause the µP to push the current PC onto the stack (the return address) and then initiate a jump to the address specified • The ReTurn from Subroutine instruction, RTS, pulls the return address from the top of the stack, places it back into the PC and continues execution of the main program
Week 4: Branch Instructions and BCD Operations 46
Subroutines
• The Branch to SubRoutine, BSR, instruction is the same as the JSR instruction except that the relative address mode is used (for subroutines within the offset range 128 to +127) • Use BSR, if the memory address of the subroutine is very close to where BSR is located. Otherwise, assembler throws an error. • BSR is more compact that JSR. BSR takes only two bytes, while JSR occupies 3 bytes.
Week 4: Branch Instructions and BCD Operations
48
Passing Arguments to Functions
We need to pass some values or addresses to subroutines
SUBR .... .... .... RTS
Passing Arguments in Registers
How? • Caller puts argument values in specific registers (as defined by the function) before making the call. • Caller needs to know which register to use for which argument
Also, some results must be returned from subroutines Can we use Registers, memory locations to pass values?
Week 4: Branch Instructions and BCD Operations 49
What about STACK to pass values?
Week 4: Branch Instructions and BCD Operations
51
Three Methods
1. In registers 2. In a common data area 3. On the stack
• • by value by reference
Passing Arguments in Registers
.... LDAA LDX LDY JSR .... .... .... COPYM LDAB STAB INX INY DECA BNE RTS
50
#$34 #$D200 #$D380 COPYM
; number of bytes to copy ; starting memory location ; destination address ; push PC, jump to address COPYM
0,X 0,Y
Each has its advantages and disadvantages
Week 4: Branch Instructions and BCD Operations
COPYM
; load from source ; store to dest ; next location ; next location ; decrement counter ; if not zero, goto the starting point ; we are done, get back to where we left
COPYM uses A, X and Y registers to operate
Week 4: Branch Instructions and BCD Operations
52
Passing Arguments in Registers
Advantages
– Very Fast (doesn’t need a memory cycle)
Passing Arguments on the Stack
LDAA PSHA LDY PSHY JSR PULX PULA .... .... ZEROM TSY LDAA LDX LOOP CLR INX DECA BNE RTS
53
#$12 #$D200 ZEROM
Disadvantages
– Limited by number of registers – A register being used to hold an argument is not available for other uses – Not recursive
; number of bytes to clear ; push it to stack ; starting memory location ; push it to stack ; push PC, jump to address ZEROM ; restore stack (we reclaim stack) ; restore stack (we reclaim stack) ; SP+1 à IY ; load counter (number of bytes) ; load beginning address ; clear location ; next location ; decrement counter ; if not zero, goto the beginning of loop ; we are done, get back to where we left
Week 4: Branch Instructions and BCD Operations 55
4,Y 2,Y 0,X
LOOP
Week 4: Branch Instructions and BCD Operations
Passing Arguments on the Stack
How? • The caller pushes the arguments onto the stack before making the call, and is usually (but not always) responsible for removing them afterwards • The called function can access the arguments using indexed addressing mode (e.g. load SP into X  TSX  then refer to args as 2,X … 3,X etc)
Passing Arguments on the Stack
Advantages
– Supports recursive function calls – Number of arguments limited only by size of stack (and maximum level of recursion, nesting) – Memory is reclaimed on return
Disadvantages
– Slower than passing by register (accesses memory)
54 Week 4: Branch Instructions and BCD Operations 56
Week 4: Branch Instructions and BCD Operations
Passing arguments in Common Memory
How? • The caller stores the argument values in known (advertised) locations before calling the function
Example: delayloop function
• This is a simple function written in 68HC11 assembly code. It creates a delay of between 1 and 256 milliseconds, by executing a loop that takes the specified number of milliseconds to complete. • The number of milliseconds is passed in accumulator A. • If A = 0 then the delay is 256, else the delay is the (unsigned, obviously) value A.
Week 4: Branch Instructions and BCD Operations
57
Week 4: Branch Instructions and BCD Operations
59
Passing arguments in Common Memory (2)
Advantages • Unlimited number of arguments • No need to clean up the stack afterwards Disadvantages • Not recursive • Potentially wasteful of memory (the common area is permanently reserved) • About as slow as the stack
Week 4: Branch Instructions and BCD Operations 58
The delayloop function
DELAY L1 LDY #570 DEY BNE L1 DECA BNE DELAY RTS ; load 570 into IY ; decrement IY ; decrement A
Week 4: Branch Instructions and BCD Operations
60
Points to note…
1. There is no special syntax for defining a function 2. The label “DELAY” marks the beginning of the function; a call to it could be written JSR DELAY 3. The programmer needs to know that this function expects its argument in accumulator A in order to write the call properly 4. The function uses the IY register as a counter. Anything previously in IY will be lost! You must be aware of this sideeffect.
Week 4: Branch Instructions and BCD Operations 61
More points…
7. The programmer decided that 570 was the number of iterations of the inner loop required to cause a 1 ms delay. The actual delay will therefore depend on how fast the CPU executes the instructions
Week 4: Branch Instructions and BCD Operations
63
More points to note
5. The function also modifies its argument (accumulator A), but this is generally not considered anything to worry about 6. The control flow of the function is two nested dowhile loops. HLL equivalent:
do { IY = 570; do {IY;} while (IY != 0); A;} while (A != 0);
Sideeffects
• We used IY as a counter, therefore lost any previous contents. • We might often want to do this sort of thing in a function. • What can we do about it?
Week 4: Branch Instructions and BCD Operations
62
Week 4: Branch Instructions and BCD Operations
64
Curing the SideEffects
DELAY L1 L2 PSHY ; save IY on stack LDY #570 DEY BNE L2 DECA BNE L1 PULY ; restore Y RTS
Programming exercises
Q1. Write the assemblycode instructions to call the delayloop function with an argument of 25 Q2. How would you modify the delayloop function to increase the length of the delays it produces by a factor of 10? Q3. How would you modify the delayloop function so that the argument value 0 produces a 0millisecond delay instead of a 256millisecond delay?
65 Week 4: Branch Instructions and BCD Operations 67
Week 4: Branch Instructions and BCD Operations
Curing the sideeffects
• The idea is to save the current value of a register by pushing it onto the stack • At the end of the function, immediately before the RTS, it is restored • Special commands for doing this: PSHY and PULY • (also PSHX, PSHA, PSHB, PULX, PULA, PULB) • Most instruction sets have push and pop (pull) instructions for this purpose
Week 4: Branch Instructions and BCD Operations 66
Programming exercises
Q4. Modify the delayloop function to produce delays in the range 0 to 200 milliseconds, where the number of milliseconds is either the unsigned value passed in accumulator A, or 200, whichever is smaller.
Week 4: Branch Instructions and BCD Operations
68
Passing by Reference Subroutine Return Values
How can we return values or addresses from subroutines to the calling program? • This means that you pass the address of an argument to a function instead of its value • For large objects (e.g. an array) it is much quicker than passing the whole object • Arguments passed by reference can be written to by the function, and then accessed by the caller; they therefore serve as input and output arguments.
69 Week 4: Branch Instructions and BCD Operations 71
Week 4: Branch Instructions and BCD Operations
Function Return Values
• All three mechanisms for passing arguments to functions can also be used to pass a return value back to the caller • It is therefore possible to have functions returning more than one value. However, programming conventions do not normally allow it • Use passing by reference if you want to do this
Week 4: Branch Instructions and BCD Operations 70
HLL Passing by Reference
In an HLL (High Level Language), you can define functions to return large objects. Most compilers will change your code to this representation.
Original HLL: bigobj = bigReturn(arg); This becomes: bigReturn(&bigobj, arg);
hidden extra argument
72
Week 4: Branch Instructions and BCD Operations