06 Assembler Programming

download 06 Assembler Programming

of 42

Transcript of 06 Assembler Programming

  • 8/12/2019 06 Assembler Programming

    1/42

    Assembler ProgrammingChapter 6

  • 8/12/2019 06 Assembler Programming

    2/42

    EEL-4746 Best

    Practices

  • 8/12/2019 06 Assembler Programming

    3/42

    EEL-4746 Best Practices

    1. All programs must begin with the followingcomment

    ***************************

    * EEL-4746 Spring 2004 Semester * Homework #NDue (Due Date)

    * Problem #M

    * Name of Partner A

    * Name of Partner B

    ****************************

    * Description of Program

  • 8/12/2019 06 Assembler Programming

    4/42

    EEL-4746 Best Practices

    2. All subroutines must begin with thefollowing comment

    ***************************

    * Subroutine Name: Mysub * Input parameter list:

    * Output parameter list:

    * Registers changed list: ****************************

    * Description of Subroutine

  • 8/12/2019 06 Assembler Programming

    5/42

    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

  • 8/12/2019 06 Assembler Programming

    6/42

    EEL-4746 Best Practices

    5. Program must have the following format: Header Comment *********************** * Standard Symbols Data EQU $0000 Program EQU $E000 Stack EQU $00FF

    Reset EQU $FFFE *******************

    Place your symbols here ****************** * Program Top: ORG Program Label: Program line ; comment ************************** * Data

    ORG Data

    Variable {Directive} ; Data goes hereORG ResetFDB Top

  • 8/12/2019 06 Assembler Programming

    7/42

    Additional Comments

    To Indent or Not Indent Not really used in assembly language programming

    Upper,Lower, and Mixed Cases Be consistent

    Symbols for constants in ALLUPPERCASELabels 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, theinput values it expects, and the registers that it modifies.

  • 8/12/2019 06 Assembler Programming

    8/42

    Pseudo-code

    A fictitious programming language

    which allows a general algorithm for acomputer program to be developed.

    Design process

    1. Develop program using pseudo-code

    2. Convert pseudo-code to 68HC11 assembly

    language

  • 8/12/2019 06 Assembler Programming

    9/42

    Structured Assembly LanguageProgramming

    IfThenElseEnd IF

    Pseudo-Code Syntax

    If Variable_A Condition Variable_B ThenPseudo Code for Then condition

    Else (Optional)Pseudo Code B for Else condition

    End If

  • 8/12/2019 06 Assembler Programming

    10/42

    Structured Assembly LanguageProgramming

    LDAA Variable_A

    CMPA Variable_B

    B?? Else ; Opposite of condition

    Code for Thenpart.

    BRA END_IF ; need to skip else part

    Else : Code for Elsepart

    ..END_IF: Rest of Code

  • 8/12/2019 06 Assembler Programming

    11/42

    Structured Assembly LanguageProgramming

    If-Then-Else-End IFCondition : Branch to Use (Opposite of condition) : BEQ (Branch if equal) = : BNE (Branch if not equal)

    Signed= : BLT (Branch if less than)> : BLE (Branch if less than or equal)

    Unsigned

    = : BLO (Branch if lower)> : BLS (Branch if lower or same)

  • 8/12/2019 06 Assembler Programming

    12/42

    If-Then-Else Example

    Pseudo-code

    IF temp > max_temp then

    Then code

    Else

    Else code

    End If

  • 8/12/2019 06 Assembler Programming

    13/42

    If-Then-Else Example

    Assembly Language

    LDAA Temp

    CMPA MAX_TEMP

    BLS Else Then code here

    BRA ENDIF

    ELSE:

    ELSE code here

    EndIf:

    Rest of program here

  • 8/12/2019 06 Assembler Programming

    14/42

    Structured Assembly LanguageProgramming

    Loops

    For Loops

    While-Do Loop

    Do-While or (RepeatUntil) Loops

  • 8/12/2019 06 Assembler Programming

    15/42

    Structured Assembly LanguageProgramming

    For Loops

    Pseudo-code Syntax

    For loop_index = start_index to end_index Begin

    Code to Execute

    ..

    ..

    End For Loop

  • 8/12/2019 06 Assembler Programming

    16/42

    Structured Assembly LanguageProgramming

    Assembly Language 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 65535

    Example Code FragmentLDAA #start_index

    Loop: Start of Code to Execute (must not change A).

    INCACMPA #end_index

    BLS Loop

  • 8/12/2019 06 Assembler Programming

    17/42

    Structured Assembly LanguageProgramming

    What if you need the loop index register (e.g. A)in your code?

    Example Code Fragment

    LDAA #start_indexLoop: Start of Code to Execute

    PSHA ; Save A on stack

    Use A

    .

    PULA ; Restore A from stack

    INCACMPA #end_index

    BLS Loop

  • 8/12/2019 06 Assembler Programming

    18/42

    Structured Assembly LanguageProgramming

    For Loops (Count down)

    Pseudo-code SyntaxFor loop_index = end_index downto start_index

    Begin

    Code to Execute

    ..

    ..

    End For Loop

  • 8/12/2019 06 Assembler Programming

    19/42

    Structured Assembly LanguageProgramming

    Example Code FragmentLDAA #end_index

    Loop: Start of Code to Execute

    .

    DECA

    CMPA #start_index

    BHS Loop

  • 8/12/2019 06 Assembler Programming

    20/42

    Structured Assembly LanguageProgramming

    For Loops (Count down from N to 1)

    This counts N items

    Pseudo-code SyntaxFor loop_index = end_index downto 1

    Begin

    Code to Execute

    ....

    End For Loop

  • 8/12/2019 06 Assembler Programming

    21/42

    Structured Assembly LanguageProgramming

    Example Code FragmentLDAA #N

    Loop: Start of Code to Execute

    .

    DECA

    BNE Loop ; Save one statement

  • 8/12/2019 06 Assembler Programming

    22/42

    TPS Quiz

  • 8/12/2019 06 Assembler Programming

    23/42

    Structured Assembly LanguageProgramming

    While-Do Loops

    Pseudo-code SyntaxWhile Variable Condition Constant_VALUE

    Do Code Here Begin

    Code to Execute

    ..

    .. End While

  • 8/12/2019 06 Assembler Programming

    24/42

    Structured Assembly LanguageProgramming

    While-Do Loops Loop: LDAA Variable

    CMPA #Constant_Value

    B?? End_While ; (opposite of condition)

    Do code here

    ..

    BRA Loop

    End_While: ; End of While loop

  • 8/12/2019 06 Assembler Programming

    25/42

    Structured Assembly LanguageProgramming

    While-DoCondition : Branch to Use (Opposite of condition) : BEQ (Branch if equal) = : BNE (Branch if not equal)

    Signed= : BLT (Branch if less than)> : BLE (Branch if less than or equal)

    Unsigned

    = : BLO (Branch if lower)> : BLS (Branch if lower or same)

  • 8/12/2019 06 Assembler Programming

    26/42

    Structured Assembly LanguageProgramming

    Example: Temp > #$4F (unsigned)

    Pseudo-code:

    While Temp > $4FDo

    (Execute your code here)

    End_While: ; End of While loop

  • 8/12/2019 06 Assembler Programming

    27/42

    Structured Assembly LanguageProgramming

    While Temp > #$4F (unsigned)

    Assembly CodeLoop: LDAA Temp

    CMPA #$4F

    BLS End_While ; (opposite of >)

    Do code here

    ..

    BRA Loop

    End_While: ; End of While loop

  • 8/12/2019 06 Assembler Programming

    28/42

    Structured Assembly LanguageProgramming

    Do-While or (Repeat-Until) Loops

    Pseudo-code SyntaxDo (or Repeat)

    BeginCode to Execute

    ..

    ..

    While Condition (or Until Condition)

  • 8/12/2019 06 Assembler Programming

    29/42

    Structured Assembly LanguageProgramming

    Do-While or (Repeat-Until) Loops

    Assembly Language SyntaxLoop:

    Do Code Here

    ..

    ..

    LDAA Variable

    CMPA #Constant B?? Loop ; (same as condition)

  • 8/12/2019 06 Assembler Programming

    30/42

    Structured Assembly LanguageProgramming

    Do-While or (RepeatUntil)Condition : Branch to Use (Same as condition) = : BEQ (Branch if equal) : BNE (Branch if not equal)

    Signed> : BGT (Branch if greater than)>= : BGE (Branch if greater than or equal)< : BLT (Branch if less than) : BHI (Branch if higher)>= : BHS (Branch if higher or same)< : BLO (Branch if lower)

  • 8/12/2019 06 Assembler Programming

    31/42

    Example

    Write a 68HC11 assembly languageprogram that converts X into anequivalent ASCII signed decimal value.

    Store your result in a memory locationlabeled: Result.

  • 8/12/2019 06 Assembler Programming

    32/42

    Pseudo-code

    X= Number_to_convert

    sign= +

    ifX< 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

    Well put this code into

    a subroutine calledSX2ASC that convertsthe X register (signed)to ASCII.

  • 8/12/2019 06 Assembler Programming

    33/42

    Lets convert the code

    For this line of pseudo-code:X= Number_to_convert

    Well create the following code

    NUMBER: EQU $8001 ; -32767

    ORG CODE_AREA

    Main: LDX #NUMBER ;Put it in regXJSR SX2ASC ;Signed convert

    End: BRA End ;Infinite loop

  • 8/12/2019 06 Assembler Programming

    34/42

    Next line

    At the start of the SX2ASC subroutine, we have thepseudocode line:

    sign = +

    Well convert this to:

    ORG DATA_AREA

    sign: RMB 1 ;1 byte for sign char

    ORG CODE_AREA

    SX2ASC: LDAA #+ ; Take an ASCII + sign.

    STAA sign ; Store it in sign var.

  • 8/12/2019 06 Assembler Programming

    35/42

    Next line

    Next, we have:ifX< 0 then end if

    This converts to the assembly:CPX #0 ; Compare X to 0.BHS endIf ; If X>=0, skip if body

    (IF body goes here)

    endIf: (code after IF goes here)

  • 8/12/2019 06 Assembler Programming

    36/42

    A more difficult case

    Consider the pseudocode line:X= X (Xgets negativeX)

    Problem: Theres no NEGX instruction! 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

    LDD #0 ; Load accum. D with a 0

    SUBD varX ; Let D = 0 varX = -X

    XGDX ; Copy D back to X.

  • 8/12/2019 06 Assembler Programming

    37/42

    Full SX2ASC in assembly

    SX2ASC: LDAA #'+ ; Take an ASCII "+" sign.STAA sign ; Store it in "sign" variable.

    CPX #0 ; Compare X to 0.

    BHS endIf ; If X < 0, then...

    LDAA #'- ; Take an ASCII "-" sign.

    STAA sign ; Store it in "sign" variable.

    STX varX ; Save X in "varX" variable.

    LDD #0 ; D = 0.

    SUBD varX ; D = 0 - varX = -varX = -X.

    XGDX ; Copy D back to X.

    endIf: LDAA sign ; A = sign character.

    STAA result+0 ; Store sign char. in result[0].JSR X2ASC ; Conv. unsigned X to result[1-5].

    RTS ; End of subroutine; return.

    X

    =

    X

  • 8/12/2019 06 Assembler Programming

    38/42

    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= DA*convert[B] ; Let Dbe the remainder

    A=Aor #$30 ; ConvertAfrom 0-9 to ASCII

    result[B+1] =A

    end for

    A= D ; Dshould have the ones

    A=Aor #$30 ; Convert to ASCII

    result[4] =A

    end functionNote: When converting this to working assembly

    code, we must be careful about overlapping

    registers.

  • 8/12/2019 06 Assembler Programming

    39/42

    for loop outline

    The skeleton of our forloop: for B = 0 to 3

    (body of forloop)end for

    Translates to:LDAB #0forBody: ;body code goes here

    INCB ; B = B + 1

    CMPB #3 ; Compare B to 3

    BLS forBody ; If B

  • 8/12/2019 06 Assembler Programming

    40/42

    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

    ASLD ; Double D, its 2B.XGDY ; Transfer this to Y

    LDX convert,Y ; X = *(convert+2B)

    But, this only works if convert array is on page 0! More generally, wed have to do this after the ASLD:

    ADDD #convert ; D = convert+2B

    XGDY ; Transfer D to Y

    LDX 0,Y ; X = *Y = convert[B]

    U i d X2ASC i bl

  • 8/12/2019 06 Assembler Programming

    41/42

    Unsigned X2ASC in assemblyX2ASC: STX varD ; Initialize variable D = X.

    LDAB #0 ; For B = 0 to 3,ForBody:STAB varB ; Save variable B.

    CLRA ; Clear MSB of accumulator D.ASLD ; Double D to get 2*varB.XGDY ; Move it into Y.LDX convert,Y ; Let X = *(convert+Y) = convert[varB]LDD varD ; Load old variable D.IDIV ; (X,D) = (quotient,remainder) of D/X.STX quot ; Save quotient temporarily.

    STD varD ; Save remainder as new variable D.CLRA ; Clear MSB of accumulator D.LDAB varB ; Set LSB of D = variable B.XGDY ; Move value of B from D into Y.INY ; Increment Y to be varB+1.LDAA quot+1 ; A = LSB of quotient.ORAA #$30 ; Convert A to ASCII.STAA result,Y ; result[varB+1] = A.

    LDAB varB ; Load old value of variable B.INCB ; Increment B to next value.CMPB #3 ; Compare B with 3.BLS ForBody ; End For. (Continue while B

  • 8/12/2019 06 Assembler Programming

    42/42

    Pseudo-code: X2ASC (w/o divide)Function X2ASC(X,Result)

    D = XFor B = 0 to 3

    A = 0; Count the number of times Convert can be subtracted from DWhile D>0 Do

    A = A + 1

    D = DConvert[B] ; Convert= 10000,1000,100,10End While

    D =D + Convert[B]A = A1A = A OR #$30 ; Convert A from 0-9 to ASCIIResult[B] = A

    END ForA = D ; D should have the onesA = A OR #$30 ; Convert to ASCIIResult[4] = A

    End Function Note: When converting this to HC11 assemblycode, we must be careful because the HC11s

    This version avoids doing division

    and would work on a processorwithout a divide instruction.