Microcontroller3

Microcontroller3 - Topics • Topics to be covered Stacks...

Info icon This 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 Te...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern