lecture08 - 0306-250 Assembly Language Programming Lecture...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 0306-250 Assembly Language Programming Lecture Eight: • String/Iteration Examples • DBcond • Subroutines • Stack for Call/Return • Stack for Save/Restore String Example One v.2 #define LETTERS (26) char char int8 int8 Letter; String[LETTERS]; Index; N = LETTERS; Letter = ‘A’; for (Index=0; Index<N; Index++) { STRING[Index] = Letter++; } ; A: Letter ; X: Pointer to STRING ; B: Index LETTERS: EQU 26 movb ldaa ldx #LETTERS,N #’A’ #String ForInit: clrb ForCond: cmpb bge N ForEnd staa inca b,x ForBody: ForStep: ForEnd: Done: incb bra ForCond ; . . . bra * MyData: String N SECTION DS.B 26 DS.B 1 2 String Example Two #define LETTERS (26) char char char char int8 *StrPtr; *StrPast; Letter; String[26]; N = LETTERS; Letter = ‘A’; StrPast = String + N; for (StrPtr = String; StrPtr < StrPast; ) { *(StrPtr++) = Letter++ } ; X: StrPtr ; A: Letter LETTERS: EQU 26 movb ldaa movw #LETTERS,N #’A’ #(String + LETTERS),StrPast ldx #String cpx bhs StrPast ForEnd 1,x+ ForEnd: Done: staa inca bra ; . . bra MyData: StrPast String N SECTION DS.W 1 DS.B 26 DS.B 1 ForInit: ForCond: ForBody: ForCond . * 3 DBcond abdxys, Label DBNE: Decrement counter register (A, B, D, X, Y, or S) and branch to label if abdxys ¹ 0 To implement n iterations with DBNE initialize counter varaible Dn.W to n DBEQ: Decrement counter register (A, B, D, X, Y, or S) and branch to label if abdxys = 0 4 DBNE abdxys,Label S12CPUV2 Reference Manual, p. 56 5 String Example Three #define LETTERS (26) char char char int8 int8 *StrPtr; Letter; String[26]; Index; N = LETTERS; Letter = ‘A’ + LETTERS - 1; StrPtr = String - 1; Index = N; repeat { *(StrPtr + Index) = Letter--; } until (--Index = 0); ; X: StrPtr ; A: Letter ; B: Index LETTERS: EQU 26 movb ldaa ldx ldab #LETTERS,N #(’A’+LETTERS–1) #(String – 1) N staa deca b,x Repeat: Until: dbne b,Repeat RepeatEnd: ; . . . Done: bra * MyData: String N SECTION DS.B 26 DS.B 1 6 String Example Four #define LETTERS (26) char char char int8 int8 *StrPtr; Letter; String[26]; Index; N = LETTERS; Letter = ‘A’; StrPtr = String; Index = N; repeat { *(StrPtr++) = Letter++; } until (--Index = 0); ; X: StrPtr ; A: Letter ; B: Index LETTERS: EQU 26 movb ldaa ldx ldab #LETTERS,N #’A’ #String N staa inca 1,x+ Repeat: Until: dbne b,Repeat RepeatEnd: ; . . . Done: bra * MyData: String N SECTION DS.B 26 DS.B 1 7 String Copy • Write an HCS12 assembly language code fragment to copy the contents of one string to another. • The source string is String1, and it has L_String1 characters. • The destination string is String2. • Use DBNE for iteration. 8 String Copy Code ; X: ; Y: ; D: Pointer to String1 Pointer to String2 Length of String1 ; Initialize string pointers ldx #String1 ldy #String2 ldd #L_String1 Copy: ; Copy a character movb dbne Done: . . . ; Constants String1 DC.B L_String1 EQU . . . ; Variables String2 DS.B 1,x+,1,y+ D,Copy ‘String’ *-String1 80 9 Subroutine • Basic procedural unit of assembly language program • Call: JSR or BSR instruction – Places return address (i.e., PC at call) on system stack • Exit: RTS instruction – Retrieves return address (i.e., restores PC) from system stack • Parameters: must be placed in “expected” locations before call • System stack: must be initialized in program for call/return to work 10 JSR Label S12CPUV2 Reference Manual, p. 195 11 BSR Label S12CPUV2 Reference Manual, p. 140 12 RTS S12CPUV2 Reference Manual, p. 271 13 System Stack for Call/Return • JSR or BSR: Pushes onto the system stack the word (16-bit) address of the instruction immediately following • RTS Pulls from the system stack the program counter (long word) • SP: Stack pointer Must be initialized before using stack XREF __SEG_END_SSTACK lds #__SEG_END_SSTACK 14 StringCopy Subroutine • Convert the previous string copy code into a subroutine—perhaps for a string library. • StringCopy copies one string to another. 15 StringCopy Subroutine StringCopy: ; Parameters ; Inputs: ; X: Pointer to source string ; Y: Pointer to destination string ; D: Length of source string ; Warning: modifies all input registers movb dbne 1,x+,1,y+ ; Copy a character d,StringCopy rts 16 Subroutine Registers • StringCopy – Modifies input parameter registers – Modifies other registers as needed for temporary values – Perhaps unwanted side effects on return • How could these side effects be eliminated? – Subroutine has to be able to modify data – Can subroutine hide changes made? 17 Save/Restore Registers • Need to save/restore registers in subroutine – Save all non-output register values – Complete subroutine code – Restore registers with values that were saved • Where can the values be saved? – Using other registers (if available) would create more side effects. – Using the system stack would be transparent. 18 RTS S12CPUV2 Reference Manual, p. 82 19 StringCopy Subroutine without Side Effects StringCopy: ; Parameters ; Inputs: ; X: Pointer to source string ; Y: Pointer to destination string ; D: Length of source string ; Leaves all registers unchanged ; Save input/used registers pshx pshy psyd Copy: movb 1,x+,1,y+ dbne d,Copy ; Restore input/used registers puld puly pulx rts ; Copy a character 20 ...
View Full Document

This note was uploaded on 05/06/2010 for the course EECC 0306-250 taught by Professor Roymelton during the Fall '10 term at RIT.

Ask a homework question - tutors are online