ECE/CS 5780/6780: Embedded System Design

Similar documents
Table 1: Mnemonics Operations Dictionary. Add Accumulators Add B to Y. Add with carry to B. Add Memory to B. Add 16-bit to D And B with Memory

Administrivia. ECE/CS 5780/6780: Embedded System Design. Assembly Language Syntax. Assembly Language Development Process

Assembly Language Development Process. ECE/CS 5780/6780: Embedded System Design. Assembly Language Listing. Assembly Language Syntax

Programming the Motorola MC68HC11 Microcontroller

The Motorola 68HC11 Instruc5on Set

Addressing Mode Description Addressing Mode Source Format Abbrev. Description

SECTION 6 CENTRAL PROCESSING UNIT

Chapter 2: HCS12 Assembly Programming. EE383: Introduction to Embedded Systems University of Kentucky. Samir Rawashdeh

Lecture 6 Assembly Programming: Branch & Iteration

ECE331 Handout 3- ASM Instructions, Address Modes and Directives

EE4390 Microprocessors

2) [ 2 marks] Both of the following statements cause the value $0300 to be stored in location $1000, but at different times. Explain the difference.

0b) [2] Can you name 2 people form technical support services (stockroom)?

Department of Computer Science and Engineering

Disassembly of an HC12 Program It is sometimes useful to be able to convert HC12 op codes into mnemonics. For example, consider the hex code:

MC9S12 Assembler Directives A Summary of MC9S12 Instructions Disassembly of MC9S12 op codes. Summary of HCS12 addressing modes ADDRESSING MODES

EE319K Fall 2007 Quiz 1A Page 1. (5) Question 2. What will be the value of the carry (C) bit after executing the following? ldab #210 subb #60

EE319K Fall 2006 Quiz 1 Page 1

EE319K Fall 2003 Quiz 1 Page 1

Lecture 7 Assembly Programming: Shift & Logical

EE319K Fall 2005 Quiz 1A Page 1

EE 3170 Microcontroller Applications

ECET Chapter 2, Part 3 of 3

Chapter 2 HCS12 Assembly Language

Reading Assignment. 68HC12 Instruction Set. M68HC12 Instruction Set Categories. Some Tips. Endianness (Byte Order) Load and Store Instructions

Disassembly of MC9S12 op codes Decimal, Hexadecimal and Binary Numbers

Disassembly of MC9S12 op codes Decimal, Hexadecimal and Binary Numbers

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-470/570: Microprocessor-Based System Design Fall 2014.

(5) Question 7. Simplified memory cycles (you may or may not need all 5 entries) R/W Addr Data

Menu. Programming Models for the Atmel XMEGA Architecture (and others devices) Assembly Programming Addressing Modes for the XMEGA Instruction Set

instruction 1 Fri Oct 13 13:05:

EE319K Fall 2010 Exam 1B Page 1

COE538 Lecture Notes Week 3 (Week of Sept 17, 2012)

538 Lecture Notes Week 3

EE319K Spring 2010 Exam 1A Page 1

2. Arithmetic Instructions addition, subtraction, multiplication, divison (HCS12 Core Users Guide, Sections 4.3.4, and ).

538 Lecture Notes Week 3

Fri. Aug 25 Announcements

HC11 Instruction Set

EE 308 Spring The HCS12 has 6 addressing modes

COSC 243. Instruction Sets And Addressing Modes. Lecture 7&8 Instruction Sets and Addressing Modes. COSC 243 (Computer Architecture)

Using the stack and the stack pointer

Exam I Review February 2017

C SC 230 Computer Architecture and Assembly Language April 2000 Exam Sample Solutions

MIGRATING TO THE 68HC12 IN C

Most of the HC12 s instructions access data in memory There are several ways for the HC12 to determine which address to access

EE 3170 Microcontroller Applications

68HC11 PROGRAMMER'S MODEL

Lecture 9 Subroutines

Timing Generation and Measurements

EE319 K Lecture 7. Address mode review Assembler, Debugging Psuedo ops 16 bit timer finite state machines. University of Texas ECE

