Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
15-348 Embedded Systems Lecture 5 January 25th , 2016 Road Map What have we done Gotten familiar with embedded hardware Bit manipulation in C Input and Output using I/O ports What we plan on doing Programming in Assembly Language What you should do Read pages 60-79 from the textbook Registers in 9S12 CCR – Condition Code Register S X H I N Z V C 8-bit condition code S – Stop Bit: Used to enable stop mode for power saving X – Interrupt Mask: I – Interrupt Mask: C – Carry Flag: Set when carry out from D7 V – Overflow Flag: Set for signed overflow Z – Zero Flag: Set when result is zero N – Negative Flag: Set when result is negative H – Half-byte carry Flag: Set when carry from D3 to D4 Registers in 9S12 Two 8-bit accumulators Register A Register B Register D Two 16-bit index registers (X and Y) 16-bit stack pointer SP 16-bit program counter PC Memory Structure of MC9S12C128 0x0000 0x0400 0x0000 – 0x03FF 1K Register Space 0x0400 – 0x1000 ~4K RAM 0x1000 0x4000 0x4000 – 0xFF00 ~128K ROM (EEPROM) 0xFF00 0xFFFF 0xFF00 – 0xFFFF Vector Space Using the RAM RAM 4K Bytes Global Variables Available Memory Stack Pointer Stack Stack grows down Using ROM Fixed Constants Fixed Constants PC Machine Code Vector Table Strings Calibration values ID numbers Finite State Machines Device Address What is assembly language Machine code in human readable format Human readable is of course relatively speaking E.g. DDRT Assembly Language equ $0x0240 ldaa #$0F staa DDRT Real Machine code 860F7A0240 Equivalent Machine code 860F 7A0240 Assembly Format Each field separated by whitespace [Label] Instruction [Operands] [Comments] PORTA equ $0000 Inp ldaa PORTA ; read input Label clra deca ; decrement a bne Label The operand field depends on the operation Assembly Instructions Two main kinds of instructions Operations Assembler Directives DDRT equ $0x0240 Assembler Directive ldaa #$0F Load Operation staa DDRT Store Operation Load instructions ldaa ldab ldd ldx ldy lds load accumulator a load accumulator b load register d load index register x load index register y load Stack Pointer 16 bit move Addressing modes (load what?) ldaa #w Load the value w in a ldaa U Load the value at address U All load instructions set the N and the Z bit in CCR Store instructions staa U stab std stx sty sts All store instructions set the N and the Z bit in CCR Example C code: DDRB = 0x45; PORTB = 0x23 Assembly Code: LDAA STAA LDAB STAB #$45 $03 ; DDRB is address 3 #$23 $01 ; PORTB is address 1 More addressing modes [Y-4] Indexed staa staa -4,Y $40,Y Reg a Address Y- 4 Reg a [Y+$40] Auto inc/dec Indexed staa staa staa 1, Y+ 4, Y+ 4, +Y Reg a[Y] and Y = Y+1 Reg a[Y] and Y = Y+4 Reg a[Y + 4] and Y = Y+4 More addressing modes Accumulator Offset Indexed ldab #4 ldy #2345 staa B,Y ;store contents of a at 2349 Arithmetic Operations 8-bit Add adda #w ; RegA=RegA + w adda U ; RegA = RegA +[U] addb #w ; RegB=RegB + w addb U ; RegB = RegB +[U] Add instructions sets the N, Z, V, and C bits in CCR 16-bit add addd addd Example: ldd addd std #W #U ; RegD = RegD + W ; RegD = RegD + [U] $1234 #1000 $1238 Example C code: byte a = 23; byte b = 12; PORTA = a + b Assembly Code: LDAA #23 ADDA #12 STAA $00 ; PORTA is address 0 Subtract suba cmpa tsta #w #w ;RegA = RegA – w Only ; RegA – w Used to set CCR ; RegA – 0 cpd subd deca incb #W U ; RegD – W ; RegD – [U] ; RegA = RegA -1 ; RegB = RegB +1 Multiply mul ; RegD = RegA * RegB ldaa ldab mul #3 #100 ; RegA = 3 ; RegB = 100 ; RegD = 300 Divide idiv ; RegX = RegD/RegX ; RegD = RegD%RegX ldd #53 ldx #12 idiv ; RegX = 4, RegD = 5 Shift Operations asla asld asrb lsra lsrb rola rora Roll left using C bit from CCR Roll right using C bit from CCR Example C code: int i = 23; PORTA = i >> 2; Assembly Code: LDAD #23 ASRD ASRD STAB $00 ; PORTA is address 0 Branch operations bcc bcs beq bne bmi bpl 8-bit signed bra 16-bit signed jmp target target target target target target target target ;Branch to target if C=0 ;Branch to target if C=1 ;Branch to target if Z=1 ;Branch to target if Z=0 ;Branch to target if N=1 ;Branch to target if N=0 ;Branch to target always ;Branch to target always More branch instructions Following instructions must follow a subtract instruction bge,bgt,ble,blt Example if(G2 == G1) isEqual(); Given two variable G1 and G2, write assembly code for the above code: ldaa suba bne bsr skip: G1 G2 skip isEqual Example Implement the following C code in assembly int main() { int i; byte sum = 12; for(i = 0; i <10; i++) sum = sum + 1; }