outline
play

Outline 2.1 Assembly language program structure 2.2 Data transfer - PowerPoint PPT Presentation

Outline 2.1 Assembly language program structure 2.2 Data transfer instructions 2.3 Arithmetic instructions 2.4 Branch and loop instructions 2.5 Shift and rotate instructions 2.6 Boolean logic instructions 2.7 Bit test and manipulate


  1. Outline 2.1 Assembly language program structure 2.2 Data transfer instructions 2.3 Arithmetic instructions 2.4 Branch and loop instructions 2.5 Shift and rotate instructions 2.6 Boolean logic instructions 2.7 Bit test and manipulate instructions 2 .8 Stack 2.9 Subroutines

  2. - Stack is a section of the memory reserved for special use. - Elements can be accessed (read/ write) from only its top - Last-in-first-out (LIFO) data structure new top Stack $1005 EF $1006 B1 $1006 B1 Read: $EF $1007 F2 $1007 F2 $1008 A5 $1008 A5 $1007 F2 Much like a variable-length array $1008 A5 $1006 FC $1007 F2 $1008 A5 2 - 97

  3. How the stack can be im plem ented? - The stack pointer (SP) points at the top byte of the stack - A stack grows from a high address toward lower address - Push operation adds a new item on the top of the stack (write). 1- SP is decremented by the number of bytes to be written to point to the new top of the stack 2- Write on the location pointed by SP - Pull operation reads (= remove) an element from stack. 1- Read the byte (or word) pointed by SP . 2- SP is incremented by the number of bytes read to point to the new top of the stack Push (write) $1005 $1006 B1 SP $1007 F2 Pull (read) $1008 A5 2 - 98

  4. Pushing and pulling a byte $1005 EF $1006 B1 SP=$1006 $1007 F2 $1008 A5 Initial stack 1- A = [SP] = EF $1005 EF SP = $1005 2- SP = SP +1 = $1006 $1006 B1 A new top of stack $1007 F2 $1008 A5 $1004 C1 SP=$1004 $1005 EF SP points at the top of stack SP = $1005 $1006 B1 $1007 F2 $1008 A5 1- SP = SP - 1 = $1004 2- [SP] = A = C1 A new top of stack 2 - 99

  5. Pushing and pulling a word $1005 EF $1006 B1 $1007 F2 SP $1008 A5 Initial stack 1- X = [SP]: [SP+1] = EFB1 $1005 EF SP 2- SP = SP + 2 = $1007 $1006 B1 A new top of stack $1007 F2 $1008 A5 SP points at the top of stack SP = $1005 SP $1003 3F $1004 5C $1005 EF $1006 B1 1- SP = SP - 2 = $1003 $1007 F2 2- [SP]: [SP+1] = X = 3F5C $1008 A5 A new top of stack 2 - 100

  6. - Push and Pull instruction (except for Pulc) do not affect on CCR bits - Push and Pull all the registers except the SP . - There is no push or pull for m em ory 2 - 101

  7. Example: What is the stack contents after the execution of the following instruction sequence. lds #$1006 ;SP = 1006 initial value ldaa #$20 ; A = $20 psha ;SP = SP -1 = 1005 and [SP] = A ldab #$28 ; B = $28 pshb ;SP = SP -1 = 1004 and [SP] = B ldx #$FF3A ; X = $FF3A pshx ; SP = SP - 2 = 1002 and [SP] = X SP (= $14FC) SP = $1002 $1002 0 $FF 0 $1003 $3A lds instruction is used to initialize SP $28 $1004 at the beginning of a program $20 $1005 $1006 2 - 102

  8. Another example: See how SP and the stack contents change after push and pull operations. From the example, observe 1- The stack returns to its initial contents w hen w e pull the sam e am ount of data w e pushed 2- W hen the order of the pull operations is the opposite of those of the push operations, the registers get their initial values. If you need to use registers but you do not want to lose their contents, save them in stack, use then and then return the original values. 2 - 103

  9. Com m on uses of stacks 1- Temporary data storage - The number of the registers is very limited. - If you want to use a register but you need its value for a later use, you can save the value temporarily on the stack and retrieve it later. See previous slide psha ;save the contents of A ; use A in computations pula ;restore the initial content of A 2- Subroutine calls The details will be in the next section. 3- Swap and reverse the order of data Stack contents are retrieved in the reverse order from which they were placed onto the stack. psha pshb Swap a and b pula pulb 2 - 104

  10. The memory is used for data, program and stack. If you push or pull too many, the processor writes in locations outside the area allocated to the stack  the program may crash because you mistakenly write in the area of the data or the program Memory management is the responsibility of the programmer No hardware enforces the stack boundary It is the programmer responsibility to keep the SP within the stack area 2 - 105

  11. Outline 2.1 Assembly language program structure 2.2 Data transfer instructions 2.3 Arithmetic instructions 2.4 Branch and loop instructions 2.5 Shift and rotate instructions 2.6 Boolean logic instructions 2.7 Bit test and manipulate instructions 2.8 Stack 2 .9 Subroutines

  12. What is a Subroutine? - It is usual that a sequence of instructions needs to be executed in several places in the program. - If we write the same code each time it is needed the program size will be large. - Subroutine is a group of instructions that is written in memory only once and can be called (executed) multiple times from anywhere in the program. Why subroutines? - Save memory - Improve reusability of code: Subroutines can be used in different programs. - Better organization/ readability: A complicated program can be more organized with subroutines. However, program execution time increases. 2 - 106

  13. Program memory A sequence of . instructions . (1) call . . . . (3) A mechanism is need to: . 1- Call . 2- Return . 3- Pass data call 4- Return data (results) . . (5) . . subroutine (4) (2) Return Without subroutines The program flow with Write the code each using a subroutine time it is needed 2 - 107

  14. 1- Calling a subroutine bsr subroutine_label ;branch to subroutine The subroutine location should be in the range of -128 to + 127 bytes from the instruction immediately after bsr jsr subroutine_label ;jump to subroutine The subroutine location can be anywhere in the memory How bsr and jsr work? 1- Save the return address that points to the next instruction after bsr or jsr. Push PC onto the stack. PC contains the address of the instruction following the jsr or bsr (the return address) 2- jump to the subroutine. Load PC with the subroutine starting address At the beginning of a subroutine, the top of the stack alw ays contains the return address 2 - 108

  15. 2- Returning from a subroutine rts ;Return from subroutine - The last instruction in a subroutine. - It returns to the instruction immediately after the calling instruction - Restore the return address from stack and load it in PC W hen executing rts, the top of stack should contain the returning address, otherw ise the program w ill crash. To ensure that: the am ount of pushed data in a subroutine should equal the am ount of pulled data SP PCH The stack after executing bsr and before executing rts PCL -- PCH is the most significant byte in PC -- PCL is the least significant byte in PC 2 - 109

  16. A good program m ing habit that can reduce the chance of errors Sub2: psha Sub1 does not pshb change the Store the register bsr Sub1 pshc values stored in (and memory) you pushx flags, registers use in the subroutine or memory ldda $1000 psha Local variable: Use A, B, the Notice the order flags, X and location $1000 This can be done in the program instead of the subroutine. Use pula push instructions before bsr and sta $1000 Restore original values pulx pull instructions after bsr. before returning pulc pulb Pula rts 2 - 110

  17. 3- Passing/ returning data - Subroutines usually expect inputs from the caller of the subroutine, and the caller usually expects the subroutine to return certain results. Parameter passing techniques: 1- Passing parameters using memory: The caller sets memory locations and the callee reads them Example: movb #2,$1000 The caller stores the data in memory locations, movb #4,$1001 e.g., $1000 and $1001 bsr addition addition: The subroutine reads the data from memory ldaa $1000 locations, e.g., $1000 and $1001 adaa $1001 rts 2 - 111

  18. 2- Passing parameters using registers The caller sets registers and the callee reads them Example: ldaa #2 ldab #4 The caller stores the data in registers, e.g., a and b bsr addition addition: The subroutine reads the data from the registers aba rts Param eter returning techniques: 1- Using memory: The subroutine places the results in memory and the caller reads them. 2- Using registers: The subroutine places the results in registers and the caller reads them. 2 - 112

  19. Example: Draw the stack for the following program segment (6) ldd #$1234 (1) pshd (5) ldx #$4000 (2) pshx (4) jsr sub_xyz (3) … (3) (4) sub_xyz: pshd (2) (5) pshx pshy (6) (1) … Rem em ber: w hen executing rts, the top of the stack m ust be the return address 2 - 113

  20. Example on using lookup tables Write a subroutine “Square” to square an input number. The number to be squared (assumed unsigned) is passed in A. The result is returned in A. If the result is too big to fit in A, return $FF in A What is lookup table? Pre ‐ calculate and store all the squared values (not many), and then look up the correct answer based on the input number Lookup tables applications Very useful when the function to be computed is very complicated (e.g., sine, log, … ), or represents some calibration values (e.g., temperature as a function of voltage) 1- Calculate the lookup table values 2- Using indexed addressing, it is easy to get the right value from the table. If X = the starting address of the table, and A = the input number (i.e., 0 to 15), then ldaa a,x will fetch the square value 2 - 114

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend