Assembler Programming Chapter 6. EEL-4746 Best Practices.

42
Assembler Assembler Programming Programming Chapter 6 Chapter 6

Transcript of Assembler Programming Chapter 6. EEL-4746 Best Practices.

Page 1: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Assembler ProgrammingAssembler Programming

Chapter 6Chapter 6

Page 2: Assembler Programming Chapter 6. EEL-4746 Best Practices.

EEL-4746 Best EEL-4746 Best PracticesPractices

Page 3: Assembler Programming Chapter 6. EEL-4746 Best Practices.

EEL-4746 Best PracticesEEL-4746 Best Practices

1.1. All programs must begin with the following All programs must begin with the following commentcomment

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

Page 4: Assembler Programming Chapter 6. EEL-4746 Best Practices.

EEL-4746 Best PracticesEEL-4746 Best Practices

2.2. All subroutines must begin with the All subroutines must begin with the following commentfollowing 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

Page 5: Assembler Programming Chapter 6. EEL-4746 Best Practices.

EEL-4746 Best PracticesEEL-4746 Best Practices

3.3. All lines must contain a commentAll lines must contain a comment

4.4. All labels must end with a semicolonAll labels must end with a semicolon

5.5. Must use a symbol for all constantsMust use a symbol for all constants

Page 6: Assembler Programming Chapter 6. EEL-4746 Best Practices.

EEL-4746 Best PracticesEEL-4746 Best Practices5.5. Program must have the following format:Program must have the following format:

Header CommentHeader Comment ********************************************** * Standard Symbols* Standard Symbols Data EQU $0000Data EQU $0000 Program EQU $E000Program EQU $E000 Stack EQU $00FFStack EQU $00FF Reset EQU $FFFEReset EQU $FFFE ************************************** Place your symbols herePlace your symbols here ************************************ * Program* Program Top: ORG ProgramTop: ORG Program Label: Program line ; commentLabel: Program line ; comment **************************************************** * Data * Data

ORG DataORG Data Variable {Directive} ; Data goes hereVariable {Directive} ; Data goes here

ORG ResetORG ResetFDB TopFDB Top

Page 7: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Additional CommentsAdditional Comments

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

Upper,Lower, and Mixed CasesUpper,Lower, and Mixed Cases Be consistentBe consistent

Symbols for constants in ALLUPPERCASESymbols for constants in ALLUPPERCASELabels for in-memory variables in lowercaseLabels for in-memory variables in lowercaseCode labels in MixedCaseCode labels in MixedCaseInstructions all uppercase or all lowercaseInstructions all uppercase or all lowercaseEtcEtc

Subroutine HeadersSubroutine Headers Develop the habit of placing comments before subroutines. Develop the habit of placing comments before subroutines.

The comment should give the function of the subroutine, the The comment should give the function of the subroutine, the input values it expects, and the registers that it modifies. input values it expects, and the registers that it modifies.

Page 8: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Pseudo-code Pseudo-code

A “fictitious” programming language A “fictitious” programming language which allows a general algorithm for a which allows a general algorithm for a computer program to be developed.computer program to be developed.

Design processDesign process1.1. Develop program using pseudo-codeDevelop program using pseudo-code

2.2. Convert pseudo-code to 68HC11 assembly Convert pseudo-code to 68HC11 assembly language language

Page 9: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

If – Then –Else – End IFIf – Then –Else – End IF

Pseudo-Code SyntaxPseudo-Code Syntax

If Variable_A Condition Variable_B If Variable_A Condition Variable_B Then Then Pseudo Code for Then conditionPseudo Code for Then condition

Else (Optional)Else (Optional)Pseudo Code B for Else conditionPseudo Code B for Else condition

End IfEnd If

Page 10: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

LDAA Variable_ALDAA Variable_A

CMPA Variable_BCMPA Variable_B

B?? Else ; Opposite of conditionB?? Else ; Opposite of condition

Code for Code for ThenThen part part

………………………… …………………………..

BRA END_IF BRA END_IF ; need to skip else part; need to skip else part

Else : Code for Else : Code for ElseElse part part

……………………… ………………………....

END_IF: Rest of CodeEND_IF: Rest of Code

Page 11: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

If-Then-Else-End IFIf-Then-Else-End IFCondition : Branch to Use (Opposite of condition)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) SignedSigned

<= : BGT (Branch if greater than)<= : BGT (Branch if greater than)< : BGE (Branch if greater than or equal)< : BGE (Branch if greater than or equal)>= : BLT (Branch if less than)>= : BLT (Branch if less than)> : BLE (Branch if less than or equal)> : BLE (Branch if less than or equal)

UnsignedUnsigned<= : BHI (Branch if higher)<= : BHI (Branch if higher) < : BHS (Branch if higher or same)< : BHS (Branch if higher or same)>= : BLO (Branch if lower)>= : BLO (Branch if lower)> : BLS (Branch if lower or same)> : BLS (Branch if lower or same)

Page 12: Assembler Programming Chapter 6. EEL-4746 Best Practices.

If-Then-Else ExampleIf-Then-Else Example

Pseudo-codePseudo-code IF temp > max_temp thenIF temp > max_temp then Then codeThen code ElseElse Else codeElse code End IfEnd If

Page 13: Assembler Programming Chapter 6. EEL-4746 Best Practices.

If-Then-Else ExampleIf-Then-Else Example

Assembly LanguageAssembly Language LDAA TempLDAA Temp CMPA MAX_TEMPCMPA MAX_TEMP BLS ElseBLS Else Then code hereThen code here BRA ENDIFBRA ENDIF ELSE: ELSE: ELSE code hereELSE code here EndIf:EndIf: Rest of program hereRest of program here

Page 14: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

LoopsLoops For LoopsFor Loops While-Do LoopWhile-Do Loop Do-While or (Repeat – Until) LoopsDo-While or (Repeat – Until) Loops

Page 15: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

For LoopsFor Loops Pseudo-code SyntaxPseudo-code Syntax

For loop_index = start_index to end_indexFor loop_index = start_index to end_index BeginBegin

Code to ExecuteCode to Execute

…………....

………….... End For LoopEnd For Loop

Page 16: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

Assembly LanguageAssembly 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)A,B=8 bit (255 max)X,Y=16 bit (65535 max)X,Y=16 bit (65535 max)Must use memory if index value is greater than 65535Must use memory if index value is greater than 65535

Example Code FragmentExample Code Fragment LDAA #start_indexLDAA #start_indexLoop: Start of Code to Execute (must not change A)Loop: Start of Code to Execute (must not change A) ………… ………….. INCAINCA CMPA #end_indexCMPA #end_index BLS LoopBLS Loop

Page 17: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

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

LDAA #start_indexLDAA #start_index

Loop: Start of Code to Execute Loop: Start of Code to Execute PSHA ; Save A on stackPSHA ; Save A on stack

Use A Use A ………… ………….. PULA ; Restore A from stackPULA ; Restore A from stack INCAINCA CMPA #end_indexCMPA #end_index BLS LoopBLS Loop

Page 18: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

For Loops (Count down)For Loops (Count down) Pseudo-code SyntaxPseudo-code Syntax

For loop_index = end_index downto start_indexFor loop_index = end_index downto start_index BeginBegin

Code to ExecuteCode to Execute

…………....

………….... End For LoopEnd For Loop

Page 19: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

Example Code FragmentExample Code Fragment LDAA #end_indexLDAA #end_index

Loop: Start of Code to Execute Loop: Start of Code to Execute

……………………… ………………………..

DECADECA

CMPA #start_indexCMPA #start_index

BHS LoopBHS Loop

Page 20: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

For Loops (Count down from N to 1)For Loops (Count down from N to 1)

This counts N itemsThis counts N items Pseudo-code SyntaxPseudo-code Syntax

For loop_index = end_index downto 1For loop_index = end_index downto 1 BeginBegin

Code to ExecuteCode to Execute

…………....

………….... End For LoopEnd For Loop

Page 21: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

Example Code FragmentExample Code Fragment LDAA #NLDAA #N

Loop: Start of Code to Execute Loop: Start of Code to Execute

……………………… ………………………..

DECADECA

BNE Loop ; Save one statementBNE Loop ; Save one statement

Page 22: Assembler Programming Chapter 6. EEL-4746 Best Practices.

TPS QuizTPS Quiz

Page 23: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

While-Do LoopsWhile-Do Loops Pseudo-code SyntaxPseudo-code Syntax

While Variable Condition Constant_VALUE While Variable Condition Constant_VALUE

Do Code HereDo Code Here BeginBegin

Code to ExecuteCode to Execute

…………....

………….... End While End While

Page 24: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

While-Do LoopsWhile-Do Loops Loop: LDAA VariableLoop: LDAA Variable CMPA #Constant_ValueCMPA #Constant_Value B?? End_While ; (opposite of condition)B?? End_While ; (opposite of condition) Do code hereDo code here ……………… ……………… …………… …………….... BRA LoopBRA Loop End_While: ; End of While loopEnd_While: ; End of While loop

Page 25: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

While-DoWhile-DoCondition : Branch to Use (Opposite of condition)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) SignedSigned

<= : BGT (Branch if greater than)<= : BGT (Branch if greater than)< : BGE (Branch if greater than or equal)< : BGE (Branch if greater than or equal)>= : BLT (Branch if less than)>= : BLT (Branch if less than)> : BLE (Branch if less than or equal)> : BLE (Branch if less than or equal)

UnsignedUnsigned<= : BHI (Branch if higher)<= : BHI (Branch if higher) < : BHS (Branch if higher or same)< : BHS (Branch if higher or same)>= : BLO (Branch if lower)>= : BLO (Branch if lower)> : BLS (Branch if lower or same)> : BLS (Branch if lower or same)

Page 26: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

Example: Temp > #$4F (unsigned)Example: Temp > #$4F (unsigned)

Pseudo-code:Pseudo-code:

While Temp > $4F While Temp > $4F DoDo

(Execute your code here)(Execute your code here)

End_While: ; End of While loopEnd_While: ; End of While loop

Page 27: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

While Temp > #$4F (unsigned)While Temp > #$4F (unsigned)

Assembly CodeAssembly CodeLoop: LDAA TempLoop: LDAA Temp

CMPA #$4FCMPA #$4F

BLS End_While ; (opposite of >)BLS End_While ; (opposite of >)

Do code hereDo code here

……………… ………………

…………… ……………....

BRA LoopBRA Loop

End_While: ; End of While loopEnd_While: ; End of While loop

Page 28: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

Do-While or (Repeat-Until) LoopsDo-While or (Repeat-Until) Loops Pseudo-code SyntaxPseudo-code Syntax

Do (or Repeat)Do (or Repeat) BeginBegin

Code to ExecuteCode to Execute

…………....

………….... While Condition (or Until Condition)While Condition (or Until Condition)

Page 29: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

Do-While or (Repeat-Until) LoopsDo-While or (Repeat-Until) Loops

Assembly Language SyntaxAssembly Language SyntaxLoop: Loop:

Do Code HereDo Code Here

…………....

………….... LDAA VariableLDAA Variable CMPA #ConstantCMPA #Constant B?? Loop ; (same as condition)B?? Loop ; (same as condition)

Page 30: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Structured Assembly Language Structured Assembly Language ProgrammingProgramming

Do-While or (Repeat – Until)Do-While or (Repeat – Until)Condition : Branch to Use (Same as condition)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) SignedSigned

> : BGT (Branch if greater than)> : BGT (Branch if greater than)>= : BGE (Branch if greater than or equal)>= : BGE (Branch if greater than or equal)< : BLT (Branch if less than)< : BLT (Branch if less than)<= : BLE (Branch if less than or equal)<= : BLE (Branch if less than or equal)

UnsignedUnsigned> : BHI (Branch if higher)> : BHI (Branch if higher)>= : BHS (Branch if higher or same)>= : BHS (Branch if higher or same)< : BLO (Branch if lower)< : BLO (Branch if lower)<= : BLS (Branch if lower or same)<= : BLS (Branch if lower or same)

Page 31: Assembler Programming Chapter 6. EEL-4746 Best Practices.

ExampleExample

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

Page 32: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Pseudo-code Pseudo-code

XX = Number_to_convert = Number_to_convertsignsign = “+” = “+”if if XX < 0 then < 0 then

signsign = “-” = “-”XX = - = -XX

end Ifend Ifresultresult[0] = [0] = signsigncall X2ASC(X) call X2ASC(X) end programend program

X2ASC is a subroutine that converts the X register X2ASC is a subroutine that converts the X register (unsigned) to ASCII(unsigned) to ASCII

We’ll put this code intoWe’ll put this code intoa subroutine calleda subroutine calledSX2ASC that convertsSX2ASC that convertsthe X register (signed)the X register (signed)to ASCII.to ASCII.

Page 33: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Let’s convert the code…Let’s convert the code…

For this line of pseudo-code:For this line of pseudo-code:XX = Number_to_convert = Number_to_convert

We’ll create the following code…We’ll create the following code…NUMBER: EQU $8001 ; -32767NUMBER: EQU $8001 ; -32767

……

ORG CODE_AREAORG CODE_AREA

Main: LDX #NUMBER ;Put it in regXMain: LDX #NUMBER ;Put it in regX

JSR SX2ASC ;Signed convertJSR SX2ASC ;Signed convert

End: BRA End ;Infinite loopEnd: BRA End ;Infinite loop

Page 34: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Next lineNext line

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

sign = “+”sign = “+”

We’ll convert this to:We’ll convert this to: ORG DATA_AREAORG DATA_AREA … …sign: RMB 1 ;1 byte for sign charsign: RMB 1 ;1 byte for sign char … … ORG CODE_AREAORG CODE_AREA … …SX2ASC: LDAA #’+ ; Take an ASCII + sign.SX2ASC: LDAA #’+ ; Take an ASCII + sign. STAA sign ; Store it in “sign” var.STAA sign ; Store it in “sign” var.

Page 35: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Next lineNext lineNext, we have:Next, we have:

if if XX < 0 then … < 0 then … end ifend if

This converts to the assembly:This converts to the assembly: CPX #0 ; Compare X to 0.CPX #0 ; Compare X to 0.

BHS endIf ; If X>=0, skip if bodyBHS endIf ; If X>=0, skip if body

… … (IF body goes here)(IF body goes here)

endIf: … (code after IF goes here)endIf: … (code after IF goes here)

Page 36: Assembler Programming Chapter 6. EEL-4746 Best Practices.

A more difficult caseA more difficult case

Consider the pseudocode line:Consider the pseudocode line:XX = = −−XX ((XX gets negative gets negative XX))

Problem:Problem: There’s no NEGX instruction! There’s no NEGX instruction! How can we work around this?How can we work around this?

One solution:One solution:varX RMB 2 ; Reserve space for X in mem.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 = -XSUBD varX ; Let D = 0 – varX = -X XGDX ; Copy D back to X. XGDX ; Copy D back to X.

Page 37: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Full SX2ASC in assemblyFull SX2ASC in assemblySX2ASC: LDAA #'+ ; Take an ASCII "+" sign.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.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

= −

X−X

Page 38: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Pseudo-code: X2ASC (w. divide)Pseudo-code: X2ASC (w. divide)function X2ASC(function X2ASC(XX,,resultresult))

