Microcontroller3

Microcontroller3 - 2/5/2011 Topics • Topics to be...

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: 2/5/2011 Topics • Topics to be covered: Stacks Subroutines Advanced Assembly Language Programming (Chapter 4) A.H. Mohsenian-Rad (U of T) Networking and Distributed Systems Strings 1 Dr. Hamed Mohsenian-Rad Stack ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-2 Texas Tech University 3-4 Stack • A first-in-last-out data structure. • Stack pointer (SP) points to the top of the stack. • Its elements can be accessed from only its top. • HCS12 stack grows from high address to low address • Operations: Low Address Push Push Top Element SP Pull Pull (or pop) Top Element High Address Bottom Element Bottom Element Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-3 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 1 2/5/2011 Stack Push and Pull Instructions Stack Push and Pull Instructions • Push a constant: movb movw #$F0, 1, -SP #$A0B2, 2, -SP ; Push 1 byte, ; Push 2 bytes, first decrement SP once. first decrement SP twice. ; Push 1 byte, ; Push 2 bytes, first decrement SP once. first decrement SP twice. ; Pull 1 byte, ; pull 2 bytes, then increment SP once. then increment SP twice. • Push a memory content: movb movw • Q: What is the value of B? ldaa psha pulb Dr. Hamed Mohsenian-Rad #$F0 $1000, 1, -SP $1000, 2, -SP • Pull to a memory location: ; [A] $F0 ; push A into the stack ; pull B from the stack ECE 3362: Microcontrollers – Spring 2011 movb movw Texas Tech University 3-5 Examples 1, SP+, $1000 2, SP+, $1000 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 3-6 Examples • Q: W hat will be at addresses $1000~$1001 after running this code? • Q: W hat are the values in the stack and the value of SP? org dc.b $1000 $FF, $FF org org lds $1000 #$2000 $1FFA org $1500 ldaa psha #$50 $1FFC lds N Texas Tech University #$2000 ldab pshb #$40 ldx pshx #0 movb movb movw swi end #$12, 1, -SP #$34, 1, -SP 2, SP+, N $1FFD ; set the top of the stack ; push $12 ; push $34 ; pull two bytes $1FFB $1FFE $1FFF $2000 ; break to D-Bug12 monitor • Recall that psha is equivalent to staa 1, -SP. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-7 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-8 2 2/5/2011 Examples Stack Pointer Instructions • Q: W hat is the value in accumulator A after running this code? org org lds $1000 #$2000 $1FFA ldab pshb #$FF • We can change the SP value using these instructions. $1FFC pula pula $1FFB $1FFD $1FFE $1FFF $2000 • Example: leas leas Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-9 Subroutine Dr. Hamed Mohsenian-Rad -5, SP 5, SP ; SP ; SP SP – 5 SP + 5 ; add 5 empty bytes / allocate memory ; kill 5 bytes / release memory ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-10 Subroutine Processing • A subroutine is a sequence of instructions that can be called from many different places in a program. • Allows the same operation to be performed with different parameters. … … <call> … … • Simplifies the design of a complex program by dividing the tasks. Jump subroutine subroutine Continue … … … <return> Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-11 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-12 3 2/5/2011 Subroutine Call and Return Subroutine Issues • Parameter Passing • The most common instruction to call a subroutine is jsr. Caller sends data to subroutine. JSR <subroutine address> • Result Returning Examples: JSR JSR JSR $1500 my_subroutine 0, X Subroutine returns data to the caller. ; Extended Addressing ; Using Label ; Indexed Mode • Allocation of Local Variables Many subroutines need local variables to do the job. • The most common instruction to return from a subroutine is rts. • Saving the CPU Registers It does not have any operand. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 The caller may want the registers to keep their values. Texas Tech University 3-13 Stack Frame Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-14 Example • Stack can be used to take care of • We want to write a subroutine that gets an array of 8-bit unsigned numbers and returns their summation as the result. All the issues in the previous slide. Let us call the subroutine: sumarr. Keep track of the return address. • Input Parameters: • For each subroutine, we define a stack frame. Parameter Name Local Variables SP Number of Bytes sa_p_arr 2 Pointer to Array Address 1 Length of Input Array sa_len • Everything will be part of the stack! Saved Registers Description Sum: 3 Return Address • It is recommended to include some pre-fix (e.g., sa_) for parameter / variable names related to each subroutine to distinguish it from parameters / variables in other subroutines and program main body. Incoming Parameters Returning Parameters Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-15 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-16 4 2/5/2011 Example Example • Output Parameters: • Registers to be Saved: Description Register Name Number of Bytes Pointer to Address to Keep the Result Y 2 Parameter Name Number of Bytes sa_p_res 2 X 2 D Sum: 2 2 CCR 1 Sum: 7 • Local Variables: Parameter Name Number of Bytes sa_i 1 Description Counter to go through the array Sum: 1 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-17 Example Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-18 Example • We can now construct the stack frame corresponding to the subroutine. SP Dr. Hamed Mohsenian-Rad SP + 0 SP + 1 SP + 2 SP + 3 SP + 4 SP + 5 SP + 6 SP + 7 SP + 8 SP + 9 SP + 10 SP + 11 SP + 12 SP + 13 SP + 14 sa_i C DH DL XH XL YH YL RTNH RTNL sa_len sa_p_arrH sa_p_arrL sa_p_resH sa_p_resL Local variable • Therefore, each variable/parameter can be reached using an offset. sa_i sa_len sa_p_arr sa_p_res Registers equ equ equ equ 0 10 11 13 ; offset to sa_i ; offset to sa_len ; offset to sq_p_arr ; offset to sa_res • Using Indexed Mode Addressing and Register SP: Return Address ldaa ldaa sa_i, SP sa_len, SP ; [A] ; [A] [sa_i] [sa_len] ldx ldx sa_p_arr, SP sa_p_res, SP ; [X] ; [X] [sa_p_arr] [sa_p_res] Incoming Parameters Outgoing Parameters ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-19 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-20 5 2/5/2011 Example Example • • Now let us look at the program: 1 2 3 ENDP: $1000 5, 24, 6, 13 0 ; array of numbers to be added together ; result org lds Arr result org dc.b dc.b $1500 #$2000 ; set SP to address $2000 movw movw movb jsr leas #result, 2, -SP #Arr, 2, -SP #4, 1, -SP sumarr 5, SP ; push: a hole for the result ; push: array pointer ; push: length of array ; call subroutine sumarr ; clean up the stack lbra … nop ENDP Here is the subroutine (1 of 3): ; we are done ; place the subroutine here ; end of the program sumarr sa_i sa_len sa_p_arr sa_p_res 2 ECE 3362: Microcontrollers – Spring 2011 ; store the registers pshc pshd pshx pshy leas • Dr. Hamed Mohsenian-Rad ; define offsets for local variables and incoming/returning parameters equ 0 ; offset to sa_i equ 10 ; offset to sa_len 1 equ 11 ; offset to sq_p_arr equ 13 ; offset to sa_res Texas Tech University 3-21 Example -1, SP ; allocate memory for local variables 3 Q: What is the content of the stack so far? Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-22 Example • Here is the subroutine (2 of 3): ldaa movb ldx sa_LOOP adda inx dec bhi #$00 sa_len, SP, sa_i, SP sa_p_arr, SP • Here is the subroutine (3 of 3): ; clear a ; initiate sa_i ; initiate x 1 1 ; release the memory for variable sa_i leas 1, SP ; restore the registers puly pulx 2 puld pulc 0, X 2 sa_i, SP sa_LOOP rts 3 ldx staa sa_p_res, SP 0, X • Q: Is the order of pulling registers the same as pushing? • Q: What if we do not release the stack in part 1? • Q: Could we do the calculation without using a local variable sa_i? Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-23 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-24 6 2/5/2011 Stack Frame Setup: Step 1 Stack Frame Setup: Step 2 • Same approach can be used for any subroutine design! • List all the registers you want to save in your subroutine. • List all the local variables you need in your subroutine. Register Name Variable Name Number of Bytes Number of Bytes Description Sum: Sum: Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-25 Stack Frame Setup: Step 3 Number of Bytes Texas Tech University 3-26 • List all the returning parameters from your subroutine. Description Parameter Name Sum: Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Stack Frame Setup: Step 4 • List all the incoming parameters to your subroutine. Parameter Name Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Number of Bytes Description Sum: Texas Tech University 3-27 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-28 7 2/5/2011 Stack Frame Setup: Step 5 Parameter Passing • Construct the stack frame and obtain offset for all variables/parameters. Local variable • Alternative ways to pass parameters or return results: Global variables. Registers Registers RTNH RTNL • But these approaches are not recommended! Return Address Incoming Parameters • We prefer to keep a subroutine implementation independent from the rest of the program. Outgoing Parameters Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-29 Subroutine for Creating Time Delay Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-30 Subroutine for Creating Time Delay • Each instruction takes some time to be executed. • Q: How long does it take to run this code? • The time it takes depends on: LOOP1 LOOP2 Number of Cycles CPU Bus Clock ldy ldx nop nop dbne dbne #1 #4799 X, LOOP2 Y, LOOP1 ; 2 cycles ; 2 cycles ; 1 cycle ; 1 cycle ; 3 cycles ; 3 cycles • Given the 24-MHz bus clock in Dragon-12 Demo Boards: Cycles Actual Time (µsec) nop Instruction 1 1/24 ldx #1000 3 3/24 psha 2 2/24 pula 2 2/24 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 [You should calculate the time for inner and outer loops.] Texas Tech University 3-31 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-32 8 2/5/2011 Subroutine for Creating Time Delay Subroutine for Creating Time Delay • Now we want to make it a subroutine for an arbitrary ms delay. • Here is the code (1 of 2): org $1000 lds • We have: one input parameter, no return parameter, no local variable. #$2000 movw SP C DH DL XH XL YH YL RTNH RTNL md_delH md_delL SP + 0 SP + 1 SP + 2 SP + 3 SP + 4 SP + 5 SP + 6 SP + 7 SP + 8 SP + 9 SP + 10 Dr. Hamed Mohsenian-Rad bra ENDP mydelay md_del Incoming Parameter Texas Tech University ; local variables and incoming/returning parameterm equ 9 ; offset to md_del pshc pshd pshx pshy Return Address 3-33 Subroutine for Creating Time Delay Dr. Hamed Mohsenian-Rad ; store the registers ; “ ; “ ; “ ECE 3362: Microcontrollers – Spring 2011 3 4 Texas Tech University 3-34 Subroutine for Creating Time Delay • Here is the code (2 of 2): LOOP1 LOOP2 1 mydelay 2, SP 2 Registers ECE 3362: Microcontrollers – Spring 2011 ; each inner loop is 1 ms ldy md_del, SP ldx #4799 nop nop dbne X, LOOP2 dbne Y, LOOP1 #5000, 2, -SP jsr leas • Q: How can we write a subroutine called delay10ms that Uses mydelay subroutine. 1 ; 2 cycles ; 1 cycle ; 1 cycle ; 3 cycles ; 3 cycles Creates 10 ms delay. 2 Does not have any incoming parameter. ; restore the registers puly pulx 3 puld pulc rts ENDP: 4 swi end • Q: How about subroutines delay100ms, delay1sec, …? Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-35 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-36 9 2/5/2011 Strings Example • A sequence of characters terminated by a NULL (ASCII code 0) or other special character such as EOT (ASCII code 4). • Consider 2-byte binary number 0000 0100 0000 0000 • In decimal representation, it is 1024 ASCII: #49, #48, #50, #52. • To display numbers/digits as string they should be converted to ASCII. Character ASCII (Decimal) ASCII (Hex) 0 48 $30 1 49 $31 2 50 3 “1” $31 $00 “0” $30 $32 “2” $32 51 $33 “4” $34 4 52 $34 NULL $00 5 53 $35 6 54 $36 7 55 $37 8 56 $38 9 Dr. Hamed Mohsenian-Rad $04 57 $39 ECE 3362: Microcontrollers – Spring 2011 Binary Texas Tech University 3-37 INT2STR Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 String Texas Tech University 3-38 INT2STR • Write a program to convert the unsigned 8-bit binary number in accumulator A into an ASCII null-ended string. Finding Decimal Digits 1st Digit: 1024 / 10 • The program should have two main parts: Remainder of dividing the number by 10. Remainder: 4 Quotient: 102 1st Digit: 4 Find and separate the decimal digits. 2nd Digit: Use the ASCII table to make digit-by-digit conversion. 102 / 10 • The program should also add a null to the end of the generated string. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-39 Dr. Hamed Mohsenian-Rad Remainder of dividing the quotient by 10. Remainder: 2 Quotient: 10 2nd Digit: 2 ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-40 10 2/5/2011 INT2STR INT2STR Finding Decimal Digits 3rd Digit: Finding Decimal Digits Remainder of dividing the number by 10. 10 / 10 3rd Digit: 0 Remainder: 0 Quotient: 1 4th Digit: test_dat equ … LOOP: 1 / 10 3 4th Digit: 1 Remainder: 1 Quotient: 0 We are done when Quotient is zero. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-41 INT2STR ldab clra … #test_dat ; load test_dat to D ; “ ldx idiv … #10 ; Divide D by 10 ; “ cpx beq xgdx bra …. 1 Remainder of dividing the quotient by 10. 234 #0 NEXTSTEP ; check if quotient is zero 2 ; put non-zero quotient back to D LOOP Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-42 INT2STR • Here is the Program (1 of 3): ASCII Conversion Given each digit, the ASCII code can be obtained by adding $30. Digit ASCII (Decimal) 48 49 $31 2 50 org ds.b 234 $1000 4 $30 1 1 ASCII (Hex) 0 test_dat equ $32 3 51 52 53 $35 6 54 $36 7 55 56 57 ; initialize stack pointer ; push a NULL into the stack #test_dat ; load test_dat to D ; “ $38 9 #$2000 #0,1,-sp $37 8 $1500 lds movb ldab clra $34 5 ; to hold the decimal string org $33 4 buf $39 2 • Here we use the stack as a memory to store the converted bits. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-43 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-44 11 2/5/2011 INT2STR INT2STR • Here is the Program (2 of 3): LOOP1: ldx idiv addb pshb cpx beq 2 #10 • Here is the Program (3 of 3): ; Divide D by 10 ; “ ; convert to ASCII code ; push Remainder into stack #$30 REVERSE ldy LOOP2 1 #0 REVERSE xgdx ; put quotient back to D 0,sp,1,y+ 0,sp ENDP ; start sending the result to buffer ; test [SP] 2 ; SP+ LOOP2 3 ENDP bra #buf movb tst beq ins bra 1 LOOP1 ins swi end • Q: What are the contents of memory addresses $1000~$1003? Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-45 INT2STR Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-46 INT2STR • Q: How about converting an 8-bit signed number? • Here is the program (1 of 2) test_dat equ %00000101 ; = -123 org ds.b $1000 4 ; to hold the decimal string org lds movb $1500 #$2000 #0,1,-sp ; initialize stack pointer ; push a NULL into the stack Insert “-” (ASCII $2D) in the string. ldy #buf Convert the number to a its absolute value. ldaa tsta bpl positive Test if the number is negative or positive. buf If the number is negative: Move on with regular INT2STR conversion. movb iny negb 1 positive: ldab clra Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-47 Dr. Hamed Mohsenian-Rad #test_dat #$2D, 0, Y ; insert ‘–’ at the beginning of the buffer 2 #test_dat ; load test_dat to D ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-48 12 2/5/2011 INT2STR STR2INT • Here is the program (2 of 2) loop1 ldx idiv addb pshb cpx beq xgdx bra #10 movb tst beq ins bra 0,sp,1,y+ 0,sp done • This time we want to convert a string to a binary number. #$30 ; convert to ASCII code ; push into stack • “163” done : #0 reverse 60 x1 + 3 + 163 ; test [SP] • We can follow a similar approach to write a program. loop2 swi end ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-49 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-50 STR2INT $1000 "136" 0 0 0 0 org lds movb #$2000 #0,1,-sp #buf ldaa beq suba psha bra 1, X+ Convert #$30 • Here is the Program (2 of 2): $1500 ldx Dr. Hamed Mohsenian-Rad 3 3 org fcc dc.b dc.b dc.b dc.b LOOP1: 100 loop1 • Here is the Program (1 of 2): 2 x 100 6 “3” STR2INT n i j 1 “6” ; put quotient back to B Dr. Hamed Mohsenian-Rad buf -$30 x 10 reverse loop2 “1” Convert: ; result ; temp variable ; temp variable 1 LOOP2: NEXT: 3 ENDP: LOOP1 ECE 3362: Microcontrollers – Spring 2011 Texas Tech University i, j ; set j = i ; pull up a digit ; test B to branch if reaching NULL 1 ; b = b * 10^i ;“ ;“ 2 ENDP 3-51 Dr. Hamed Mohsenian-Rad tst ble ldaa mul dec bra j NEXT #10 addb stab n n ;b ;n inc bra ; push a NULL into the stack ; branch if reaching the NULL ; - $30 ; push the result into stack movb pulb tstb beq i Convert ; increment i ; “ LOOP2 b+n b 3 swi end ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-52 13 2/5/2011 D-Bug12 Subroutines and Functions Parameter Passing • The D-Bug12 monitor provides a few subroutines. • If the subroutine has only one input parameter Then, the parameter is passed in accumulator D. • Some of them work with strings. • Some support I/O operations. Subroutine getchar () Function • If the subroutine has multiple input parameters Pointer Address Gets a character from Serial Ports / PC putchar () Sends a character to Serial Ports / PC $EE86 printf () Makes formatted string $EE88 toupper() Converts lowercase characters to uppercase $EE94 strlen() Returns the length of a NULL-ended string Then, the first parameter is passed in accumulator D. $EE84 The rest of the parameters are pushed into the stack. $EE98 • [Same as before] It is responsibility of the caller to remove passed parameters from the stack and release the allocated memory. • We can call these subroutines using jsr instruction. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-53 Keeping Register Values ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-54 STRLEN • Returns the length of a NULL-ended string. • None of the register values X, Y, D, C are preserved by the functions. • Thus, if you want to keep the value for any register, you should push them into the stack before you push any input parameter. 1 Dr. Hamed Mohsenian-Rad 1 equ equ $EE98 $0 2 mystr org dc.b $1000 "Texas Tech", NULL 3 pshc pshd pshx pshy strlen NULL org ldd jsr $2000 #mystr [strlen, pcr] ; push parameters ; Pointer to strlen subroutine address ; Null ; The only parameter is the string address ; That is how we call a D-bug12 function swi end ; call subroutine ; release memory for parameters 2 Dr. Hamed Mohsenian-Rad puly pulx puld pulc ECE 3362: Microcontrollers – Spring 2011 • Q: W here did $EE98 come from? • Q: what are the values of A and B after running this code? Texas Tech University 3-55 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-56 14 2/5/2011 STRLEN TOUPPER • Q: What does this program do? strlen NULL equ equ $EE98 $0 mystr yourstr org dc.b dc.b $1000 "Texas Tech", NULL "Rice", NULL org lds $2000 #$3000 ; set the stack pointer ldd jsr 1 • Converts a lowercase character in accumulator B to uppercase . Q: What is the value of Flag N? #mystr [strlen, pcr] ; The only parameter is string address ; That is how we call a D-bug12 function #yourstr [strlen, pcr] ; The only parameter is string address ; That is how we call a D-bug12 function 2 ; Pointer to strlen subroutine address ; Null 2 toupper equ $EE94 c org dc.b $1000 "t" org ldab clra $2000 c jsr 1 [toupper, pcr] 3 3 cba ; convert c to uppercase pula 4 ; set accumulator D for parameter passing ; " swi end pshb ldd jsr ; Pointer to toupper subroutine address ; compare A and B Dr. Hamed Mohsenian-Rad • Q: what are the values in accumulators A and B? [A – B] ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-57 TOUPPER Dr. Hamed Mohsenian-Rad Texas Tech University 3-58 TOUPPER • Here is a program to uppercase a string (1 of 2): toupper NULL equ equ $EE94 $0 mystr i org dc.b dc.b • Here is a program to uppercase a string (2 of 2): ldx ldaa $1000 "Texas Tech", NULL 0 ; Pointer to toupper subroutine address ; Null #mystr i ; set X and A for indexed mode addressing ; “ stab #mystr i ; set X and A for indexed mode addressing ; ldab beq clra A, X ENDP ; set D for parameter, done if NULL ; " ; " jsr [toupper, pcr] ; convert c to uppercase ENDP: ; [B] i 2 ; increment the index bra $2000 ldx ldaa A, X inc org LOOP: ECE 3362: Microcontrollers – Spring 2011 [A+X] LOOP swi end 1 • Q: Why do we need to reload X again in part 2? • Q: Can you think of any alternative approach? Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-59 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-60 15 2/5/2011 PUTCHAR PUTCHAR • Sends the character in accumulator B to Serial Ports / PC. • Example: 1 putchar equ $EE86 org • When using MiniIDE, the character will appear in Terminal Window. $1000 ldd jsr #'T' [putchar, pcr] 2 ; Pointer to putchar subroutine address ; set D to hold character T / ACSII $54 ; send it to Terminal Window swi end Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-61 GETCHAR $EE84 $EE86 org [getchar, pcr] ; Pointer to getchar subroutine address ; Pointer to putchar subroutine address getchar equ putchar equ toupper equ [putchar, pcr] [putchar, pcr] $EE84 $EE86 $EE94 3-62 ; Pointer to getchar subroutine address ; Pointer to putchar subroutine address ; Pointer to toupper subroutine address org $1000 ; read a char from Terminal Window jsr [getchar, pcr] ; ; send a char to Terminal Window ; send a char to Terminal Window jsr [toupper, pcr] ; jsr [putchar, pcr] ; swi end swi end • Q: What if you turn on Caps Lock? • Q: How come there is no instruction involving accumulators? Dr. Hamed Mohsenian-Rad Texas Tech University • Q: What does this program do? $1000 jsr jsr jsr ECE 3362: Microcontrollers – Spring 2011 GETCHAR • Q: What does this program do? getchar getchar equ putchar equ Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-63 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-64 16 2/5/2011 PRINTF PRINTF • It is used to convert, format, and print its arguments. • However, printf can do more. • Let us start with a simple example: 1 equ equ equ equ $0D $0A $0 $EE88 prompt 1 CR LF NULL printf org dc.b $1000 "Texas Tech University", CR, LF, NULL org ldd jsr $1400 #prompt [printf, pcr] 2 ; Carriage return ; Line Feed 2 ; Pointer to printf subroutine address CR LF NULL printf getchar equ equ equ equ equ $0D $0A $0 $EE88 $EE84 prompt org dc.b $1000 "You Typed: %c", CR, LF, NULL org jsr pshd $1400 [getchar, pcr] ldd jsr leas #prompt [printf, pcr] 2, SP 3 ; pointer to the start of the string ; print the string on Terminal Window 4 swi end; Dr. Hamed Mohsenian-Rad Q: What does this program do? ; Carriage return ; Line Feed ; Pointer to printf subroutine address ; Pointer to getchar subroutine address ; ; We use stack to pass the second parameter. ; ; ; release the in stack used for parameter passing swi end ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-65 PRINTF Conversion Characters Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University Example • Some other characters that you can use, just like ‘%c’: • Q: W hat is the printed output if you enter ‘m’? CR LF NULL printf getchar 1 • Examples: prompt equ equ equ equ equ $0D $0A $0 $EE88 $EE84 prompt org dc.b $1000 "You Entered: ASCII: %c, Dec: %d, Hex: %X", CR, LF, NULL org jsr pshd pshd pshd $1400 [getchar, pcr] ldd jsr leas #prompt [printf, pcr] 6, SP 1 dc.b “%d + %d = %d", CR, LF, NULL 3 prompt dc.b 3-66 ; Carriage return ; Line Feed ; Pointer to printf subroutine address ; Pointer to getchar subroutine address ; push d ; push d ; push d “You Entered: ASCII: %c, Dec: %d, Hex: %X", CR, LF, NULL end Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-67 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-68 17 2/5/2011 Example Example • Q: W hat is the printed output? Q: Does it look correct? printf equ $EE88 prompt org dc.b • The parameters need to be pushed to the stack in a reversed order. $1000 "%d + %d = %d", $0D, $0A, 0 ; Pointer to printf subroutine address org $1400 ldd pshd ldd pshd ldd pshd #2 ; push #2 #3 ; push #5 #3 ; push #3 #2 ; push #2 ; push #5 ldd jsr leas #5 ; push #3 #5 ldd pshd ldd pshd ldd pshd #prompt [printf, pcr] 6, SP • Because the implementation of the printf function is easier that way. swi end Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-69 #INCLUDE Command Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-70 Calling D-Bug12 Functions • So far we have defined several constant terms using EQU: getchar putchar printf toupper strlen equ equ equ equ equ $EE84 $EE86 $EE88 $EE94 $EE98 ; Pointer to getchar subroutine address ; Pointer to putchar subroutine address ; Pointer to printf subroutine address ; Pointer to toupper subroutine address ; Pointer to strlen subroutine address CR LF NULL equ equ equ $0D $0A $0 • Why do we call the printf function like this? ; Carriage return ; Line Feed jsr [printf, pcr] • Recall that: Subroutine printf () printf Function Makes formatted string equ $EE88 Pointer Address $EE88 ; Pointer to printf subroutine address • But $EE88 is NOT the address for the code for subroutine printf. • Alternatively, we can use this command: • Instead, the address of subroutine printf is available at $EE88~$EE89. #include "DEBUG12V4EQU.inc“ ; it already includes all these EQU lines That is why we call it “Pointer Address” not “Address”. Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-71 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-72 18 2/5/2011 Calling D-Bug12 Functions Calling D-Bug12 Functions • Therefore, we shall either use the final address: • Pointer Address Contents: getchar $DB77 $DB78 .. $DB79 .. .. $DFA9 .. $DFA8 DB .. .. B2 $EE86 $EE87 • Or alternatively, we can use indexed indirect addressing mode: putchar 77 $EE88 DF $EE89 ; getchar ; puchar ; printf $DBB2 $DB77 $DFA8 .. $DFA8 $EE85 .. $DBB4 DB jsr jsr jsr .. $DBB3 $EE84 $DBB2 A8 jsr jsr jsr [getchar, pcr] [putchar, pcr] [printf, pcr] ; jump to the addressed pointed by $EE84 ; jump to the addressed pointed by $EE86 ; jump to the addressed pointed by $EE88 printf Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 • PCR is an assembly directive that obtains the value of Program Counter for the immediately next instruction. Texas Tech University 3-73 Calling D-Bug12 Functions • Consider the following code: Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-74 Calling D-Bug12 Functions org jsr $1000 [getchar, pcr] • Now assume that the program is running. Once “jsr [$DE84, PC]” is executed, PC will point to the immediately next instruction. ; 4 Bytes PC = $1004. • In this case, pcr = $1004. • Thus, the program will jump to where ever the content of address $DE84 + $1004 = $EE84 is pointing to. • Seeing instruction “jsr [getchar, pcr]” the asembler will create an indexed indirect addressing mode with the following elements: Base register: PC $DE84 + $1004 Offset: getchar – pcr = $EE84 - $1004 = $DE84 $DBB2 …. $EE84 $DBB2 • Therefore, what will actually be compiled to machine code is jsr Dr. Hamed Mohsenian-Rad • Q: Why do we need to go for this kind of indirect indexed addressing? [$DE84, PC] ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-75 Dr. Hamed Mohsenian-Rad ECE 3362: Microcontrollers – Spring 2011 Texas Tech University 3-76 19 ...
View Full Document

This document was uploaded on 10/27/2011 for the course EE 3362 at Texas Tech.

Ask a homework question - tutors are online