eem336 week04 branch bcd subr 4pp
TRANSCRIPT
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
1/18
Week 4: Branch Instructions and BCD Operations 1
Branch Instructions
Conditional Branching, BCD,
Subroutines
Week 4: Branch Instructions and BCD Operations 2
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 thecurrent one
Branch instructions allow the sequence to
be varied
Week 4: Branch Instructions and BCD Operations 3
Conditional Branching
Conditionalbranch 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 4
Branch Instruction Format
Typical machine code format:
relative addressop-code
Relative to the
location of next
instruction
Each branch
instruciton has a
different opcode
8bits 8bits
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
2/18
Week 4: Branch Instructions and BCD Operations 5
Relative Address
It is a 2s complement signed integer (128... +127)
Specifies the differencebetween the addressof the next instruction if you do follow thebranch, and that if you dont
Using relative address rather than absolutemeans youre not dependent on whereprogram is loaded in memory
Week 4: Branch Instructions and BCD Operations 6
Relative
Address
Example $22br-op
$F2C231
br-opC230
opC224
$FEC203
br-opC202
C201
C200
Address
(hex) Jumps to$C202 + $22
$FE (in 2'scomplement)
= -2
jumps to $C204
2
(i.e. $C202)
$F2 (in 2's
complement)
= -14
jumps to
$C232 14
(i.e. $C224)
Week 4: Branch Instructions and BCD Operations 7
Typical Set of Branch Instructions
Branch if less than or equal=BGE
Branch if less thanBGT
Branch if not equal!=BNE
Branch if equal= =BEQ
Branch always (unconditional)BRA
ExplanationConditionMnemonic
Week 4: Branch Instructions and BCD Operations 8
Branch if plus>=0BPL
Branch if minus< 0BMI
Branch if overflow clearBVC
Branch if overflow setBVS
Branch if carry clearBCC
Branch if carry setBCS
Branch if lower or sameunsigned =BHS
Branch if lowerunsigned BHI
in wordsconditionmnemonic
Typical Set of Branch Instructions
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
3/18
Week 4: Branch Instructions and BCD Operations 9
Branch Code Interpretation
Most branch codes are interpreted in
the context of asubtraction
integer
b
integer
a
CCR flags areupdated
Appropriate
flags are checked
to execute BGT
properly
-
Week 4: Branch Instructions and BCD Operations 10
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 assigned
The mathematical operation preceding the branchstatement will have the same result whether youre
dealing with signed or unsigned integers.
Its the branch statement you use that determines
how theyre interpreted
Week 4: Branch Instructions and BCD Operations 11
Signed vs Unsigned Arithmetic
a = 11110000
b = 00001111
in 2s complementform a=-16
Ifit'staken as
unsigned, a=240
CCR flags are updated
considering a and b both
signed and unsigned Use BHS, BLO if
you assume a and b
as unsigned
numbers
Let
Use BGE, BLT if
you assume a andb
as signed numbers
Week 4: Branch Instructions and BCD Operations 12
Signed vs Unsigned Arithmetic
Example 1
a = 11110000 (-16/240)
b = 00001111 (15)
ab = 11100001
(-31/ 225)
Z N C V
0 1 0 0
BGT will not
branch since
N = 1
BHI will
branch since
C = 0
11100001 is either
31 or 225
depending on
how you interpret
the source values
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
4/18
Week 4: Branch Instructions and BCD Operations 13
Signed vs Unsigned Arithmetic
Example 2
a = 11110000 (-16/240)
b = 11110001 (-15/241)
ab = 11111111
(-1/ 255)
Z N C V
0 1 1 0
BGT will not
branch since
N = 1
BHI will not
branch since
C = 1
if you assumea and
b as signed
numbers, you use
BGE or BLT,
otherwise BHS or
BLO
Week 4: Branch Instructions and BCD Operations 14
Example
LDAA #%11110000
CMPA #%00001111
BHS SKIP
INCA
SKIP ....
....
LDAA #%11110000
CMPA #%00001111
BGE SKIP
INCA
SKIP ....
....
unsigned signed
240 >= 15, branches to
address SKIP-16 < 15, it executes
INCA
Week 4: Branch Instructions and BCD Operations 15
Branch Conditions
BCS branches if C = 1
BVC branches if V = 0
BPL branches if N = 0 (even if Z=1)
BEQ branches if Z = 1
BLT branches if N = 1
BHI branches if C = 0 AND Z = 0
Week 4: Branch Instructions and BCD Operations 16
Example
You have a list of unsigned numbersstarting at address $D101.
Address $D100 contains the length of thelist.
Write a program segment (starting ataddress $C000) that returns how manynumbers that are greater than or equal to100 in Accumulator A.
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
5/18
Week 4: Branch Instructions and BCD Operations 17
Example Solution
ORG $C000 sets starting address
SUBR LDAB $D100 load length
CLR CTR clear temp variable
LDX #$D101 list head
LOOP LDAA 0,X load from list
CMPA #100 compare
BLO SKIP less than? (unsigned)
INC CTR if >=, increment temp var.
SKIP INX next element in the list
DECB decrement list counter
BNE LOOP if not EOL, goto beginning
LDAA CTR Accu A returns the counter
SWI return to monitor
CTR RMB 1 temporary variable
Week 4: Branch Instructions and BCD Operations 18
Solution (if numbers are signed)
ORG $C000 sets starting address
BEGIN LDAB $D100 load length
CLR CTR clear temp variable
LDX #$D101 list head
LOOP LDAA 0,X load from list
CMPA #100 compare
BLT SKIP less than? (signed)INC CTR if >=, increment temp var.
SKIP INX next element in the list
DECB decrement list counter
BNE LOOP if not EOL, goto beginning
LDAA CTR Accu A returns the counter
SWI return to monitor
CTR RMB 1 temporary variable
Week 4: Branch Instructions and BCD Operations 19
Fixing Limitations
What if the destination is too far?
Outside 128 .. +127
e.g. BEQ TOOFAR
Reverse the logic as follows:
BNE L1
JMP TOOFAR
L1 >
Reverse of
BEQ must be
used in the
reverse logic
Week 4: Branch Instructions and BCD Operations 20
Example of Use: If
if (a > b)
statement1;
LDAA N1
CMPA N2
BLE LBL1
LBL1
signed!!!
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
6/18
Week 4: Branch Instructions and BCD Operations 21
if (a > b)
statement1;
else
statement2;
LDAA N1
CMPA N2
BLE LBL1
BRA LBL2
LBL1 B)
{statement;
}
while (test on entry)
WHLE LDAA A
CMPA B
BLE QUIT
BRA WHLE
QUIT
Week 4: Branch Instructions and BCD Operations 23
do-while (test on exit)
do
{
statement;
} while (A>B);
DOWH
LDAA A
CMPA B
BLE QUIT
BRA DOWH
QUIT
Week 4: Branch Instructions and BCD Operations 24
Some Coding Hints LDAA and STAA WILL NOT affect carry
Most arithmetic operations WILL change carry
Logic operations typically WILL NOT changecarry
If have two arithmetic processes in program,
desired carry information can be DESTROYED ifcare not taken
There are many ways to implement a program to dosomething
You should plan out your coding before writingcode
Use EQU to define constants
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
7/18
Week 4: Branch Instructions and BCD Operations 25
Bit-Manipulation Branch
Instructions
The 68HC11 MPU has two conditional
branch instructions that are much different
than the two-byte branch instructions wehave been using up to now. These special
conditional branch instructions are:
Mnemonic Condition for branching
BRCLRSpecific bits of operand are clear (0)
BRSET Specific bits of operand are set (1)
Week 4: Branch Instructions and BCD Operations 26
BRSET and BRCLR
[] BRCLR (opr) (msk) (rel)
[] BRSET (opr) (msk) (rel)
where opr specifies the memory location tobe checked
and must be specified using either the direct or indexaddressing mode.
msk is an 8-bit mask that specifies the bits of thememory location to be checked. The bits of thememory byte to be checked correspond to those bitpositions that are 1s in the mask.
rel is the branch offset and is specified in therelative mode.
Week 4: Branch Instructions and BCD Operations 27
BRCLR
BRCLR 0,X $F0 $F2
JUMP Address
Mask
Data address
Instruction
Week 4: Branch Instructions and BCD Operations 28
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.
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
8/18
Week 4: Branch Instructions and BCD Operations 29
Example
In executing this instruction, the CPU will
fetch the operand byte from the operandaddress (in this case, 0,X).
Then it will AND that operand with themask byte ($F0).
If the resulting byte is all zeros, the MPUwill branch to address PC-14 for its nextinstruction;
BRCLR 0,X $F0 $F2
Week 4: Branch Instructions and BCD Operations 30
Example
For example, the sequence
will force the 68HC11 continue to execute the
second instruction until the bit 7 is set to 1.
ldx #$1000
here brclr $30,X %10000000 hereldaa $31,X
Week 4: Branch Instructions and BCD Operations 31
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.
Week 4: Branch Instructions and BCD Operations 32
SolutionN equ $D013 ; number elements in the array
org $D020
sum rmb 2 ; result
org $C000
clra
staa sum ; initialize sum to 0
staa sum+1 ; "
ldx #$D000 ; point X to array[0]
loop brclr 0,X $01 chkend ; is it an odd number?
ldd sum ; add the odd number to the sum addb 0,X ; "
adca #0 ; "
std sum ; "
chkend cpx #N ; compare the pointer to the address
; of the last element
bhs exit ; is this the end?
inx
bra loop ; not yet done, continue
exit end
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
9/18
Week 4: Branch Instructions and BCD Operations 33
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.
Week 4: Branch Instructions and BCD Operations 34
Solution
org $C000
ldaa #$00 ; initialize the 1s count to 0
staa $D002 ; "
ldd $D000 ; place the number in D
loop lsrd ; shift the lsb of D to the C flag
bcc tst0 ; is the C flag a 0?
inc $D002 ; increment 1s count if the lsb is a 1
tst0 cpd #0 ; check to see if D is already 0
bne loop
swi
Week 4: Branch Instructions and BCD Operations 35
Binary Coded Decimal
Operations
BCD addition, subtraction,
adjustment after operations
Week 4: Branch Instructions and BCD Operations 36
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 operationusing the DAA instruction
the instruction DAA can be applied after the
instructions ADDA, ADCA, and ABA
simplifies I/O conversion
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
10/18
Week 4: Branch Instructions and BCD Operations 37
BCD Example
For example, the instruction sequence
adds the BCD numbers stored at $D000 and
$D001 and saves the sum in the memory
location at $D002.
LDAA $D000ADDA $D001
DAA
STAA $D002
6 60 3 0 31 1
6 60 2 A F1 0
6 00 2 0 91 0
6 6A F 0 30 1
0 60 9 0 30 1
6 69 F A F0 0
6 0A F 0 90 0
0 60 8 A F0 0
0 00 9 0 90 0
Add to upper/lower
Including carries.
Upper lower
4 bits 4 bitsC H
Decimal AdjustTemporary Bin.
result
Status of Flag
bits
Week 4: Branch Instructions and BCD Operations 39
Decimal Adjust and Example
AccA $44 = 0 1 0 0 0 1 0 0
AccB $23 = 0 0 1 0 0 0 1 1
$67 0 0 1 1 0 0 1 1 1
Both BCD values
between 0 and 9
H=0, C=0
Dec. adjust =0
68HC11 has special instruction
Decimal Accumulator Adjust A: DAA
Week 4: Branch Instructions and BCD Operations 40
DAA: Example 2
AccA $52 = 0 1 0 1 0 0 1 0
AccB $54 = 0 1 0 0 0 1 0 0
$1F = 1 0 0 1 1 1 1 1 (the result of add)
+ 0 0 0 0 0 1 1 0 (correction: $06)
$25 = 0 0 1 0 0 1 0 1 (BCD number)
lower BCD AFH=0, C=0
Decimal adjust =$06
correct value
in this case,
but not proper
BCD
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
11/18
Week 4: Branch Instructions and BCD Operations 41
DAA: Example 3
AccA $18 = 0 0 0 1 1 0 0 0
AccB $07 = 0 0 0 0 0 1 1 1
$1F = 0 0 0 1 1 1 1 1 (the result of add)
+ 0 1 1 0 0 0 0 0 (correction: $60)
C=1 $06 = 0 0 0 0 0 1 1 0 (BCD number)
higher BCD A FH=0, C=0
Decimal adjust =$60
correct value
in this case,
but not proper
BCD
Week 4: Branch Instructions and BCD Operations 42
Example
Write a program to convert the 16-bit number storedat $D000-$D001 to BCD
format and store the result at $D002-$D006. EachBCD digit is stored in one byte.
Solution:
A binary number can be converted to BCD format byusing repeated division by 10.
The largest 16-bit binary number is 65535 which has fivedecimal digits.
The first division by 10 obtains the least significant digit,the second division by 10 obtains the second leastsignificant digit, and so on.
Week 4: Branch Instructions and BCD Operations 43
SolutionLDD $D000 ; place the 16-bit number in D
LDX #10
IDIV ; compute the least significant digit
STAB $D006 ; save the least significant BCD digit
XGDX ; place the quotient in D
LDX #10
IDIV ; compute the second least significant BCD digit
STAB $D005 ; save the second least significant BCD digitXGDX ; place the quotient in D
LDX #10
IDIV ; compute the middle BCD digit
STAB $D004 ; save the middle BCD digit
XGDX
LDX #10
IDIV ; compute the second most significant digit
STAB $D003 ; the second most significant BCD digit
XGDX
STAB $D002 ; save the most significant BCD digit
END
Week 4: Branch Instructions and BCD Operations 44
Subroutines
Functions, passing arguments to
subroutines
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
12/18
Week 4: Branch Instructions and BCD Operations 45
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 thattask, 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.
Week 4: Branch Instructions and BCD Operations 46
Subroutines
JSR and RTS Instructions are used to direct the Pto jump to or return from a subroutine respectively
The Jump to SubRoutine, JSR, requires asubroutine address as an operand and will causethe P to push the current PC onto the stack (the
return address) and then initiate a jump to theaddress 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 executionof the main program
Week 4: Branch Instructions and BCD Operations 47
Subroutines
....
....
JSR SUBR ;push PC,jump to address SUBR
LDAA #$01 ; this instr. will be executed after RTS
....
....
....
SUBR PSHX
LDX $D400
....
RTS ; pull the return addressfrom stack
.... ; jump to thatlocation
....
....
....
Week 4: Branch Instructions and BCD Operations 48
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 subroutineis 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.
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
13/18
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
14/18
Week 4: Branch Instructions and BCD Operations 53
Passing Arguments in Registers
Advantages
Very Fast (doesnt need a memory cycle)
Disadvantages Limited by number of registers
A register being used to hold an argument is not
available for other uses
Not recursive
Week 4: Branch Instructions and BCD Operations 54
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)
Week 4: Branch Instructions and BCD Operations 55
Passing Arguments on the StackLDAA #$12 ; number of bytesto clear
PSHA ; push it to stack
LDY #$D200 ; starting memory location
PSHY ; push it to stack
JSR ZEROM ; push PC, jump to addressZEROM
PULX ; restore stack (we reclaim stack)
PULA ; restore stack (we reclaim stack)....
....
ZEROM TSY ; SP+1 IY
LDAA 4,Y ; load counter(number of bytes)
LDX 2,Y ; load beginning address
LOOP CLR 0,X ; clear location
INX ; next location
DECA ; decrement counter
BNE LOOP ; if not zero, goto the beginning of loop
RTS ; we are done, get back to where we left
Week 4: Branch Instructions and BCD Operations 56
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)
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
15/18
Week 4: Branch Instructions and BCD Operations 57
Passing arguments in Common
Memory
How?
The caller stores the argument values in
known (advertised) locations before callingthe function
Week 4: Branch Instructions and BCD Operations 58
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 59
Example: delay-loop 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 takesthe 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 60
The delay-loop function
DELAY LDY #570 ; load 570 into IY
L1 DEY ; decrement IY
BNE L1
DECA ; decrement ABNE DELAY
RTS
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
16/18
Week 4: Branch Instructions and BCD Operations 61
Points to note
1. There is no special syntax for defining afunction
2. The label DELAY marks the beginning of thefunction; a call to it could be written
JSR DELAY3. The programmer needs to know that this
function expects its argument in accumulator Ain order to write the call properly
4. The function uses the IY register as a counter.Anything previously in IY will be lost! Youmust be aware of this side-effect.
Week 4: Branch Instructions and BCD Operations 62
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 do-while loops. HLL equivalent:do {
IY = 570;
do {IY--;} while (IY != 0);
A--;}
while (A != 0);
Week 4: Branch Instructions and BCD Operations 63
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 actualdelay will therefore depend on how fast
the CPU executes the instructions
Week 4: Branch Instructions and BCD Operations 64
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?
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
17/18
Week 4: Branch Instructions and BCD Operations 65
Curing the Side-Effects
DELAY PSHY ; save IY on stack
L1 LDY #570
L2 DEYBNE L2
DECA
BNE L1
PULY ; restore Y
RTS
Week 4: Branch Instructions and BCD Operations 66
Curing the side-effects
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 67
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?
Week 4: Branch Instructions and BCD Operations 68
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 valuepassed in accumulator A, or 200, whichever
is smaller.
-
7/22/2019 EEM336 Week04 Branch Bcd Subr 4pp
18/18
Week 4: Branch Instructions and BCD Operations 69
Subroutine Return Values
How can we return values or
addresses from subroutines to the
calling program?
Week 4: Branch Instructions and BCD Operations 70
Function Return Values
All three mechanisms for passing arguments tofunctions can also be used to pass a returnvalue back to the caller
It is therefore possible to have functionsreturning more than one value. However,programming conventions do not normallyallow it
Use passing by reference if you want to do this
Week 4: Branch Instructions and BCD Operations 71
Passing by Reference
This means that you pass the address of anargument to a function instead of its value
For large objects (e.g. an array) it is muchquicker than passing the whole object
Arguments passed by reference can bewritten to by the function, and thenaccessed by the caller; they therefore serveas input andoutput arguments.
Week 4: Branch Instructions and BCD Operations 72
HLL Passing by Reference
Original HLL:
bigobj = bigReturn(arg);
This becomes:
bigReturn(&bigobj, arg);
hidden extra
argument
In an HLL (High
Level Language),
you can define
functions to return
large objects.
Most compilers will
change your code to
this representation.