Assembler Programming Chapter 6. EEL-4746 Best Practices.

Slides:



Advertisements
Similar presentations
Chapter 3 Introduction to the 68000
Advertisements

Branches Two branch instructions:
EET 2261 Unit 3 Assembly Language; Load, Store, & Move Instructions  Read Almy, Chapters 5 and 6.  Homework #3 and Lab #3 due next week.  Quiz next.
EET 2261 Unit 5 Tables; Decision Trees & Logic Instructions
Revised: Aug 1, EE4390 Microprocessors Lesson 6,7 Instruction Set, Branch Instructions, Assembler Directives.
68HC11 Polling and Interrupts
H. Huang Transparency No.1-1 The 68HC11 Microcontroller Chapter 1: Introduction to 68HC11 The 68HC11 Microcontroller.
Programming the HC12 in C. Some Key Differences – Note that in C, the starting location of the program is defined when you compile the program, not in.
1 ECE 372 – Microcontroller Design Basic Assembly Programming for(j=0; j
© 2010 Kettering University, All rights reserved..
The M68HC11 Basic Instruction Set Basic Arithmetic Instructions
H. Huang Transparency No.2-1 The 68HC11 Microcontroller Chapter 2: 68HC11 Assembly Programming The 68HC11 Microcontroller.
Lab 1 – Assembly Language and Interfacing Start date: Week 3 Due date: Week 4 1.
Basic Microcontroller System
Assembly Language Programming for the MC68HC11. Assembly language programming  Recall the 4 design levels for software development: – Application – High.
ME4447/6405 The George W. Woodruff School of Mechanical Engineering ME4447/6405 Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
Stacks and Subroutines. Some example stacks Stacks and subroutine usage The stack is a special area of the random access memory in the overall memory.
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero.
Making Decision – Microprocessor
ECE 447: Lecture 12 Logic, Arithmetic, Data Test and Control Instructions of MC68HC11.
6-1 EE 319K Introduction to Microcontrollers Lecture 6: Indexed Addressing Mode and Variants, Functional Debugging, Arrays, Strings.
ME4447/6405 The George W. Woodruff School of Mechanical Engineering ME4447/6405 Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics.
9/20/6Lecture 3 - Instruction Set - Al1 Program Design Examples.
1 Segments and Pseudo Operations Program Development.
Assembly Language ELEC 330 Digital Systems Engineering Dr. Ron Hayne.
ECE 265 – LECTURE 5 The M68HC11 Basic Instruction Set 12/8/ ECE265.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
1 Stacks, Subroutines, I/O Routines Today: First Hour: Stacks, Subroutines –Section 3.9,3.10 of Huang’s Textbook –In-class Activity #1 Second Hour: I/O.
ECE 265 – LECTURE 11 Editing and the Assembler (updated 11/11/10) 12/15/ ECE265.
1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Addressing Modes Stack Operations Subroutines.
Ch.2 Intro. To Assembly Language Programming
ECE Lecture 21 Typical Assembly Language Program Bugs.
Ch.2 Intro. To Assembly Language Programming From Introduction to Embedded Systems: Interfacing to the Freescale 9s12 by Valvano, published by CENGAGE.
1 Microcontroller Fundamentals & Programming Addressing Modes.
Microcontroller Fundamentals & Programming Arithmetic Instructions.
ECE 447: Lecture 16 Common Errors & Good Programming Style.
Advanced Assembly Language Programming
Embedded Systems Lecture 5 January 25 th, 2016.
Physics 413 Chapter 4: Advanced Assembly Programming.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
1 Subroutines Advanced Programming. 2 Top-Down approach to problem solving Algorithm step by step description of how to solve a problem Divide and Conquer.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
1 ECE 372 – Microcontroller Design Basic Assembly Programming for(j=0; j
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
EE345 Chapter 2 Lecture 3 April Instruction and addressing modes 1.Extended Addressing 2.Direct Addressing 3.Inherent Addressing 4.Immediate Addressing.
Stacks and Subroutines May 23. Stacks and subroutine usage The stack is a special area of the random access memory in the overall memory system The stack.
5-1 EE 319K Introduction to Microcontrollers Lecture 5: Conditionals, Loops, Modular Programming, Sub- routines, Parameter passing.
The 68HC11 Microcontroller Minnesota State University, Mankato
EET 2261 Unit 3 Assembly Language; Load, Store, & Move Instructions
Addressing Modes in Microprocessors
ECE 3430 – Intro to Microcomputer Systems
Learning Outcome #1 Architecture and Programming Model
HC11 Programming.
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
Wed. Sept 6 Announcements
“Assembly Table Lookup”
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ME4447/6405 Microprocessor Control of Manufacturing Systems and
Microprocessor and Assembly Language
Branching and Loops.
Write a program to calculate x**y, given x and y.
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
EET 2261 Unit 6 The Stack; Subroutines
Generalities for Assembly Language
Accum A: Index X: CCR Bit Z: $0100 $0101 $0102 $0103 $0104 $0105
ECE511: Digital System & Microprocessor
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

Assembler Programming Chapter 6

EEL-4746 Best Practices

1.All programs must begin with the following comment *************************** *************************** * EEL-4746 Spring 2004 Semester * EEL-4746 Spring 2004 Semester * Homework #N – Due (Due Date) * Homework #N – Due (Due Date) * Problem #M * Problem #M * Name of Partner A * Name of Partner A * Name of Partner B * Name of Partner B **************************** **************************** * Description of Program * Description of Program

EEL-4746 Best Practices 2.All subroutines must begin with the following comment *************************** *************************** * Subroutine Name: Mysub * Subroutine Name: Mysub * Input parameter list: * Input parameter list: * Output parameter list: * Output parameter list: * Registers changed list: * Registers changed list: **************************** **************************** * Description of Subroutine * Description of Subroutine

EEL-4746 Best Practices 3.All lines must contain a comment 4.All labels must end with a semicolon 5.Must use a symbol for all constants

EEL-4746 Best Practices 5.Program must have the following format: Header Comment Header Comment *********************** *********************** * Standard Symbols * Standard Symbols Data EQU $0000 Data EQU $0000 Program EQU $E000 Program EQU $E000 Stack EQU $00FF Stack EQU $00FF Reset EQU $FFFE Reset EQU $FFFE ******************* ******************* Place your symbols here Place your symbols here ****************** ****************** * Program * Program Top: ORG Program Top: ORG Program Label: Program line ; comment Label: Program line ; comment ************************** ************************** * Data * Data ORG Data Variable {Directive} ; Data goes here Variable {Directive} ; Data goes here ORG Reset FDB Top

Additional Comments To Indent or Not Indent Not really used in assembly language programming Not really used in assembly language programming Upper,Lower, and Mixed Cases Be consistent Be consistent Symbols for constants in ALLUPPERCASE Labels for in-memory variables in lowercase Code labels in MixedCase Instructions all uppercase or all lowercase Etc Subroutine Headers Develop the habit of placing comments before subroutines. The comment should give the function of the subroutine, the input values it expects, and the registers that it modifies. Develop the habit of placing comments before subroutines. The comment should give the function of the subroutine, the input values it expects, and the registers that it modifies.

Pseudo-code A “fictitious” programming language which allows a general algorithm for a computer program to be developed. Design process 1. Develop program using pseudo-code 2. Convert pseudo-code to 68HC11 assembly language

Structured Assembly Language Programming If – Then –Else – End IF Pseudo-Code Syntax If Variable_A Condition Variable_B Then Pseudo Code for Then condition Else (Optional) Pseudo Code B for Else condition End If

Structured Assembly Language Programming LDAA Variable_A LDAA Variable_A CMPA Variable_B CMPA Variable_B B?? Else ; Opposite of condition B?? Else ; Opposite of condition Code for Then part Code for Then part …………………………. …………………………. BRA END_IF ; need to skip else part BRA END_IF ; need to skip else part Else : Code for Else part ……………………….. ……………………….. END_IF: Rest of Code

Structured Assembly Language Programming If-Then-Else-End IF Condition : Branch to Use (Opposite of condition) <> : BEQ (Branch if equal) <> : BEQ (Branch if equal) = : BNE (Branch if not equal) = : BNE (Branch if not equal) Signed Signed <= : BGT (Branch if greater than) < : BGE (Branch if greater than or equal) >= : BLT (Branch if less than) > : BLE (Branch if less than or equal) Unsigned Unsigned <= : BHI (Branch if higher) < : BHS (Branch if higher or same) < : BHS (Branch if higher or same) >= : BLO (Branch if lower) > : BLS (Branch if lower or same)

If-Then-Else Example Pseudo-code IF temp > max_temp then IF temp > max_temp then Then code Then code Else Else Else code Else code End If End If

If-Then-Else Example Assembly Language LDAA Temp LDAA Temp CMPA MAX_TEMP CMPA MAX_TEMP BLS Else BLS Else Then code here Then code here BRA ENDIF BRA ENDIF ELSE: ELSE: ELSE code here ELSE code here EndIf: EndIf: Rest of program here Rest of program here

Structured Assembly Language Programming Loops For Loops For Loops While-Do Loop While-Do Loop Do-While or (Repeat – Until) Loops Do-While or (Repeat – Until) Loops

Structured Assembly Language Programming For Loops Pseudo-code Syntax Pseudo-code Syntax For loop_index = start_index to end_index Begin Begin Code to Execute ……..…….. End For Loop End For Loop

Structured Assembly Language Programming Assembly Language Use one of the registers for the loop index. Use one of the registers for the loop index. A,B=8 bit (255 max) X,Y=16 bit (65535 max) Must use memory if index value is greater than Example Code Fragment Example Code Fragment LDAA #start_index LDAA #start_index Loop: Start of Code to Execute (must not change A) …………. …………. INCA INCA CMPA #end_index CMPA #end_index BLS Loop BLS Loop

Structured Assembly Language Programming What if you need the loop index register (e.g. A) in your code? Example Code Fragment LDAA #start_index LDAA #start_index Loop: Start of Code to Execute PSHA ; Save A on stack PSHA ; Save A on stack Use A Use A …………. …………. PULA ; Restore A from stack PULA ; Restore A from stack INCA INCA CMPA #end_index CMPA #end_index BLS Loop BLS Loop

Structured Assembly Language Programming For Loops (Count down) Pseudo-code Syntax Pseudo-code Syntax For loop_index = end_index downto start_index Begin Begin Code to Execute ……..…….. End For Loop End For Loop

Structured Assembly Language Programming Example Code Fragment LDAA #end_index LDAA #end_index Loop: Start of Code to Execute ………………………. ………………………. DECA DECA CMPA #start_index CMPA #start_index BHS Loop BHS Loop

Structured Assembly Language Programming For Loops (Count down from N to 1) This counts N items Pseudo-code Syntax Pseudo-code Syntax For loop_index = end_index downto 1 Begin Begin Code to Execute ……..…….. End For Loop End For Loop

Structured Assembly Language Programming Example Code Fragment LDAA #N LDAA #N Loop: Start of Code to Execute ………………………. ………………………. DECA DECA BNE Loop ; Save one statement BNE Loop ; Save one statement

TPS Quiz

Structured Assembly Language Programming While-Do Loops Pseudo-code Syntax Pseudo-code Syntax While Variable Condition Constant_VALUE Do Code Here Begin Begin Code to Execute ……..…….. End While End While

Structured Assembly Language Programming While-Do Loops Loop: LDAA Variable Loop: LDAA Variable CMPA #Constant_Value CMPA #Constant_Value B?? End_While ; (opposite of condition) B?? End_While ; (opposite of condition) Do code here Do code here ……………… ……………… …………….. …………….. BRA Loop BRA Loop End_While: ; End of While loop End_While: ; End of While loop

Structured Assembly Language Programming While-Do Condition : Branch to Use (Opposite of condition) <> : BEQ (Branch if equal) <> : BEQ (Branch if equal) = : BNE (Branch if not equal) = : BNE (Branch if not equal) Signed Signed <= : BGT (Branch if greater than) < : BGE (Branch if greater than or equal) >= : BLT (Branch if less than) > : BLE (Branch if less than or equal) Unsigned Unsigned <= : BHI (Branch if higher) < : BHS (Branch if higher or same) < : BHS (Branch if higher or same) >= : BLO (Branch if lower) > : BLS (Branch if lower or same)

Structured Assembly Language Programming Example: Temp > #$4F (unsigned) Pseudo-code: While Temp > $4F Do (Execute your code here) (Execute your code here) End_While: ; End of While loop

Structured Assembly Language Programming While Temp > #$4F (unsigned) Assembly Code Loop: LDAA Temp CMPA #$4F CMPA #$4F BLS End_While ; (opposite of >) BLS End_While ; (opposite of >) Do code here Do code here ……………… ……………… …………….. …………….. BRA Loop BRA Loop End_While: ; End of While loop

Structured Assembly Language Programming Do-While or (Repeat-Until) Loops Pseudo-code Syntax Pseudo-code Syntax Do (or Repeat) Begin Begin Code to Execute ……..…….. While Condition (or Until Condition) While Condition (or Until Condition)

Structured Assembly Language Programming Do-While or (Repeat-Until) Loops Assembly Language Syntax Loop: Do Code Here ……..…….. LDAA Variable LDAA Variable CMPA #Constant CMPA #Constant B?? Loop ; (same as condition) B?? Loop ; (same as condition)

Structured Assembly Language Programming Do-While or (Repeat – Until) Condition : Branch to Use (Same as condition) = : BEQ (Branch if equal) = : BEQ (Branch if equal) <> : BNE (Branch if not equal) <> : BNE (Branch if not equal) Signed Signed > : BGT (Branch if greater than) >= : BGE (Branch if greater than or equal) < : BLT (Branch if less than) <= : BLE (Branch if less than or equal) Unsigned Unsigned > : BHI (Branch if higher) >= : BHS (Branch if higher or same) < : BLO (Branch if lower) <= : BLS (Branch if lower or same)

Example Write a 68HC11 assembly language program that converts X into an equivalent ASCII signed decimal value. Store your result in a memory location labeled: Result.

Pseudo-code X = Number_to_convert sign = “+” if X < 0 then sign = “-” X = -X end If result[0] = sign call X2ASC(X) end program X2ASC is a subroutine that converts the X register (unsigned) to ASCII We’ll put this code into a subroutine called SX2ASC that converts the X register (signed) to ASCII.

Let’s convert the code… For this line of pseudo-code: X = Number_to_convert We’ll create the following code… NUMBER: EQU $8001 ; … ORG CODE_AREA ORG CODE_AREA Main: LDX #NUMBER ;Put it in regX JSR SX2ASC ;Signed convert JSR SX2ASC ;Signed convert End: BRA End ;Infinite loop

Next line At the start of the SX2ASC subroutine, we have the pseudocode line: sign = “+” We’ll convert this to: ORG DATA_AREA ORG DATA_AREA … sign: RMB 1 ;1 byte for sign char … ORG CODE_AREA ORG CODE_AREA … SX2ASC: LDAA #’+ ; Take an ASCII + sign. STAA sign ; Store it in “sign” var. STAA sign ; Store it in “sign” var.

Next line Next, we have: if X < 0 then … end if This converts to the assembly: CPX #0 ; Compare X to 0. CPX #0 ; Compare X to 0. BHS endIf ; If X>=0, skip if body BHS endIf ; If X>=0, skip if body … (IF body goes here) … (IF body goes here) endIf: … (code after IF goes here)

A more difficult case Consider the pseudocode line: X = −X (X gets negative X) Problem: There’s no NEGX instruction! How can we work around this? How can we work around this? One solution: varX RMB 2 ; Reserve space for X in mem. … STX varX ; Save X in memory at varX STX varX ; Save X in memory at varX LDD #0 ; Load accum. D with a 0 LDD #0 ; Load accum. D with a 0 SUBD varX ; Let D = 0 – varX = -X SUBD varX ; Let D = 0 – varX = -X XGDX ; Copy D back to X. XGDX ; Copy D back to X.

Full SX2ASC in assembly SX2ASC: LDAA #'+ ; Take an ASCII "+" sign. STAA sign ; Store it in "sign" variable. STAA sign ; Store it in "sign" variable. CPX #0 ; Compare X to 0. CPX #0 ; Compare X to 0. BHS endIf ; If X < 0, then... BHS endIf ; If X < 0, then... LDAA #'- ; Take an ASCII "-" sign. LDAA #'- ; Take an ASCII "-" sign. STAA sign ; Store it in "sign" variable. STAA sign ; Store it in "sign" variable. STX varX ; Save X in "varX" variable. STX varX ; Save X in "varX" variable. LDD #0 ; D = 0. LDD #0 ; D = 0. SUBD varX ; D = 0 - varX = -varX = -X. SUBD varX ; D = 0 - varX = -varX = -X. XGDX ; Copy D back to X. XGDX ; Copy D back to X. endIf: LDAA sign ; A = sign character. STAA result+0 ; Store sign char. in result[0]. STAA result+0 ; Store sign char. in result[0]. JSR X2ASC ; Conv. unsigned X to result[1-5]. JSR X2ASC ; Conv. unsigned X to result[1-5]. RTS ; End of subroutine; return. RTS ; End of subroutine; return. X = −X

Pseudo-code: X2ASC (w. divide) function X2ASC(X,result) D = X for B = 0 to 3 A = floor(D / convert[B]) ; where convert[]= 10000,1000,100,10 D = D – A*convert[B] ; Let D be the remainder A = A or #$30 ; Convert A from 0-9 to ASCII result[B+1] = A end for A = D ; D should have the ones A = A or #$30; Convert to ASCII result[4] = A end function Note: When converting this to working assembly code, we must be careful about overlapping registers.

for loop outline The skeleton of our for loop: for B = 0 to 3 … (body of for loop) end for for B = 0 to 3 … (body of for loop) end for Translates to: LDAB #0 LDAB #0 forBody: … ;body code goes here INCB ; B = B + 1 INCB ; B = B + 1 CMPB #3 ; Compare B to 3 CMPB #3 ; Compare B to 3 BLS forBody ; If B<=3, continue BLS forBody ; If B<=3, continue But, we must be careful to preserve the value of B within the body code!

Accessing an array of words We can set up the convert[] array as follows: convert FDB 10000,1000,100,10 But, how do we access convert[B]? CLRA; Effectively sets D=B CLRA; Effectively sets D=B ASLD; Double D, it’s 2B. ASLD; Double D, it’s 2B. XGDY; Transfer this to Y XGDY; Transfer this to Y LDX convert,Y; X = *(convert+2B) LDX convert,Y; X = *(convert+2B) But, this only works if convert array is on page 0! More generally, we’d have to do this after the ASLD: More generally, we’d have to do this after the ASLD: ADDD #convert; D = convert+2B ADDD #convert; D = convert+2B XGDY; Transfer D to Y LDX 0,Y; X = *Y = convert[B] LDX 0,Y; X = *Y = convert[B]

Unsigned X2ASC in assembly X2ASC: STX varD ; Initialize variable D = X. LDAB #0 ; For B = 0 to 3, LDAB #0 ; For B = 0 to 3, ForBody:STAB varB ; Save variable B. CLRA ; Clear MSB of accumulator D. CLRA ; Clear MSB of accumulator D. ASLD ; Double D to get 2*varB. ASLD ; Double D to get 2*varB. XGDY ; Move it into Y. XGDY ; Move it into Y. LDX convert,Y ; Let X = *(convert+Y) = convert[varB] LDX convert,Y ; Let X = *(convert+Y) = convert[varB] LDD varD ; Load old variable D. LDD varD ; Load old variable D. IDIV ; (X,D) = (quotient,remainder) of D/X. IDIV ; (X,D) = (quotient,remainder) of D/X. STX quot ; Save quotient temporarily. STX quot ; Save quotient temporarily. STD varD ; Save remainder as new variable D. STD varD ; Save remainder as new variable D. CLRA ; Clear MSB of accumulator D. CLRA ; Clear MSB of accumulator D. LDAB varB ; Set LSB of D = variable B. LDAB varB ; Set LSB of D = variable B. XGDY ; Move value of B from D into Y. XGDY ; Move value of B from D into Y. INY ; Increment Y to be varB+1. INY ; Increment Y to be varB+1. LDAA quot+1 ; A = LSB of quotient. LDAA quot+1 ; A = LSB of quotient. ORAA #$30 ; Convert A to ASCII. ORAA #$30 ; Convert A to ASCII. STAA result,Y ; result[varB+1] = A. STAA result,Y ; result[varB+1] = A. LDAB varB ; Load old value of variable B. LDAB varB ; Load old value of variable B. INCB ; Increment B to next value. INCB ; Increment B to next value. CMPB #3 ; Compare B with 3. CMPB #3 ; Compare B with 3. BLS ForBody ; End For. (Continue while B <= 3.) BLS ForBody ; End For. (Continue while B <= 3.) LDAA varD+1 ; A = LSB of D (final remainder). LDAA varD+1 ; A = LSB of D (final remainder). ORAA #$30 ; Convert A to ASCII. ORAA #$30 ; Convert A to ASCII. STAA result+5 ; result[5] = A. STAA result+5 ; result[5] = A. RTS ; Subroutine done; return. RTS ; Subroutine done; return. X=convert[B] result[B+1]=A+$30

Pseudo-code: X2ASC (w/o divide) Function X2ASC(X,Result) D = X For B = 0 to 3 A = 0 ; Count the number of times Convert can be subtracted from D While D>0 Do A = A + 1 D = D – Convert[B] ; Convert= 10000,1000,100,10 End While D = D + Convert[B] A = A – 1 A = A OR #$30; Convert A from 0-9 to ASCII Result[B] = A END For A = D ; D should have the ones A = A OR #$30; Convert to ASCII Result[4] = A End Function Note: When converting this to HC11 assembly code, we must be careful because the HC11’s register D overlaps registers A and B. This version avoids doing division and would work on a processor without a divide instruction.