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

outline
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 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

Stack new top

$1005 $1008 $1006 $1007

A5 F2 B1 EF A5 F2 B1

$1008 $1006 $1007

A5 F2 FC

$1008 $1006 $1007

Read: $EF A5 F2

$1008 $1007

Much like a variable-length array

2 - 97

slide-3
SLIDE 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

A5 F2 B1 SP

$1005 $1008 $1006 $1007

Push (write) Pull (read)

2 - 98

slide-4
SLIDE 4

A5 F2 B1 EF SP = $1005

$1005 $1008 $1006 $1007

SP points at the top of stack SP = $1005 Initial stack A5 F2 B1 EF SP=$1006

$1005 $1008 $1006 $1007

1- A = [SP] = EF 2- SP = SP +1 = $1006 A new top of stack A5 F2 B1 EF

$1005 $1008 $1006 $1007

C1 SP=$1004

$1004

1- SP = SP - 1 = $1004 2- [SP] = A = C1 A new top of stack Pushing and pulling a byte

2 - 99

slide-5
SLIDE 5

A5 F2 B1 EF SP

$1005 $1008 $1006 $1007

SP points at the top of stack SP = $1005 Initial stack A5 F2 B1 EF SP

$1005 $1008 $1006 $1007

1- X = [SP]: [SP+1] = EFB1 2- SP = SP + 2 = $1007 A new top of stack 1- SP = SP - 2 = $1003 2- [SP]: [SP+1] = X = 3F5C A new top of stack A5 F2 B1 EF

$1005 $1008 $1006 $1007

5C SP

$1004

3F

$1003

Pushing and pulling a word

2 - 100

slide-6
SLIDE 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

slide-7
SLIDE 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) $28 $20

$1006 $1005 $1004 $1003 $1002 SP = $1002 $3A $FF

lds instruction is used to initialize SP at the beginning of a program

2 - 102

slide-8
SLIDE 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

  • f 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

slide-9
SLIDE 9

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 pula pulb

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 Com m on uses of stacks Swap a and b

2 - 104

slide-10
SLIDE 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

No hardware enforces the stack boundary It is the programmer responsibility to keep the SP within the stack area Memory management is the responsibility of the programmer

2 - 105

slide-11
SLIDE 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

slide-12
SLIDE 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
  • nce 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

slide-13
SLIDE 13

A sequence of instructions

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

(1) (2) (3) (4) (5) Return 2 - 107

slide-14
SLIDE 14

At the beginning of a subroutine, the top of the stack alw ays contains the return address

1- Calling a subroutine

The subroutine location should be in the range of -128 to + 127 bytes from the instruction immediately after bsr 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 bsr subroutine_label ;branch to subroutine jsr subroutine_label ;jump to subroutine

2 - 108

slide-15
SLIDE 15

2- Returning from a 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
  • PCL

PCH SP The stack after executing bsr and before executing rts rts ;Return from subroutine 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

2 - 109

PCH is the most significant byte in PC PCL is the least significant byte in PC

slide-16
SLIDE 16

A good program m ing habit that can reduce the chance of errors

psha pshb pshc pushx ldda $1000 psha Local variable: Use A, B, the flags, X and location $1000 pula sta $1000 pulx pulc pulb Pula rts Store the register (and memory) you use in the subroutine Sub2: Restore original values before returning Notice the order bsr Sub1

Sub1 does not change the values stored in flags, registers

  • r memory

This can be done in the program instead of the subroutine. Use push instructions before bsr and pull instructions after bsr.

2 - 110

slide-17
SLIDE 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: The caller stores the data in memory locations, e.g., $1000 and $1001

movb #2,$1000 movb #4,$1001 bsr addition addition: ldaa $1000 adaa $1001 rts

The subroutine reads the data from memory locations, e.g., $1000 and $1001

2 - 111

slide-18
SLIDE 18

2- Passing parameters using registers The caller sets registers and the callee reads them Example: The subroutine reads the data from the registers The caller stores the data in registers, e.g., a and b

ldaa #2 ldab #4 bsr addition addition: 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

slide-19
SLIDE 19

Example: Draw the stack for the following program segment

ldd #$1234 pshd ldx #$4000 pshx jsr sub_xyz … sub_xyz: pshd pshx pshy …

(1) (2) (3) (4) (5) (6)

Rem em ber: w hen executing rts, the top of the stack m ust be the return address

(1) (2) (3) (4) (5) (6)

2 - 113

slide-20
SLIDE 20

Example on using lookup tables 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 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

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

slide-21
SLIDE 21

ldaa #7 jsr Square ;Call subroutine to compute the square of A ;A = 49, the square of 7

  • Square: pshx

; save the content of X because it used by the subroutine ldx #Square_table ;x = Starting address of the lookup cmpa #15 ;Check if A > 15 bhi tooBig ;Branch if A > 15 ldaa a,x ;A <= 15, so get the result bra _End tooBig: ldaa #$FF ;Signal overflow with $FF _End: pulx rts Square_table dc.b 0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,255 Pseudocode If (A > 15) Load A with $FF Else Load A with the value in the table, at the location indexed by X+ A End

1 4 9 225

  • X+ 0

X+ 1 X+ 2 X+ 3 X+ 15

2 - 115

slide-22
SLIDE 22

string1 fcc “Tennesee Tech University”, 0 string1 fcc “TN Tech University”, 0

  • ldx #string2

ldy #string1 bsr check_strings

  • check_strings:

movb #$FF,$1200 ;Result = FF assume the two strings are identical Loop: ldaa 0,x+ ;a = the first byte of String1, x = x+1 ldab 0,y+ ;b = the first byte of String2, y = y+1 cba bne not_identical ;String1<>String2 if one byte is not equal. ; the loop ends when a = b = 0 cmpa #0 ; enough to check a because a = b beq _end ; if a = 0, b = 0 because they are equal bra Loop not_identical: movb #$00,$1200 ;Result = 00 the strings are different _end: rts

Write a subroutine that returns FF if two strings are identical and 00 if they are not. The beginning addresses of the strings are X and Y and the result is passed at memory location $1200. Each string ends with 0.

2 - 116

slide-23
SLIDE 23

Questions

Mohamed Mahmoud