elec 2220 computer systems l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
ELEC 2220 Computer Systems PowerPoint Presentation
Download Presentation
ELEC 2220 Computer Systems

Loading in 2 Seconds...

play fullscreen
1 / 12

ELEC 2220 Computer Systems - PowerPoint PPT Presentation


  • 159 Views
  • Uploaded on

ELEC 2220 Computer Systems. Chapter 4. Modular Programming Soo-Young Lee Department of Electrical and Computer Engineering Auburn University. 4. Modular Programming: Chapter Objectives. Subroutine Subroutine-related instructions Parameter Passing. 4-1. Subroutines.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'ELEC 2220 Computer Systems' - azra


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
elec 2220 computer systems
ELEC 2220 Computer Systems

Chapter 4. Modular Programming

Soo-Young Lee

Department of Electrical and Computer Engineering

Auburn University

ELEC2220 Auburn University

4 modular programming chapter objectives
4. Modular Programming: Chapter Objectives
  • Subroutine
  • Subroutine-related instructions
  • Parameter Passing

ELEC2220 Auburn University

4 1 subroutines
4-1. Subroutines

A large task may be partitioned into blocks, and some of the blocks can be implemented as subroutines (modules), especially those executed repeatedly.

  • Example
    • Finding the sum of all elements in array[N]
  • Issues: task partitioning, parameter passing

Main

  • Why modular programming?
  • easily understand
  • easy debugging
  • localized changes
  • share modules

EA of array, N

Sum

Sum up elements in array[N]

Subroutine

ELEC2220 Auburn University

4 1 subroutines4
4-1. Subroutines
  • Task Partitioning
    • Define the computation to be carried out by each subroutine.
  • Parameter Passing
    • Define the input parameters (constants, variables, etc.) to be passed to the subroutine.
    • Define the results from the subroutine.
    • How are they passed between the calling procedure and the subroutine?

ELEC2220 Auburn University

4 1 subroutines5
4-1. Subroutines
  • Example

N equ 10

org $800

array ds.b 10

total ds.b 1

org $8000

ldx #array

ldab #N

jsr sum

staa total

….

rts

  • sum
  • ldaa #0
  • next:
  • adda 1,x+
  • dbne b,next
  • rts

ELEC2220 Auburn University

4 2 subroutine instructions
4-2. Subroutine Instructions
  • BSR (branch to subroutine)
    • Save the return address (address of the instruction immediately following the BSR) and initiate execution of the subroutine.
    • Saving return address
      • SP  SP – 2
      • M[SP]:M[SP+1]  return address
    • Initiate execution of the subroutine
      • PC  PC + distance ; relative addressing
  • JSR (jump to subroutine)
    • The same as BSR except for the addressing mode
    • Initiate execution of the subroutine
      • PC  address of the subroutine : all other addressing modes

ELEC2220 Auburn University

4 2 subroutine instructions7
4-2. Subroutine Instructions
  • RTS (return from subroutine)
    • Retrieve the return address from the stack
      • PC  M[SP]:M[SP+1]
      • SP  SP + 2
  • Never jump to or out of a subroutine using branch instructions !
  • Don’t forget to initialize SP

ELEC2220 Auburn University

4 2 subroutine instructions8
4-2. Subroutine Instructions
  • Subroutine Sequence

Jsr subr

next instr ; addr_of_next

….

subr

first instr ; addr_of_first

….

rts

….

1. Save PC

Addr_of_next

Addr_of_next

SP

SP

Stack

Stack

3. Return

PC  addr_of_next

2. Initiate execution of subr

PC  addr_of_first

ELEC2220 Auburn University

4 3 parameter passing
4-3. Parameter Passing

4-3-1. Registers

  • Data to be processed are passed in registers to the subroutine.
  • Results are returned in registers to the calling procedure.
  • The number of parameters is limited.

4-3-2. Memory

  • The global shared memory variables are used for parameter passing.
  • Subroutine: variable-name specific

4-3-3. Stack

  • Parameters are passed to and from the subroutine via the stack.
    • Data are pushed onto the stack, which are popped by the subroutine.
    • Results are pushed by the subroutine, which are popped by the calling procedure.
  • Careful with the return address on the stack

ELEC2220 Auburn University

4 3 parameter passing10
4-3. Parameter Passing

4-3-4. Address Passing

  • Addresses of parameters, instead of their values, are passed.
  • Subroutine: variable-name independent
  • Particularly useful when the size of data to be passed is large.
  • Example: finding the maximum in an array

Find_max( aptr, N)

Int *aptr, N;

{

Int temp_max, i;

temp_max = *aptr;

for(i=1; i<N; i++)

if( *(aptr+i) > temp_max )

temp_max = *(aptr+i);

return(temp_max);

}

int array[100], max;

N=100;

max = find_max(array, N);

….

ELEC2220 Auburn University

4 5 macro
4-5. Macro
  • A subroutine may be too costly for a small task due to the call/return overhead.
  • A small task (code) can be defined as a macro which may be considered as a user-defined instruction.
  • An assembler copies the code wherever it encounters the macro.
    • The program flow doesn’t change.
    • The actual program length after assembled is longer than what it appears to be.
  • Example (CodeWarrior)

sum_abs MACRO

ldaa \1

adda \2

bge done\@

nega

done\@

ENDM

movb #$90,var1

Movb #$30,var2

sum_abs var1,var2

ELEC2220 Auburn University

4 6 chapter summary
4-6. Chapter Summary
  • Modular programming has several advantages.
  • Task partitioning
  • BSR, JSR
    • Save the return address and load PC with the address of the subroutine
  • RTS
  • Parameter Passing
    • Registers
    • Memory
    • Stack
    • Address passing
  • MACRO

ELEC2220 Auburn University