DD = = XXfor for BB = 0 to 3 = 0 to 3

AA = floor( = floor(DD / convert[ / convert[BB]) ]) ; where convert[]= 10000,1000,100,10; where convert[]= 10000,1000,100,10

DD = = DD – – AA*convert[*convert[BB] ; Let ] ; Let DD be the remainder be the remainder AA = = AA or #$30 or #$30 ; Convert ; Convert AA from 0-9 to from 0-9 to

ASCIIASCIIresultresult[[BB+1] = +1] = AA

end forend forAA = = DD ; ; DD should have the ones should have the onesAA = = AA or #$30 or #$30 ; Convert to ASCII; Convert to ASCIIresultresult[4] = [4] = AA

end functionend function

Note: When converting this to working assemblyNote: When converting this to working assemblycode, we must be careful about overlapping code, we must be careful about overlapping registers.registers.

Page 39: Assembler Programming Chapter 6. EEL-4746 Best Practices.

for loop outlinefor loop outline

The skeleton of our The skeleton of our forfor loop: loop: for B = 0 to 3for B = 0 to 3

… (body of … (body of forfor loop) loop)end forend for

Translates to:Translates to: LDAB #0LDAB #0forBody: … ;body code goes hereforBody: … ;body code goes here INCB ; B = B + 1INCB ; B = B + 1 CMPB #3 ; Compare B to 3CMPB #3 ; Compare B to 3 BLS forBody ; If B<=3, continueBLS forBody ; If B<=3, continue

But, we must be careful to preserve the value of But, we must be careful to preserve the value of B within the body code!B within the body code!

Page 40: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Accessing an array of wordsAccessing an array of words

We can set up the convert[] array as follows:We can set up the convert[] array as follows:convert FDB 10000,1000,100,10convert FDB 10000,1000,100,10

But, how do we access convert[But, how do we access convert[BB]?]? CLRACLRA ; Effectively sets D=B; Effectively sets D=B ASLDASLD ; Double D, it’s 2B.; Double D, it’s 2B. XGDYXGDY ; Transfer this to Y; Transfer this to Y LDX convert,YLDX convert,Y ; X = *(convert+2B); X = *(convert+2B)

But, this only works if convert array is on page 0!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 #convertADDD #convert ; D = convert+2B; D = convert+2B

XGDYXGDY ; Transfer D to Y; Transfer D to Y LDX 0,YLDX 0,Y ; X = *Y = convert[B]; X = *Y = convert[B]

Page 41: Assembler Programming Chapter 6. EEL-4746 Best Practices.

Unsigned X2ASC in assemblyUnsigned X2ASC in assemblyX2ASC: STX varD ; Initialize variable D = X.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.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=

conv

ert[

B]

X=

conv

ert[

B]

resu

lt[B

+1]

=A

+$3

0re

sult[

B+

1]=

A+

$30

Page 42: Assembler Programming Chapter 6. EEL-4746 Best Practices.

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

D = XD = XFor B = 0 to 3For B = 0 to 3

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

A = A + 1 A = A + 1 D = D – Convert[B] ; Convert= 10000,1000,100,10D = D – Convert[B] ; Convert= 10000,1000,100,10

End WhileEnd WhileDD = = D + Convert[B] D + Convert[B]A = A – 1 A = A – 1 A = A OR #$30A = A OR #$30 ; Convert A from 0-9 to ASCII; Convert A from 0-9 to ASCIIResult[B] = AResult[B] = A

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

End FunctionEnd Function Note: When converting this to HC11 assemblyNote: When converting this to HC11 assemblycode, we must be careful because the HC11’scode, we must be careful because the HC11’sregister D overlaps registers A and B.register D overlaps registers A and B.

This version avoids doing divisionThis version avoids doing divisionand would work on a processorand would work on a processorwithout a divide instruction.without a divide instruction.