Académique Documents
Professionnel Documents
Culture Documents
Comments
Explains the function of a single or a group of instructions
Operand field
Follows the operation field and is separated from the operation field by at least one space Contains operands for instructions or arguments for assembler directives
Operation field
Contains the mnemonic of a machine instruction or an assembler directive Separated from the label by at least one space
Comment field
Any line starts with an * or ; is a comment Separated from the operand and operation field for at least one space Optional
(1) no label field (b) movb is an instruction mnemonic (c) 0,X,0,Y is the operand field (d) ; memory to memory copy is a comment
Assembler Directives
END
Ends a program to be processed by an assembler Any statement following the END directive is ignored.
ORG
The assembler uses a location counter to keep track of the memory location where the next machine code byte should be placed. This directive sets a new value for the location counter of the assembler. The sequence
ORG $1000 LDAB #$FF places the opcode byte for the instruction LDAB #$FF at location $1000.
dc.b (define constant byte) db (define byte) fcb (form constant byte) - These three directives define the value of a byte or bytes that will be placed at a given location. - These directives are often preceded by the org directive. - For example, org $800 array dc.b $11,$22,$33,$44 dc.w (define constant word) dw (define word) fdb (form double bytes) - Define the value of a word or words that will be placed at a given location. - The value can be specified by an expression. - For example, vec_tab dc.w $1234, abc-20
ds (define storage) rmb (reserve memory byte) ds.b (define storage bytes)
- Each of these directives reserves a number of bytes given as the arguments to the directive. - Example buffer ds 100 reserves 100 bytes
equ (equate)
- This directive assigns a value to a label. - Using this directive makes ones program more readable. - Examples arr_cnt equ 100 oc_cnt equ 50
loc
- This directive increments and produces an internal counter used in conjunction with the backward tick mark (`). - By using the loc directive and the ` mark, one can write program segments like the following example, without thinking up new labels: loc loc ldaa #2 ldaa #2 loop` deca same as loop001 deca bne loop` bne loop001 loc loc loop` brclr 0,x,$55,loop` loop002 brclr 0,x,$55,loop002
Macro
- A name assigned to a group of instructions - Use macro and endm to define a macro. - Example of macro sumOf3 ldaa adda adda endm macro arg1 arg2 arg3 arg1,arg2,arg3
- Invoke a defined macro: write down the name and the arguments of the macro sumOf3 $1000,$1001,$1002 is replaced by ldaa $1000 adda $1001 adda $1002
Programming. Convert the algorithm or flowchart into programs. Program testing Program maintenance
Symbols of Flowchart
Terminal A
Process
Subroutine
Input or output
Decision no
A on-page connector
org $1500
ldd $1000 addd$1002 std $1100
end
The Carry Flag - bit 0 of the CCR register - set to 1 when the addition operation produces a carry 1 - set to 1 when the subtraction operation produces a borrow 1 - enables the user to implement multi-precision arithmetic
signed 32 by 16 divide 16 by 16 fractional divide unsigned 16 by 16 integer divide signed 16 by 16 integer divide
Example 2.11 Write an instruction sequence to divide the 16-bit number stored at $1020-$1021 into the 16-bit number stored at $1005-$1006 and store the quotient and remainder at $1100 and $1102, respectively. Solution: ldd ldx idiv stx std $1005 $1020 $1100 $1102 ; store the quotient ; store the remainder
upper half lower half upper half lower half upper half lower half upper half lower half
Address
P ~ P+1 msb
P+2 ~ P+3
Note: msb stands for most significant byte and lsb for least significant byte Figure 2.3 Unsigned 32-bit by 32-bit multiplication
Example 2.12 Write a program to multiply two unsigned 32-bit numbers stored at M~M+3 and N~N+3, respectively and store the product at P~P+7. Solution: org $1000 M ds.b 4 N ds.b 4 P ds.b 8 org $1500 ldd M+2 ldy N+2 emul ; compute MLNL sty P+4 std P+6 ldd M ldy N emul ; compute MHNH sty P std P+2 ldd M ldy N+2 emul ; compute MHNL
; add MHNL to memory locations P+2~P+5 addd P+4 std P+4 tfr Y,D adcb P+3 stab P+3 adca P+2 staa P+2 ; propagate carry to the most significant byte ldaa P+1 adca #0 ; add carry to the location at P+1 staa P+1 ; ldaa P ; add carry to the location at P adca #0 ; staa P ; ; compute MLNH ldd M+2 ldy N emul
; add MLNH to memory locations P+2 ~ P+5 addd P+4 std P+4 tfr Y,D adcb P+3 stab P+3 adca P+2 staa P+2 ; propagate carry to the most significant byte clra adca P+1 staa P+1 ldaa P adca #0 staa P end
Example 2.13 Write a program to convert the 16-bit number stored at $1000-$1001 to BCD format and store the result at $1010-$1014. Convert each BCD digit into its ASCII code and store it 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 generates the least significant digit, the second division by 10 obtains the second least significant digit, and so on.
data result org dc.w org ds.b org ldd ldy ldx idiv addb stab xgdx ldx $1000 12345 $1010 5 $1500 data #result #10 #$30 4,Y ; convert the digit into ASCII code ; save the least significant digit ; data to be tested ; reserve bytes to store the result
#10
idiv adcb stab xgdx ldx idiv addb stab xgdx ldx idiv addb stab xgdx addb stab end
#$30 3,Y #10 #$30 2,Y #10 #$30 1,Y #$30 0,Y
Program Loops
Types of program loops: finite and infinite loops Looping mechanisms:
do statement S forever For i = n1 to n2 do statement S or For i = n2 downto n1 do statement S While C do statement S Repeat statement S until C
Program loops are implemented by using the conditional branch instructions and the execution of these instructions depends on the contents of the CCR register.
S
C false
true S
I i2 ? yes S II+ 1
I i1 ? yes
initialize C
S
S II-1
C false
true
(a) For I = i1 to i2 DO S
Table 2.2 Summary of short branch instructions Unary Branches Mnemonic BRA BRN Mnemonic BCC BCS BEQ BMI BNE BPL BVC BVS Mnemonic BHI BHS BLO BLS Function Branch always Branch never Simple Branches Function Branch if carry clear Branch if carry set Branch if equal Branch if minus Branch if not equal Branch if plus Branch if overflow clear Branch if overflow set Unsigned Branches Function Branch if higher Branch if higher or same Branch if lower Branch if lower or same Signed Branches Mnemonic BGE BGT BLE BLT Function Branch if greater than or equal Branch if greater than Branch if less than or equal Branch if less than Equation or Operation N V= 0 Z + (N V) = 0 Z + (N V) = 1 N V= 1 Equation or Operation 1 =1 1 =0 Equation or Operation C=0 C=1 Z=1 N=1 Z=0 N=0 V=0 V=1 Equation or Operation C + Z= 0 C=0 C=1 C + Z= 1
Table 2.3 Summary of long branch instructions Unary Branches Mnemonic LBRA LBRN Mnemonic LBCC LBCS LBEQ LBMI LBNE LBPL LBVC LBVS Mnemonic LBHI LBHS LBLO LBLS Function Long branch always Long branch never Simple Branches Function Long branch if carry clear Long branch if carry set Long branch if equal Long branch if minus Long branch if not equal Long branch if plus Long branch if overflow is clear Long branch if overflow set Unsigned Branches Function Long branch if higher Long branch if higher or same Long branch if lower Long branch if lower or same Signed Branches Mnemonic LBGE LBGT LBLE LBLT Function Long branch if greater than or equal Long branch if greater than Long branch if less than or equal Long branch if less than Equation or Operation N V= 0 Z + (N V) = 0 Z + (N V) = 1 N V= 1 Equation or Operation 1 =1 1 =0 Equation or Operation C=0 C=1 Z=1 N=1 Z=0 N=0 V=0 V=1 Equation or Operation C + Z= 0 C=0 C=1 C + Z= 1
HCS12 provides a group of instructions that either decrement or increment a loop count to determine if the looping should be continued. The range of the branch is from $80 (-128) to $7F (+127).
Mnemonic
Function
Equation or Operation
Decrement counter and branch if = 0 counter (counter) - 1 DBEQ cntr, rel (counter = A, B, D, X, Y, or SP) If (counter) = 0, then branch else continue to next instruction Decrement counter and branch if 0 counter (counter) - 1 DBNE cntr, rel (counter = A, B, D, X, Y, or SP) If (counter) 0, then branch else continue to next instruction Increment counter and branch if = 0 counter (counter) + 1 IBEQ cntr, rel (counter = A, B, D, X, Y, or SP) If (counter) = 0, then branch else continue to next instruction Increment counter and branch if 0 counter (counter) + 1 IBNE cntr, rel (counter = A, B, D, X, Y, or SP) If (counter) 0, then branch else continue to next instruction Test counter and branch if = 0 If (counter) = 0, then branch TBEQ cntr, rel (counter = A, B, D, X, Y, or SP) else continue to next instruction Test counter and branch if 0 TBNE cntr, rel (counter = A, B, D, X, Y, or SP) If (counter) 0, then branch else continue to next instruction
Note. 1. cntr is the loop counter and can be accumulator A, B, or D and register X, Y, or SP. 2. rel is the relative branch offset and is usually a label
Example 2.14 Write a program to add an array of N 8-bit numbers and store the sum at memory locations $1000~$1001. Use the For i = n1 to n2 do looping construct. Solution:
N sum i equ org rmb rmb org ldaa staa staa staa ldab cmpb beq ldx abx ldab ldy aby sty 20 $1000 2 1 $1500 #0 i sum sum+1 i #N done #array 0,X sum sum
Start i 0 sum 0
; sum 0 ; ; is i = N?
yes
Stop
loop
done array
inc i ; increment the loop count by 1 bra loop swi dc.b 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 end
Example 2.15 Write a program to find the maximum element from an array of N 8bit elements using the repeat S until C looping construct. Solution:
no
no
i = 0? yes Stop
equ org max_val ds.b org ldaa staa ldx ldab loop ldaa cmpa bge ldaa staa chk_end dex dbne forever bra array db db end
20 $1000 1 $1500 array max_val #array+N-1 #N-1 max_val 0,x chk_end 0,x max_val
; set array[0] as the temporary max max ; ; start from the end of the array ; set loop count to N - 1
where opr specifies the memory location to be checked and must be specified using either the direct, extended, or index addressing mode. msk is an 8-bit 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 8-bit relative mode.
For example, in the sequence loop inc count brclr $66,$e0,loop the branch will be taken if the most significant three bits at $66 are all ones.
Example 2.17 Write a program to compute the number of elements that are divisible by 4 in an array of N 8-bit elements. Use the repeat S until C looping construct. Solution: A number divisible by 4 would have the least significant two bits equal 0s.
N equ org total ds.b org clr ldx ldab loop brclr bra yes inc chkend inx dbne forever bra array db end 20 $1000 1 $1500 total ; initialize total to 0 #array #N ; use B as the loop count 0,x,$03,yes ; check bits 1 and 0 chkend total b,loop forever 2,3,4,8,12,13,19,24,33,32,20,18,53,52,80,82,90,94,100,102
The operation is
C b7 ----------------- b0
b7 ----------------- b0 accumulator A
b7 ----------------- b0 accumulator B
The HCS12 has arithmetic shift right instructions that apply to a memory location and accumulators A and B.
[<label>] asr opr [<comment>] [<label>] asra [<comment>] [<label>] asrb [<comment>] The operation is -- memory location opr is shifted right one place -- accumulator A is shifted right one place -- accumulator B is shifted right one place
b7 ----------------- b0
The HCS12 has logical shift left instructions that apply to a memory location and accumulators A and B.
[<label>] lsl opr [<label>] lsla [<label>] lslb The operation is C b7 ----------------- b0 0 [<comment>] [<comment>] [<comment>] -- memory location opr is shifted left one place -- accumulator A is shifted left one place -- accumulator B is shifted left one place
b7 ----------------- b0
b7 ----------------- b0
accumulator B
accumulator A
The HCS12 has three logical shift right instructions that apply to 8-bit operands.
[<label>] lsr opr [<label>] lsra [<label>] lsrb The operation is [<comment>] [<comment>] [<comment>] 0 -- memory location opr is shifted right one place -- accumulator A is shifted right one place -- accumulator B is shifted right one place C
b7 ----------------- b0
[<comment>]
b7 ----------------- b0 accumulator A
b7 ----------------- b0 accumulator B
The HCS12 has three rotate left instructions that operate on 9-bit operands.
[<label>] rol opr place [<label>] rola [<label>] rolb The operation is [<comment>] [<comment>] [<comment>] -- memory location opr is rotated left one -- accumulator A is rotated left one place -- accumulator B is rotated left one place C
b7 ----------------- b0
The HCS12 has three rotate right instructions that operate on 9-bit operands.
[<label>] ror opr [<comment>] place [<label>] rora [<comment>] [<label>] rorb [<comment>] The operation is -- memory location opr is rotated right one
-- accumulator A is rotated right one place -- accumulator B is rotated right one place
b7 ----------------- b0
Example 2.18 Suppose that [A] = $95 and C = 1. Compute the new values of A and C after the execution of the instruction asla. accumulator A Solution:
1 C flag 0 0 0 1 0 1 0 1 0
Example 2.19 Suppose that m[$800] = $ED and C = 0. Compute the new values of m[$800] and the C flag after the execution of the instruction asr $1000. Solution:
1 memory location $1000 1 1 1 1 0 1 1 0 1 1 1 0 1 1 0 1 C flag
Example 2.20 Suppose that m[$1000] = $E7 and C = 1. Compute the new contents of m[$1000] and the C flag after the execution of the instruction lsr $1000. Solution: 1 1 1 0 0 1 1 1
memory location $800 0 0 1 1 1 0 0 1 1 1 C flag
Original value
New value
[$800] = 11100111 [$800] = 01110011 C=1 C=1 Figure 2.13b Execution result of LSR $800
Example 2.21 Suppose that [B] = $BD and C = 1. Compute the new values of B and the C flag after the execution of the instruction rolb. Solution:
1 0 1 1 1 1 0 1 1 C flag accumulator B 0 1 1 1 1 0 1 1 1
Example 2.22 Suppose that [A] = $BE and C = 1. Compute the new values of mem[$00] after the execution of the instruction rora. Solution:
0 C flag
accumulator A
Example 2.23 Write a program to count the number of 0s in the 16-bit number stored at $1000-$1001 and save the result in $1005. Solution: * The 16-bit number is shifted to the right 16 time. * If the bit shifted out is a 0 then increment the 0s count by 1. org db org zero_cnt rmb lp_cnt rmb org clr ldaa staa ldd loop lsrd bcs inc chkend dec bne forever bra end $1000 $23,$55 $1005 1 1 $1500 zero_cnt #16 lp_cnt $1000 ; test data
; 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
again
Assume that the HCS12 runs under a crystal oscillator with a frequency of 16 MHz, then the E frequency is 8 MHz and, hence, its clock period is 125 ns. Therefore, the instruction sequence on the next page will take 5 ms to execute.
Example 2.25 Write a program loop to create a delay of 100 ms. Solution: A delay of 100 ms can be created by repeating the previous loop 20,000 times. The following instruction sequence creates a delay of 100 ms. ldx #20000 loop psha ; 2 E cycles pula ; 3 E cycles psha pula psha pula psha pula psha pula psha pula psha pula nop ; 1 E cycle nop ; 1 E cycle dbne x,loop ; 3 E cycles
Example 2.26 Write an instruction sequence to create a delay of 10 seconds. Solution: By repeating the previous instruction sequence 100 times, we can create a delay of 10 seconds.
ldab out_loop ldx in_loop psha pula psha pula psha pula psha pula psha pula psha pula psha pula nop nop dbne dbne