来到美国后,选了微处理器课,有幸老教授是 MIT 贝尔实验室出来的,希望好好学习这门课。
三态缓冲器(Three-state buffer),又称为三态门、三态驱动器,其三态输出受到使能输出端的控制,当使能输出有效时,器件实现正常逻辑状态输出(逻辑0、逻辑1),当使能输入无效时,输出处于高阻状态,即等效于与所连的电路断开。
•Note that there are 4 possible outcomes when adding a pair of binary numbers
•0 + 0 = carry 0, sum 0
•0 + 1 = carry 0, sum 1
•1 + 0 = carry 0, sum 1
•1 + 1 = carry 1, sum 0
•Recall that the AND gate only outputs 1 if both inputs are true, thus it could be used to output the carry bit.
•Recall that the XOR gate only outputs 1 if and only if one of the inputs are true.
另一种形式
Application of the previous 3 Bit Adder
•Assume we want to add the binary numbers 010 and 011..
•Using the truth table shows before..
•We know that 0+1 with no carry in will result in Sum of 1 with no carry
•Also, 1+1 with no carry in will result in sum of 0 with carry out of 1 •Finally, 0+0 with carry in will result in sum of 1 with no carry
•Result is 0101
•The take away here is that we can cascade as many full adders as we want to add as large of a pair of binary numbers as we want.
•For example a 64 bit sum would require 64 cascaded adders
•3 major subsystems
•CPU – Central Processing Unit •Memory •I/O Input Output
•Connected by buses
•Data •Address •Control
More About the Data Bus
•Analogous to Highway
•The more lanes, the more simultaneous traffic
•The more data buses the better
•This adds to expense
•Note that we currently have 64 bit CPUs and operating systems
•The size of the data bus is related to both size and precisions of arithmetic operations
More About the Address Bus
•Every addressable device must have a unique address
•The larger the address bus, the larger number of devices which may be addressed by the CPU
•Recall that decoders are used on addressable device to establish their address.
•The number of devices which may be addressed is always equal to 2^x where x is the number of address lines.
•For example, a 16 bit address bus can access 64k of addressable memory
•This is a unidirectional bus (send only from CPU)
•This is the number of bits a memory chip can store
•Chip capacity has units of bits, Computer system capacity has units of bytes
•Memory chips are organized into a number of locations within the IC
•Each location can hold 1, 4, 8, 16, or more bits
•The number of bits that each location will hold is equal to the number of data
pins on the chip.
•The number of locations on the chip is determined by the number of address pins on the chip say x. 2^x is the number of locations (addresses) on the chip
•Total number of bits which may be stored on the entire chip is given by 2^(address pins) times the number of data pins
Another Organization/Capacity Example
•We have a chip with 256k capacity with 8 data pins.
•The organization is 256k/8 = 32K x 8 •The number of address lines is 15 since 2^15 = 32K
Different types such as:
•PROM
•EPROM
•EEPROM
•FLASH EPROM
•MASK PROM
(1)PROM (programmable ROM) and OTP
(2)EPROM (erasable programmable ROM) and UV-EPROM
(3)Some UV-EPROM Memory Chips obsolete 已过时
(4)EEPROM (electrically erasable programmable ROM)
(5)Flash Memory EPROM
(6)Mask ROM
3 types
•Static RAM
•Nonvolatile RAM
•Dynamic Ram
(1)SRAM (static RAM)
(2)NV-RAM (nonvolatile RAM)
(3)DRAM (dynamic RAM)
•Simple Logic Gates
•Using 74LS138
•Programmable Logic such as CPLD or FPGA
详见:http://blog.csdn.net/scythe666/article/details/52428243
Using the 74LS138 3 – 8 decoder
•One of the most widely used decoders •3 inputs A, B, and C generate 8 active low outputs Y0 to Y7, •Each output is connected to CS of a different memory device allowing
control of 8 memory blocks
•Three additional inputs G1, G2A, and G2B may connected to an Address or Control Signal
•G2A and G2B active low •G1 active high •These inputs must be terminated in way or another •These inputs enable the device
LDAA #$35 ; loads 55 hex into Accumulator A
•# signifies an immediate value
•$ signifies that the immediate value is base 16
Absence of the # sign indicates that the value should be loaded from a memory location
•Eg LDAA $17 will load the contents of memory location 17 hex
•HCS12 has 6 distinct addressing modes
–Inherent 无操作数
–Immediate 立即数
–Extended 直接访问内存地址
–Direct 直接寻址寄存器
–Relative 间接寻址,多用于跳转
–Indexed 变址寻址,用 X、Y
小端是高高低低,这里类似于大端,因为hcs12里面,A地址小于B
大小端模式
Freescale 微处理器都是大端模式
特殊的 Extended Addressing,用地址空间寻址寄存器
Note the below equivalent methods of storing contents of register A into PORTA
•STAA $00 == •STAA PORTA
•Set whenever there is a carry out from the D7 bit
•Affected after 8 or 16 bit addition or subtraction
•Set when there is carry from D3 bit
•Cleared otherwise
•Used by operations that perform binary coded decimal arithmetic
•Sometimes called the Auxiliary Carry or AC flag
•Reflects the result of an arithmetic or logic operation
•Set to 1 for a 0 result
•Set to 0 if result is not zero
•Useful for looping
•This flag is set whenever the result of a signed number operation is too large
•The sign bit will therefore be overwritten
•Bit D7, the MSB in binary representation of 8 bit signed numbers is used to represent sign
–It is 0 for positive numbers and 1 for negative
numbers
•N is 0 when D7 is 0 •N is 1 when D7 is 1
4 ways to represent a byte of data in the HCS12 assembler
–Hex
–Binary
–Decimal
–ASCII
•Assembly:
–Precede number with $ sign
–Eg. LDDA #$25
•C language
–Precede number with 0x
–Eg. PORTB = 0x99
•Number is preceded by % sign
–Eg. LDDA #% 00100101 ; 25 hex
•There is no symbol in front of the number
–Eg LDDA #12 ; 0C hex
•Single quotes around the character are used to denote it as ASCII.
–Eg. LDAA #’9’ loads the value for the character 9
(39 hex)
•Double quotes are used for a string
–Use with the DC (define constant) directive
Assembler Directives
•This is used to define a constant value or fixed address
–Eg. COUNT EQU $25 ; Value of Count defined as 25 hex
–LDDA #COUNT ; Value of register A is 25 hex
SET
•Same as EQU except that:
–Value assigned by the SET directive may be reassigned later
USING EQU for RAM address assignment
•MYCOUNT EQU 5
•MYREG EQU $1200
–LDDA #MYCOUNT
–STAA MYREG ; NOW LOCATION 12OO H IS VALUE 5
Another address example
MYVAL EQU $33
MYRAM EQU $1200 ;RAM ADRESS 1200 H
LDAA #MYVAL ;A = 33H
STAA MYRAM ; STORE A CONTENTS TO 1200 H
STAA MYRAM +1 ; A CONTENT TO 1201 H
STAA MYRAM +2 ; ACONTET TO 1202 H
CLR A ; CLEAR CONTENTS 0F A
ADDA MYRAM ; A=33 H
ADDA MYRAM +1 ; A=66H
ADDA MYRAM +2 ; A=99 H
•ORG indicates the beginning of the address space
ORG tells the assembler to place the opcode at memory location 0
END DIRECTIVE
•END indicates the end of the source source code.
END indicates the end of the source code to the assembler
•Stack Pointer points to last used location of the stack
–SP is decremented each time we push
–This is a common convention
sp在ram最后
Ram area can be thought of as a large number of register
We can rename them as long as we do not use reserved names for SFR’s
•BNE stands for Branch if Not equal to 0
•For example
Limitation of previous example
•Because Count is an 8 bit register:
–Maximum of 255 times or FF loops
–One way to overcome this is a nested loop
•Target must be within 128 bytes of the contents of PC
•These are 2 bytes instructions
•First byte is opcode
•Second byte is relative address to PC
•Value of second byte may be between -127 and +128
•Jump forward on positive, backwards on negative
•Target address is sum of second byte and PC of next instruction
Branching Using the $ Sign
HERE JMP HERE ;EQUIVALENT TO JMP $
HERE BRA HERE ;EQUIVALENT TO BRA $
Complement A,即 COMA 指令,A 的反码
Another List Example
•Assume your particular HCS12 is equipped with 32K on chip Flash ROM.
–Which command would you use?
–JSR is 3 byte instruction and saves ROM
–We do not need to use CALL because we will not go out of 64 KB range
Bottom Line
•Use timers for more accurate software generated delays
•This is covered in detail in Chapter 9
变址寻址,用X,Y
Clearing Ram Locations with loop 2 bytes at once! Nothing but net
MOVB Can Replace Combination of LDAA and STAA
Many of the ports in the 9S12 may be used for either input or output
- DDR register contents determine the Port function at a given point in the program
Writing all 1’s to DDRx configures the Port as an output port and we can subsequently write to the Port itsef
Conversely, writing all 0’s to DDRx configures the Port as an input and we may subsequently read from it.
All ports are configured as input after reset
; code segment to toggle PORTB
LDAA #$FF ; A IS ALL ONES
STAA DDRB ; CONFIGURES PORT B AS OUTPUT
L1 LDAA #$55 ; A = 55 H
STAA PORTB ; PORT B IS 0101 0101 B
;NOTE THAT COMPLEMENT OF 0101 0101 IS 1010 1010 B OR AA H
JSR DELAY ; ZZZZ
LDAA #$AA ; A = AA H
STAA PORTB ; PORT B IS TOGGLED
JSR DELAY ; ZZZZ
BRA L1
; HERE IS ANOTHER WAY TO SKIN THE TOGGLE PORTB CODE
MOVB #$FF,DDRB ; CONFIGURE AS OUTPUT
L1 MOVB #$55,PORTB ; OUTPUT TO PORTB
JSR DELAY ; ZZZZ
MOVB #$AA,PORTB ; OUTPUT COMPLIMENT OF 55 H TO PORTB
JSR DELAY ; ZZZZ
BRA L1 ; TRY IT AGAIN FROM THE TOP
; PROGRAM TO READ PTH, OPERATE ON IT, THEN STORE RESULT IN PORTB
MOVB #$FF,DDRB ;PORT B IS OUTPUT
MOVB #00,DDRH ;PORT H IS INPUT
L2 LDAA PTH ; READ FROM PORT H
ADDA #5 ; OPERATE ON IT
STAA PORTB ; RESULT IN PORTB
BRA L2 ; INFINITE LOOP
In expanded mode(直接访问内存地址) the HCS12 uses Port A and Port B for address/data multiplexing allowing connection to external RAM and ROM
Port E, and Ports H thru T also have alternative functions and uses in expanded mode.
Analog to digital converter Pins
Depending on the package either 1 or 2, 8 channel ADC’s are available, (122 pin LQFP 2 are available)
We can use the ADC pins for I/O if not used by ADC
Some times it is convenient to access individual bits of Ports and RAM addresses
BSET & BCLR
; PROGRAM TO TOGGLE PB2 AND PB0
BSET DDRB,%00000101 ; THIS MAKES PB2 AN PB0 OUTPUT PINS
AGAIN BSET PORTB,%00000101 ; PB2 AND PB0 ARE HIGH
JSR DELAY ; ZZZZ
BCLR PORTB,%00000101 ; PB2 AND PB0 ARE LOW
JSR DELAY ; ZZZZ
BRA AGAIN ; INFINITE LOOP
; PROGRAM TO CREATE A 50% DUTY CYCLE SQUARE WAVE ON PORTA BIT 0
BSET DDRA,%00000001 ; BIT ZERO IS OUTPUT
HERE BSET PORTA,%00000001 ; PA0 IS HIGH
JSR DELAY ; ZZZZ
BCLR PORTA,%00000001 ; PA0 IS LOW
JSR DELAY ; ZZZZ
BRA HERE ; INFINITE LOOP
; PROGRAM TO CREATE A 66% DUTY CYCLE SQUARE WAVE ON BIT 4 OF PORT A
BSET DDRA,$04 ; DDRA BIT 4 IS OUTPUT
BACK BSET PORTA,$04 ; BIT 4 IS HIGH
JSR DELAY ; ONE RINGY DINGY
JSR DELAY ; TWO RINGY DINGIES
BCLR PORTA,$04 ; BIT 4 IS LOW
JSR DELAY ; ONE DELAY
BRA BACK ; INFINITE LOOP
; PROGRAM TO MONITOR PB2 BIT UNTIL IS BECOMES HIGH THEN WRITE A VALUE TO
; PORT A THEN SEND A HIGH TO LOW PULSE TO PE3
BCLR DDRB,%0000100 ; PB2 IS INTPUT
LDAA #$FF ; VALUE OF A IS FF H
STAA DDRA ; A IS OUTPUT
LDAA #$45 ; VALUE OF A IS 45 H
AGAIN BRSET PORTB,%00000100,OVER ; BRANCH TO OVER IF PB2 IS HIGH
BRA AGAIN ; LOOP UNTIL, MYSTERIOUSLY, PB2
; IS LOW
OVER STAA PORTA ; WRITING 45 H TO PORT A
BSET PORTE,%00001000 ; PE3 IS HIGH
BCLR PORTE,%00001000 ; PE3 IS LOW
; ANOTHER WAY TO ACCOMPLISH THE PREVIOUS EXAMPLE USING BRCLR
; INSTRUCTION
BCLR DDRB,%00000100 ; PB2 IS INPUT
LDAA #$FF ; A = FF H
STAA DDRA ; PORT A IS OUTPUT
BSET DDRE,%00001000 ; PE3 IS OUTPUT
LDAA #$45 ; A = 45 H
AGAIN BRCLR PORTB,%00000100,AGAIN ; CHECK IF PB2 IS LOW, IF SO BRANCH
; TO AGAIN
STAA PORTA ; ELSE WRITE PORT A
BSET PORTE,%00001000 ; PE3 IS HIGH
BCLR PORTE,%00001000 ; PE3 IS LOW
; THINK ABOUT THE APPLICATION OF ALARM MONITORING. WE COULD USE PB3 AS
; THE BIT THAT MONITORS THIS CONDITION, AND FURTHER ASSUME THAT IT IS
; PULLED LOW WHEN THE DOOR IS OPEN, IT IS A REQUIREMENT TO SEND A HIGH TO
; LOW PULSE TO PE5 TO TURN ON A BUZZER
BCLR DDRB,%00001000 ; PB3 IS INPUT
BSET DDRE,%00100000 ; PE5 IS OUTPUT
HERE BRSET PORTB,%00001000,HERE ; BRANCH IF PB3 IS HIGH TO HERE
BSET PORTE,%00100000 ; PE5 IS HIGH
BCLR PORTE,%00100000 ; PE5 IS LOW
$43
.这个时候需要考虑到补码,43H-05H=3EH,也就是0100 0011 - 0000 0101,这个时候减去这个数相当于加上这个数的反码,0100 0011 + 1111 1011 = 0011 1110
Upon completion, Students will be able to:
Define the range of numbers in HCS12 for unsigned data
Code addition and subtraction of numbers for unsigned numbers
Perform addition of BCD data
Code HCS12 multiplication a division of unsigned numbers
Code HCS12 logic instructions: AND, OR, and EX-OR
Code HCS12 assembly instructions for bit shift and rotate
Use HCS12 logic instructions for bit manipulation
Use Compare for program control
Code HCS12 rotate instructions and data serialization
Explain BCD system of data representation
Contrast and compare packed and unpacked BCD data
Code HCS12 programs for ASCII and BCD data conversion
Define the range of numbers possible in HCS12 for signed data
; Program to add two 16 bit numbers using ADCA instruction. The numbers are 3CE7
; H and 3B8D H. Assume big endian convention, and 3B8D are stored in locations 807
; and 806. The result is stored in locations 808 and 809
LDAA #$E7 ; A = VALUE OF FIRST NUMBER LOW BYTE
ADDA $807 ; LOCATION OF NEXT LOW BYTE
; E7 + 8D = 174 AND C = 1
STAA $809 ; STORE ACCORDING TO CONVENTION
LDAA #$3C ; A = VALUE OF FIRST NUMBER HIGH BYTE
ADCA $806 ; A = A + VALUE OF NEXT HIGH BYTE + 1
STAA $808 ; STORE RESULTANT HIGH BYTE BIG ENDIAN
; Program to add two multi-byte numbers
MYCOUNT EQU $800 ; ADDRESS OF COUNTER VARIABLE
LDAA #4 ; A = 4 D
STAA MYCOUNT ; VALUE OF MYCOUNT IS 4 D
CLRA ; A = 0
LDX #$813 ; X POINTER
LDY #$853 ; Y POINTER
CLC ; CLEAR CARRY FLAG
BACK LDAA 0,X ; LOAD BYTE AT POINTED ADDRESS
ADCA 0,Y ; ADD BYTE AT POINTED ADDRESS
STAA 0,Y ; STORE WITH CARRY
DEX ; DECREMENT POINTER X
DEY ; DECREMENT POINTER Y
DEC MYCOUNT ; DECREMENT LOOP COUNTER
BNE BACK ; BRANCH IF COUNTER NE 0
DAA stands for Decimal Adjust Accuumulator
Consider the following
LDAA #$47 ; A = $47
ADDA #$25 ; A = A + 25 H RESULT IS 6C H
DAA ; ADUST FOR BCD ADDITION, RESULT IS 72 H
; Program to subtract 6E H from 4C H
MYREG EQU $820 ; LOCATION OF NUMBER TO BE SUBTRACTED
LDAA #$6E ; A = 6E H
STAA MYREG ; STORE 6E H AT LOCATION 820 H
LDDA #$4C ; A = 4C H
SUBA MYREG ; A = (4C-6E) = DE, INVERTED C = 1
BCC NEXT ; IF C = 0 BRANCH TO NEXT TARGET
NEGA ; TAKE 2’S COMPLEMENT OF A
NEXT STAA MYREG ; RESULT IS STORED IN MYREG
; 6E = 01101110 2’S COMPLIMENT OF THIS IS 1001 0010
; AFTER THE SUBA COMMAND C =1 AND THE RESULT IS NEGATIVE WE DO NOT
; BRANCH AND 2’S COMPLEMENT IS TAKEN AND MYREG = 22 H MAGNITUDE OF
; NEGATIVE RESULT
; Program to subtract 2 16 bit numbers eg $2762 - $1296 assuming location 806 H
; stores 12 H and location 807 H stores 96 H, store result in same locations with big
; endian convention. We are looking for the result of 14CC
LDAA #$62 ; LOAD LOW BYTE
SUBA $807 ; A = 62 H – 96 H = CC H, C = BORROW = 1
STAA $807 ; STORE LOW BYTE
LDAA #$27 ; LOAD HIGH BYTE
SBCA $806 ; A = 27 – 12 – 1 = 14 H
STAA $806 ; STORE HIGH BYTE
; Since these are 16 bit or 2 byte numbers, we could us the instruction SUBD
LDD #2762 ; LOAD D REGISTER
SUBD $806 ; SUBTRACT THE VALUES AT $806 AND $807
STDD $806 ; STORE RESULT
; Multiply 25 H by 65 H
LDAA #$25 ; FIRST OPERAND IN A
LDAB #$65 ; SECOND OPERAND IN B
MUL ; 25 H x 65 H = E99 H IN REG D
; A = 0E H, B = 99 H
;
; 16 bit multiplication
LDD #500 ; D = 500 DECIMAL
LDY #700 ; Y = 700 DECIMAL
EMUL ; 500 x 700 = 350000 = 55730 H
; Y = 0005 H, D = 5730 H
; divide 255 decimal by 10 decimal
LDD #255 ; D = 255 DECIMAL
LDX #10 ; X = 10 DECIMAL
IDIV ; 255/10 = 25 REMAINDER 5
` ; X = 25 DECIMAL AND D = 5
;
; divide 90FF24 H by 1000 decimal
;
LDD #$FF24 ; LOWER WORD
LDY #$90 ; UPPER WORD
; 90FF24 H = 9502500 DECIMAL
LDX #1000 ; X = 1000 DECIMAL
EDIV ; Y:D/X = 9502 (251E H) REMAINDER 500 (1F4)
Division Application Example ADC
; ADC provides Hex data between 00 and FF H. Conversion to decimal is accomplished
; by dividing by 10 repetitively and saving remainder. This program converts FD H to
; decimal with byte by byte division using locations 822 – 824 H to store results where 824 is LSD
RMNDL EQU $824 ; LOCATION OF LEAST SIGNIFICANT DIGIT
RMNDM EQU $823 ; LOCATION OF MIDDLE SIGNIFICANT DIGIT
RMNDH EQU $822 ; LOCATION OF HIGH SIGNIFICANT DIGIT
MYNUM EQU $FD ; FD H = 253 D
MYDEN EQU 10 ; DENOMINATOR IS 10 DECIMAL
ORG $8000 ; START AT 8000 H
LDD #MYNUM ; LOAD NUMERATOR IN REGISTER D
LDX #MYDEN ; LOAD DENOMINATOR IN REGISTER X
IDIV ; 253/10 = 25 R 3
STAB RMNDL ; SAVE REMAINDER ALWAYS LT 10, 3 IN THIS CASE
XGDX ; EXCHANGE D AND X FOR NEW NUMERATOR 25
LDX #MYDEN ; X = 10
IDIV ; 25/10 = 2 R 5
STAB RMNDM ; SAVE NEXT DIGIT, 5 IN THIS CASE
XGDX ; X IS NOW 5 AND D IS 2
STAB RMNDH ; STORE FINAL DIGIT 2
NEG 是2的补码
LDAA #$FF
EORA PORTB ; ALL BITS ARE CHANGED
STAA PORTB ; TO OPPOSITE VALUE
; $FF 1111 1111
; XOR $45 0100 0101 ; ASSUMED VALUE
; 1011 1010
; $BA
; COULD WE DO THIS WITH COM INSTRUCTION?
; Given that PB7 is used to monitor the status of a switch, write a program to toggle PA0 when ever the switch
; goes high. Then further analyze the cases of PB7 = 1 and PB7 = 0
BCLR DDRB, %10000000 ; PB7 IS INPUT
BSET DDRA, %00000001 ; PA0 IS OUTPUT
BACK BRSET PORTB, %10000000 , TOG ; BRANCH TO TOG IF PB7 IS HIGH
BRA BACK ; KEEP CHECKING IT
TOG BCLR PORTA, %00000001 ; PA0 = 0
BSET PORTA, %00000001 ;PA0 = 1
BRA BACK ; CHECK PB7 AGAIN
; CASE 1 PB7 = 1 MASK BYTE 1000 0000
; AND INVERTED PORTB 0000 0000
; RESULT 0000 0000 IT WILL BRANCH
; CASE 2 PB7 = 0 MASK BYTE 1000 0000
; AND INVERTED PORTB 1000 0000
; RESULT 1000 0000 IT WILL NOT BRANCH
; You are given that PB2 is used to monitor the status of a switch. Write a program to toggle PA7 whenever
; PB is low. Further analyze each of the two possible cases.
BCLR DDRB, %00000100 ; PB2 IS INPUT
BSET DDRA, %10000000 ; PA7 IS OUTPUT
BACK BRCLR PORTB, %00000100, TOG ; BRANCH TO TOG IF PB2 = 0
BRA BACK ; KEEP LOOKING AT PB2
TOG BCLR PORTA, %10000000 ; PA7 = 0
BSET PORTA, %10000000 ; PA7 = 1
BRA BACK ; KEEP LOOKING AT PB2
; CASE 1 PB2 = 1 MASK BYTE 00000100
; AND PB2 00000100
; RESULT 00000100 WILL NOT BRANCH
; CASE 2 PB2 = 0 MASK BYTE 00000100
; AND PB2 00000000
; RESULT 00000000 WILL BRANCH
本质是相减比较大小
; Program determines if data on PORTB contains the value 99 H, If so, write ‘Y’ to Port A else write ‘N’ to Porta.
BCLR DDRB, %11111111 ; PORT B IS INPUT
BSET DDRA, %11111111 ; PORT A IS OUTPUT
LDAA #’N’ ; A IS ASCII N
STAA PORTA ; PORT A IS ‘N’
LDAA #$99 ; A IS 99H
BACK COMPA PORTB ; IS PORTB = 99 H?
BNE BACK ; NO KEEP CHECKING
LDAA #’Y’ ; YES WRITE TO PORT A
STAA PORTA ; PORT A IS ‘Y’
; PROGRAM FINDS THE GREATER OF TWO VALUES AND PLACES THE LARGER ONE IN LOCATION $820
VAL1 EQU 54 ; 54 D
VAL2 EQU 27 ; 27 D
BIG EQU $820 ; VARIABLE ASSIGNED TO LOCATION
LDAA #VAL1 ; A = 54 D
CMPA #VAL2 ; COMPARE 54 D TO 27 D
BCS OVER ; JUMP IF VAL1 < VAL2 (C = 1)
STAA BIG ; VAL1 > VAL2, STORE A INTO BIG
; Program to find the number of ones in a given byte
R1 EQU $820 ; LOCATION OF VARIABLE TO STORE NUMBER OF 1’S
COUNT EQU $821 ; LOCATION OF COUNT VARIABLE
VALUE EQU $822 ; LOCATION OF DATA BYTE
CLC ; C = 0
CLRA ; A = 0
STAA R1 ; INITIAL R1 VALUE IS 0
LDAA #8 ; A = 8 D
STAA COUNT ; INTIALYZE COUNTER TO 8 D
LDAA #$97 ; WE WILL FIND THE NUMBER OF 1’S IN 97 H OR 1001 0111 B
STAA VALUE ; DATA BYTE INITIALYZED
AGAIN ROLA ; ROTATE LEFT THRUOU CARRY
BCC NEXT ; IF C = 0 BRANCH TO NEXT
INC R1 ; ELSE INCREMENT THE NUMBER OF 1’S
NEXT DEC COUNT ; DECREMENT COUNTER
BNE AGAIN ; GET NEXT BIT FOR 8 TIMES
; Representation of -128 D
; 1000 0000 128 in unsigned binary
; 0111 1111 1’s compliment
; 0000 0001 Add 1 for 2’s compliment
;1000 0000 Result is 80 H and N = 1
; Note that 128 D unsigned = signed -128 D
16 Bit Signed Numbers
•Either of the following conditions cause V = 1
1. There is a carry from Bit 6 to Bit 7 but no carry from Bit 7
2. There is a carry from Bit 7 but no carry from Bit 6 to Bit 7
•Note in either case we do not have a carry from both
栗子:
Comments about previous example
•Using a loop is most efficient in the general case
•This is only possible because of the indexed addressing mode •Looping is not possible in extended addressing mode
•This is the main difference between extended and indexed modes
•Think about the case where we might want to send a string
•This would much more efficient using indexed mode
•Note the next example
这样都不需要count了,直接用NULL哨兵控制
25 H, 62 H, 3F H, 52 H 的和为 118H
bcd码通常是指8421码就是将十进制的数以8421的形式展开成二进制,大家知道十进制是0~9十个数组成,这十个数每个数都有自己的8421码
bcd码是四位二进制码, 也就是将十进制的数字转化为二进制, 但是和普通的转化有一点不同, 每一个十进制的数字0-9都对应着一个四位的二进制码,对应关系如下: 十进制0 对应 二进制0000 ;十进制1 对应二进制0001 ……. 9 1001 接下来的10就有两个上述的码来表示 10 表示为00010000 也就是BCD码是遇见1001就产生进位,不象普通的二进制码,到1111才产生进位10000
压缩BCD码与非压缩BCD码的区别—— 压缩BCD码的每一位用4位二进制表示,一个字节表示两位十进制数。例如10010110B表示十进制数96D;非压缩BCD码用1个字节表示一位十进制数,高四位总是0000,低4位的0000~1001表示0~9.例如00001000B表示十进制数8.
百度百科http://baike.baidu.com/view/45179.htm?fr=ala0_1_1
BCD码代表数字0~9,ASCII码中也有代表数字0~9的码元,可以查看ASCII表,’0’=48,’1’=49,依次类推。
先把压缩BCD拆开变成非压缩BCD,然后加48就是ASCII中的0~9了!
所以反而类似于“所见即所得”了:
Linking modules in Code Warrior
•This is accomplished by copying the related .asm files into the source folder in code warrior.
•Character data type is most natural type for many applications.
•Unsigned character type is one of the most used in HCS12 programming
•C compilers default to character type unless keyword “unsigned” is used
•Programmers should use unsigned character over other data types whenever possible
•8 bit data type representing signed 7 bit numbers
•MSB contains sign, if 0 then positive, if 1 then negative
•Values -128 to +127 may be represented
•Must be used when we are dealing with any data that may take on a value in the above range
•This the default character data type
•16 bit data type •Range is 0000 – FFFF H
•Typically used to define 16 bit variables such as data addresses •Also useful for counter value > 256
•Use it sparingly
•Use the unsigned variant if negative numbers not expected
•C compiler defaults to signed integer
250 ms delay assuming 2 MHz clock
void MSDelay(unsigned int itime)
{
unsigned int i; unsigned int j;
for (i=0; ifor (j=0; j<331; j++);
}
// concludes example
perform checksum calculation
常用重要标签汇总
1、Load and Store Instructions
2、CCR register
3、分支语句(1)CCR基本跳转
4、DC.B
5、Stack Instructions & Data Transfer Instructions
6、MC9S12Dx256 内存图
7、Auto Increment/Decrement for CLR Instruction
8、分支语句(2)LOOP基本跳转
9、分支语句(3)Long Conditional Branches
10、HCS12 Call Instruction Summary
11、Ports in the MC9S12D Family
12、Some Widely Used Arithmetic Instructions
13、Unsigned Multiplication and Division Summary
课件地址:https://github.com/billhhh/hcs12_dragon12_asm/tree/master/PPT
示例代码地址:
http://www.microdigitaled.com/HCS12/Hardware/Dragon12/CodeWarrior_asm/