538 Lecture Notes Week 2

ME4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics. Instructor: Professor Charles Ume LECTURE 7

The 6502 Instruction Set

Sample Problem Set #1

Introduction to Embedded Systems. Some Nagging

EE319 K Lecture 3. Introduction to the 9S12 Lab 1 Discussion Using the TExaS simulator. University of Texas ECE

CS 273 Machine Programming and Organization Lecture Notes

Cross Assembly and Program Development

EE319K Final Fall 2005 Solution C. (3) Question 1. (3) Question 2. short function(const short in){ return in+5; } const

Mark II Aiken Relay Calculator

ECE 3120 Computer Systems Arithmetic Programming

Introduction to Embedded Microcomputer Systems Lecture 8.1. Computers in the future may weigh no more than 1.5 tons Popular Science, 1949

Addition and Subtraction of Hexadecimal Numbers Simple assembly language programming

Condition Code Register. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

N bit is set if result of operation in negative (MSB = 1) Z bit is set if result of operation is zero (All bits = 0)

ME 6405 Introduction to Mechatronics

Outline. 2.8 Stack. 2.9 Subroutines

Exam 1 Feb. 23, 25, 27?

AN1064. Motorola Semiconductor Application Note. Use of Stack Simplifies M68HC11 Programming By Gordon Doughman. Introduction

m 1 se 7 m 23 Introduction to Embedded Microcomputer Systems Lecture 16.1 Recap Finite State Machines Pointer implementation

Motorola HC11. Fun with Microcontrollers and Embedded Systems

Introduction to Programming the 9S12 in C Huang Sections 5.2 and 5.3. You will be able to use all of the Motorola data manuals on the exam.

Exam 2 E2-1 Fall Name: Exam 2

Decimal, Hexadecimal and Binary Numbers Writing an assembly language program

Module 1-G. Marcos and Structured Programming

Motorola 6809 and Hitachi 6309 Programmer s Reference

Accumulator and memory instructions 1. Loads, stores, and transfers 2. Arithmetic operations 3. Multiply and divide 4. Logical operations 5. Data test

COSC345 Software Engineering. Basic Computer Architecture and The Stack

Addition and Subtraction of Hexadecimal Numbers Simple assembly language programming

Exam 2 E2-1 Fall Name: Exam 2

EE319K Fall 2006 Final A Page 1

CPU08RM/AD REV 3 8M68HC08M. CPU08 Central Processor Unit. Reference Manual

Coe538 Final Study Guide 2016 (Questions & Answers)

EE319K Fall 2007 Quiz 3 Page 1 of 8

Ryerson University Department of Electrical and Computer Engineering ELE 538 Microprocessor Systems Final Examination December 8, 2003

HC 11 Instructions! From Alex Hollowayʼs notes with! many thanks!

UNIVERSITY OF MANITOBA DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING. Term Test #2 Solution ECE 3610 MICROPROCESSING SYSTEMS

HC11 Instruction Set Architecture

HC11 Instruction Set Architecture

EE 3170 Microcontroller Applications

A Simple MC9S12 Program

Introduction to Microcontroller. Systems. Embedded system. Assembler or C? Datatypes 2. Datatypes 1

Introduction to Programming the 9S12 in C Huang Sections 5.2 and 5.3

Lecture #3 Microcontroller Instruction Set Embedded System Engineering Philip Koopman Wednesday, 20-Jan-2015

ECE3120: Computer Systems Hardware & Software Development Tools

History of the Microprocessor. ECE/CS 5780/6780: Embedded System Design. Microcontrollers. First Microprocessors. MC9S12C32 Block Diagram

Transporting M68HC11 Code to M68HC12 Devices

ECE 372 Microcontroller Design Assembly Programming. ECE 372 Microcontroller Design Assembly Programming

CHAPTER 8. Solutions for Exercises

Transcription:

