Documente Academic
Documente Profesional
Documente Cultură
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
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
op-code
relative address
Relative Address
It is a 2s 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 dont Using relative address rather than absolute means youre not dependent on where program is loaded in memory
mnemonic BHI BLO BHS BLS BCS BCC BVS BVC BPL BMI
6
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
>=0 <0
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
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 youre dealing with signed or unsigned integers. Its the branch statement you use that determines how theyre interpreted
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
10
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
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.
14
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
SKIP
CTR
L1
Example of Use: If
signed!!! LDAA N1 CMPA N2 BLE LBL1 <<statement1>> LBL1
if (a > b) statement1;
18
20
21
23
BRCLR
BRCLR
Instruction
0,X
$F0
$F2
JUMP Address Mask Data address
27
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 PC-14 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 8-bit elements. The array is stored at $D000-$D013. Save the sum at $D020-$D021.
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 16-bit number stored at $D000-$D001. Save the result in $D002. Solution :
The 16-bit 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.
33
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
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
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
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 AF 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
09 08 AF 9F 09 AF 02 02 03
09 AF 09 AF 03 03 09 AF 03
0 0 6 6 0 6 6 6 6
0 6 0 6 6 6 0 6 6
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 16-bit 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
41
43
Example
Write a program to convert the 16-bit 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 16-bit 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
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
45
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.
48
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
51
Three Methods
1. In registers 2. In a common data area 3. On the stack
by value by reference
; number of bytes to copy ; starting memory location ; destination address ; push PC, jump to address COPYM
0,X 0,Y
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
52
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
LOOP
Disadvantages
Slower than passing by register (accesses memory)
54 Week 4: Branch Instructions and BCD Operations 56
57
59
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 side-effect.
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
63
Side-effects
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?
62
64
Programming exercises
Q1. Write the assembly-code instructions to call the delay-loop function with an argument of 25 Q2. How would you modify the delay-loop function to increase the length of the delays it produces by a factor of 10? Q3. How would you modify the delay-loop function so that the argument value 0 produces a 0-millisecond delay instead of a 256-millisecond delay?
65 Week 4: Branch Instructions and BCD Operations 67
Programming exercises
Q4. Modify the delay-loop 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.
68