ELEC 2220 Computer Systems

1 / 12

# ELEC 2220 Computer Systems - PowerPoint PPT Presentation

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.

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

## PowerPoint Slideshow about 'ELEC 2220 Computer Systems' - azra

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

Chapter 4. Modular Programming

Soo-Young Lee

Department of Electrical and Computer Engineering

Auburn University

ELEC2220 Auburn University

4. Modular Programming: Chapter Objectives
• Subroutine
• Subroutine-related instructions
• Parameter Passing

ELEC2220 Auburn University

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. Subroutines
• 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. 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:
• dbne b,next
• rts

ELEC2220 Auburn University

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.
• SP  SP – 2
• Initiate execution of the subroutine
• PC  PC + distance ; relative addressing
• 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 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 Instructions
• Subroutine Sequence

Jsr subr

….

subr

….

rts

….

1. Save PC

SP

SP

Stack

Stack

3. Return

2. Initiate execution of subr

ELEC2220 Auburn University

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 Passing

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

bge done\@

nega

done\@

ENDM

movb #\$90,var1

Movb #\$30,var2

sum_abs var1,var2

ELEC2220 Auburn University

4-6. Chapter Summary
• Modular programming has several advantages.