ECE/CS 5780/6780: Embedded System Design Scott R. Little Lecture 3: Assembly Language Programming Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 1 / 59 Administrivia 2 versions of CodeWarrior are on the lab machines. You should use the 4.5 version (CW for HC12 v 4.5). Lab 1 was updated yesterday morning. Both team members need to be present for check-off. Lab reports don t need to be in report format. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 2 / 59

Assembly Language Development Process Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 3 / 59 Assembly Language Syntax Label Operation Operand Comment PORTA equ $0000 ; Assembly time constant Inp ldaa PORTA ; Read data from PORTA If first character of label is or ;, then line is a comment. If first character is white space, then there is no label. Labels composed of characters, digits,., $, or, and must start with a character,., or, and are case-sensitive. Labels should be defined only once except those defined by set. With exception of equ and set, a label is assigned value of program counter for the next instruction or assembler directive. A label may have an optional : which is ignored or be on a line by itself. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 4 / 59

Assembly Language Syntax (cont) Operations must be proceeded by at least one white space character, and they are case-insensitive (nop, NOP, NoP). Operations can be an opcode or assembler directive (pseudo-op). Operand must be proceeded by white space. Operands must not contain any white space unless the following comment begins with a semicolon. Operands are composed of symbols or expressions. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 5 / 59 Operand Types Operand Format Example no operand INH clra #<expression> IMM ldaa #4 <expression> DIR,EXT,REL ldaa 4 <expression>,idx indexed (IND) ldaa 4,x <expr>,#<expr> bit set or clear bset 4,#$01 <expr>,#<expr>,< expr > bit test & branch brset 4,#$01,foo <expr>,idx,#<expr>,< expr > bit test & branch brset 4,x,#$01,foo <expression>,idx+ IND, post incr ldaa 4,x+ <expression>,idx- IND, post decr ldaa 4,x- <expression>,+idx IND, pre incr ldaa 4,+x <expression>,-idx IND, pre decr ldaa 4,-x acc,idx accum offset IND ldaa A,x [<expression>,idx] IND indirect ldaa [4,x] [D,idx] RegD IND indirect ldaa [D,x] Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 6 / 59

Indexed Addressing Mode Uses a fixed signed offset with a 16-bit register: X, Y, SP, or PC. Offset can be 5-bits, 9-bits, or 16-bits. Example (5-bit): Obj code Op Operand Comment $6A5C staa -4,Y ;[Y-4] = RegA Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 7 / 59 Building the Object Code staa -4,Y $6A5C First byte is $6A - Op code (pg. 254) Second byte is formatted as %rr0nnnnn (pg. 33). rr is %01 for register Y. nnnnn is %11100 for -4. %0101 1100 $5C. Information is found in the CPU12 Reference Manual (CPU12RM.pdf). Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 8 / 59

Auto Pre/Post Decrement/Increment Indexed Can be used with the X, Y, and SP registers, but not PC. The register used is incremented/decremented by the offset value (1 to 8) either before (pre) or after (post) the memory access. In these examples assume that RegY=2345: Op Operand Comment staa 1,Y+ ;Store RegA at 2345, then RegY=2346 staa 4,Y- ;Store RegA at 2345, then RegY=2341 staa 4,+Y ;RegY=2349, then store RegA at 2349 staa 1,-Y ;RegY=2344, then store RegA at 2344 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 9 / 59 Building the Object Code staa 1,X+ $6A30 First byte is $6A - Op code (pg. 254) Second byte is formatted as %rr1pnnnn (pg. 33). rr is %00 for register X. nnnn is %0000 for 1. p is %1 for post. %0011 0000 $30. Information is found in the CPU12 Reference Manual (CPU12RM.pdf). Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 10 / 59

Accumulator Offset Indexed Uses two registers, offset is in A, B, or D, while index is in X, Y, SP, or PC. Examples: Op Operand Comment ldab #4 ldy #2345 staa B,Y ;Store value in RegA at 2349 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 11 / 59 Indexed Indirect Adds 16-bit offset to 16-bit register (X,Y,SP, or PC) to compute address in which to fetch another address. This second address is used by the load or store. Examples: Op Operand Comment ldy #$2345 staa [-4,Y] ;Fetch 16-bit address from $2341, ;store $56 at $1234 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 12 / 59

Accumulator D Offset Indexed Indirect Offset is in D and index is in another 16-bit register. Computed addressed is used to fetch another address from memory. Load or store uses the second address. Examples: Op Operand Comment ldd #4 ldy #$2341 stx [D,Y] ;Store value in RegX at $1234 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 13 / 59 Load Effective Address Used with IND addressing modes. Calculate the effective address and store it in the specified register: X, Y, or SP. CC bits are not affected. Example: leas -4,SP ;SP -= 4 $1B9C $1B is leas op code (first byte). Second byte is %rr0nnnnn. %10 is the rr code for SP. %1 1100 is -4. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 14 / 59

Load and Store Instructions Used to move data to (from) registers from (to) memory. Load instructions are: ldaa, ldab, ldd, lds, ldx, and ldy. Load addressing modes are: IMM, DIR, EXT, IND. Store instructions are: staa, stab, std, sds, sdx, and sdy. Store addressing modes are: DIR, EXT, IND. CC bits N and Z are updated based on data loaded or stored. Examples: Op Operand Comment ldaa #$FF IMM staa $25 DIR ldab $0025 EXT std $05,X IND ldd $C025 EXT Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 15 / 59 Memory to Memory Move Instructions Used to move constant into memory or the value of one memory location into another. CC bits are not affected by these instructions. Move an 8-bit constant into memory: movb #w,addr [addr]=w Move an 8-bit value memory to memory: movb addr1,addr2 [addr2]=[addr1] Move a 16-bit constant into memory: movw #W,addr {addr}=w Move a 16-bit value memory to memory: movw addr1,addr2 {addr2}={addr1} Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 16 / 59

Clear/Set Instructions Used to initialize memory (clr), accumulators (clra,clrb), or bits in the CC (clc, cli, clv). clr addressing modes are: EXT, IND. clra, clrb, clc, cli, clv are INH. Examples: Op Operand Comment clra INH clr $0025 EXT The carry (C), interrupt mask (I), and overflow (V) bits in the CC can also be set (sec, sei, sev). Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 17 / 59 Other Data Movement Instructions Transfer instructions used to copy data between registers. tab, tap, tba, tpa, tsx, tsy, txs, tys (all INH). Exchange instructions used to exchange data between accumulator D and index registers X and Y. xgdx, xgdy (all INH). Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 18 / 59

Add and Subtract Instructions Registers: aba, abx, aby, sba (all INH). With carry to memory: adca, adcb, sbca, sbcb. w/o carry to memory: adda, addb, addd, suba, subb, subd. Addressing modes are: IMM, DIR, EXT, IND. Examples: 16-bit addition using only A Op Operand Comment ldaa $25 load least sig byte adda $35 add data at $35 to A staa $45 store least sig byte ldaa $24 load most sig byte adca $34 add data at $34 to A staa $44 store most sig byte Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 19 / 59 Compare Instructions Perform a subtraction to update the CC, but do not alter data register values. Typically used just before a branch instruction. Compare registers: cba (INH). Compare to memory: cmpa, cmpb, cpd, cpx, cpy. Addressing modes: IMM, DIR, EXT, IND Example: comparing with a known set point Obj code Op Operand Comment $8650 ldaa #$50 load set point into A $B11031 cmpa $1031 compare A to memory If Z flag is 1 then the contents of $1031 equals $50. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 20 / 59

Miscellaneous Arithmetic Instructions dec, deca, decb, des, dex, dey - decrement inc, inca, incb, ins, inx, iny - increment neg, nega, negb - two s complement. tst, tsta, tstb - subtracts 0 from memory or register and sets Z and N flags in the CC. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 21 / 59 Multiply Instructions Multiplies two unsigned 8-bit values in A and B to produce a 16-bit unsigned product stored in D (i.e., A B D). Example: Op Operand Comment ldaa #$FF (255) IMM ldab #$14 (20) IMM mul INH At the end, accumulator D contains $13EC (5100). $FF * $FF = $FE01 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 22 / 59

Integer Divide Instruction Use D register for the dividend and X register for the divisor. Resultant placed in X register and remainder in D register. idiv performs integer division. Example: Obj code Op Operand Comment $CCFFFF ldd #$FFFF (65535) After idiv executes $CE2710 ldx #$2710 (10000) X contains $0006 $02 idiv D contains $159F (5535) Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 23 / 59 Fractional Divide Instruction fdiv performs fractional division resulting in binary weighted fraction between 0 and 0.999998 (i.e., (65536 D)/X X ). Numerator must be less than denominator or overflow occurs. Next 16-bits of the fraction can be obtained by reloading the denominator and doing fdiv again. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 24 / 59

fdiv Example Op Operand ldd #1 ldx #3 fdiv Result: X: $5555 and D: $0001. Assuming decimal point is to the left of the MSB $5555 = 0.333328247... Another fdiv refines the value to: 0.33333333325572 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 25 / 59 Extended Precision Arithmetic Instruction emul and emuls perform 16 16 unsigned and signed multiplication. ediv and edivs perform 32 16 unsigned and signed division. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 26 / 59

Example: M = (53 N + 50)/100 ldd N ;RegA=N (between 0 and 65535) ldy #53 emul ;RegY:D=53*N (between 0 and 3473355) addd #50 ;RegD=53*N+50 (between 0 and 3473355) bcc skip iny skip ldx #100 ediv ;RegY=(53*N+50)/100 (between 0 and 34734) sty M Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 27 / 59 Multiply and Accumulate emacs performs a 16 16 signed multiply followed by a 32-bit signed addition. Uses indexed addressing to access two 16-bit inputs and extended addressing to access the 32-bit sum. < U > = < U > +{X } {Y } Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 28 / 59

Shift Instructions Logical shift right (lsr, lsra, lsrb, lsrd) shifts 0 s into MSB. Arithmetic shift right (asr, asra, asrb) retains MSB value. Logical shift left (lsl, lsla, lslb, lsld) and arithmetic shift left (asl, asla, aslb, asld) are equivalent (same op code). Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 29 / 59 Rotate Instructions Rotate right (ror, rora, rorb) put carry bit into the MSB. Rotate left (rol, rola, rolb) put carry bit into the LSB. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 30 / 59

Logical Operation Instructions AND - anda, andb (IMM, DIR, EXT, IND). Inclusive OR - oraa, orab (IMM, DIR, EXT, IND). Exclusive OR - eora, eorb (IMM, DIR, EXT, IND). 1 s complement - com, coma, comb. Example: masking unwanted bits Obj code Op Operand Comment $8634 ldaa #$34 %00110100 $840F anda #$0F %00001111 Result in A is %00000100 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 31 / 59 Data Test and Bit Manipulation bita and bitb instructions perform an AND operation and update N and Z flags of the CC w/o altering the operand. bclr and bset instructions are used to clear or set bit(s) in a given memory location. bclr addr, mm bset addr, mm where addr is a memory location specified using DIR, EXT, or IND addressing mode and mm is a mask byte. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 32 / 59

Stack Instructions Stack pointer (RegSP) defines the top of the stack. Should be loaded with a RAM memory address early in any assembly language program. Push and pull instructions put data onto and take data off the stack. psha, pshb, pshx, pshy, pula, pulb, pulx, puly (all INH). Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 33 / 59 Example: Saving State to Stack Op pshy pshx pshb psha tpa psha pula tap pula pulb pulx puly Comment INH INH INH INH INH INH body of subroutine INH INH INH INH INH INH Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 34 / 59

Subroutine Calls and Return bsr - branch to subroutine using REL addressing. jsr - jumps to subroutine using DIR, EXT, or IND addressing. On either bsr or jsr, PC is automatically pushed onto the stack (least significant byte first). rts - return from subroutine, PC automatically pulled off the stack and jumps to that location. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 35 / 59 Example Using bsr and rts org $C000 main lds #$0900 ($CF0900) clra ($87) loop bsr Add1 ($0702) bra loop ($20FC) ;*******Add1********** ;Purpose: Subtract one from RegA ; Input: RegA ;Output: RegA=Input+1 Add1 inca ($42) rts ($3D) org $FFFE fdb main Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 36 / 59

bsr and rts Execution org $C000 main: lds #$0900 clra loop: bsr Add1 bra loop Add1: inca rts $C000 $CF09 $C002 $0087 $C004 $0702 $C006 $20FC $C008 $423D...... $08FE $XXXX $0900 $XXXX SP $0900 PC $C000 A $XX Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 37 / 59 bsr and rts Execution org $C000 main: lds #$0900 clra loop: bsr Add1 bra loop Add1: inca rts $C000 $CF09 $C002 $0087 $C004 $0702 $C006 $20FC $C008 $423D...... $08FE $XXXX $0900 $XXXX SP $0900 PC $C003 A $00 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 38 / 59

bsr and rts Execution org $C000 main: lds #$0900 clra loop: bsr Add1 bra loop Add1: inca rts $C000 $CF09 $C002 $0087 $C004 $0702 $C006 $20FC $C008 $423D...... $08FE $C006 $0900 $XXXX SP $08FE PC $C004 A $00 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 39 / 59 bsr and rts Execution org $C000 main: lds #$0900 clra loop: bsr Add1 bra loop Add1: inca rts $C000 $CF09 $C002 $0087 $C004 $0702 $C006 $20FC $C008 $423D...... $08FE $C006 $0900 $XXXX SP $08FE PC $C008 A $01 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 40 / 59

bsr and rts Execution org $C000 main: lds #$0900 clra loop: bsr Add1 bra loop Add1: inca rts $C000 $CF09 $C002 $0087 $C004 $0702 $C006 $20FC $C008 $423D...... $08FE $C006 $0900 $XXXX SP $0900 PC $C009 A $01 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 41 / 59 bsr and rts Execution org $C000 main: lds #$0900 clra loop: bsr Add1 bra loop Add1: inca rts $C000 $CF09 $C002 $0087 $C004 $0702 $C006 $20FC $C008 $423D...... $08FE $C006 $0900 $XXXX SP $0900 PC $C006 A $01 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 42 / 59

Jump and Branch Always jmp and bra instructions are unconditional. bra uses relative addressing (REL) so it can only be used to jump 128 or 127 instructions. jmp can use EXT and IND addressing so it can be used to jump anywhere in the 64K address space. bra $ stops progress of CPU, but it continues to execute this instruction. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 43 / 59 Single Condition Branches bcc - branch if carry clear (i.e., C = 0). bcs - branch if carry set (i.e., C = 1). bne - branch if not equal to zero (i.e., Z = 0). beq - branch if equal to zero (i.e., Z = 1). bpl - branch if positive or zero (i.e., N = 0). bmi - branch if negative (i.e., N = 1). bvc - branch if overflow clear (i.e., V = 0). bvs - branch if overflow set (i.e., V = 1). brn - branch never Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 44 / 59

Example: Tests for Equality C Code Assembly Code ===================================================== if (G2==G1) { ldaa G2 isequal(); cmpa G1 } bne next ;skip if not equal jsr isequal ;G2==G1 next ===================================================== if (G2!=G1) { ldaa G2 isnotequal(); cmpa G1 } beq next ;skip if equal jsr isnotequal ;G2!=G1 next Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 45 / 59 Unsigned Number Branches These branches usually follow cba, cmp(a,b,d), cp(x,y), sba, sub(a,b,d) instructions. bhi - branch if higher > (i.e., C + Z = 0). bhs - branch if higher or same (i.e., C = 0). blo - branch if lower < (i.e., C = 1). bls - branch if lower or same (i.e., C + Z = 1). Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 46 / 59

Signed Number Branches These branches usually follow cba, cmp(a,b,d), cp(x,y), sba, sub(a,b,d) instructions. bgt - branch if greater > (i.e., Z (N V ) = 0). bge - branch if greater or equal (i.e., N V = 0). blt - branch if less < (i.e., N V = 1). ble - branch if less or equal (i.e., Z (N V ) = 1). Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 47 / 59 Example: Unsigned Tests C Code Assembly Code ===================================================== unsigned int G1; ldaa G2 unsigned int G2; cmpa G1 if (G2 > G1) { bls next ;skip if G2<=G1 isgreater(); jsr isgreater ;G2>G1 } next ===================================================== unsigned int G1; ldaa G2 unsigned int G2; cmpa G1 if (G2 > G1) { blo next ;skip if G2<G1 isgreatereq(); jsr isgreatereq ;G2>=G1 } next Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 48 / 59

Bit Masking Branches brset - performs logical AND of memory address and mask provided and branches only when all bits in the mask are set. brclr - performs logical AND of memory address and mask provided and branches only when all bits in the mask are clear. Op Operand Comment brset 5,$10,$F889 goto $F889 if bit 4 of loc 5 is 1 brclr 5,$10,$F889 goto $F889 if bit 4 of loc 5 is 0 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 49 / 59 Long Branches and Delays Use branch and jump to branch outside of range: Address Op Operand Comment C3B0 bcs $40 C3B2 Next instruction... C3F2 jmp PI PI may be any valid address. Branch loops can be used to insert a time delay: Label Op Operand Comment ldab #$Count Load B with count Delay decb decrement B bne Delay If B 0 goto Delay DelayTime = t(ldab) + (t(decb) + t(bne)) count Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 50 / 59

Interrupt Handling wai instruction puts CPU into standby mode waiting for an external interrupt. swi instruction executes a software interrupt. rti instruction is called at the end of an interrupt service routine to restore the CPU registers. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 51 / 59 Miscellaneous nop - no operation, creates a 2-cycle delay. stop - if S flag in CCR is 0 then stop clocks to save power, recover after RESET, XIRQ, or unmasked IRQ. Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 52 / 59

Assembler Pseudo-ops Set the location to put the following object code (org,.org): org <expression> Equate symbol to a value (equ, =): <label> equ <expression> Redefinable equate symbol to a value (set): <label> set <expression> Reserve multiple bytes (rmb, ds, ds.b,.blkb): <label> rmb <expression> Reserve multiple words (ds.w,.blkw): <label> ds.w <expression> Reserve multiple 32-bit words (ds.l,.blkl): <label> ds.l <expression> Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 53 / 59 Using equ for Constants org $3800 size equ 5 data rmb size org $4000 sum ldaa #size ldx #data clrb loop addb 1,x+ dbne A,loop rts Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 54 / 59

Assembler Pseudo-ops (cont) Form constant byte (fcb, dc.b, db,.byte): <label> fcb <expression> Form double byte (fdb, dc.w, dw,.word): <label> fdb <expression> Define 32-bit constant (dc.l, dl,.long): <label> fdb <expression> Form constant character string (fcc): hello fcc Hello World,0 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 55 / 59 A Stepper Motor Controller size equ 4 PTT equ $0240 DDRT equ $0242 org $4000 main movb #$FF,DDRT ;PT3-0 outputs run ldaa #size ldx #steps step movb 1,x+,PTT ;step motor dbne A,step bra run steps fcb 5,6,10,9 ;out sequence org $FFFE fdb main Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 56 / 59

Memory Allocation in Intel x86 Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 57 / 59 Memory Allocation in Embedded System Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 58 / 59

Memory Allocation in Software org $3800 ;RAM cnt rmb 1 ;global org $4000 ;EEPROM const fcb 5 ;amount to add init movb #$FF,DDRT ;outputs clr cnt rts main lds #$4000 ;sp=>ram bsr init loop ldaa cnt staa PTT ;output adda const staa cnt bra loop org $FFFE ;EEPROM fdb main ;reset vector Scott R. Little (Lecture 3: Assembly) ECE/CS 5780/6780 59 / 59