Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 - Instruction Set - Al

Similar presentations


Presentation on theme: "Lecture 3 - Instruction Set - Al"— Presentation transcript:

1 Lecture 3 - Instruction Set - Al
Program Design (2) 9/20/6 Lecture 3 - Instruction Set - Al

2 Lecture 3 - Instruction Set - Al
Lecture Overview Have seen the initial part of Top down design Modular design Now Parameter Passing Stack and Local Variables Structured Programming 9/20/6 Lecture 3 - Instruction Set - Al

3 Parameter Passing to Modules
Modules (subroutines) should have no side effects Receive (access) only the data passed to it Modify and store/update only parameters in its formal output list Some subroutines trigger an event such as sounding an alarm, running a pump, etc. 9/20/6 Lecture 3 - Instruction Set - Al

4 Parameter passing example
Subroutine PUT_CHAR displays a single character on the monitor. Main program action Push character on stack Call subroutine BSR PUT_CHAR PUT_CHAR knows the location, and as only 1 byte, the data might be passed in a data register More formal method places the data on the stack It can use 4(A7) or 6(A7) to access the character. 9/20/6 Lecture 3 - Instruction Set - Al

5 Parameter passing in Registers
Works when the quantity of data is small or the processor has many, many registers. Passing arguments in registers is code that is position independent and reentrant. Position independent as no absolute address location involved in data transfer Reentrant as the registers would be saved before they are reused. 9/20/6 Lecture 3 - Instruction Set - Al

6 Mechanisms for Parameter Passing
Ways in which parameters are passed. Pass-by-value: The current value of the variable at the time the subroutine call is made, is passed to the subroutine Pass-by-reference: The address of the variable is passed to the subroutine, i.e., a pointer. This allows modification of the variable back in the calling routine, by the subroutine. Mechanisms Registers - Memory Location - Stack 9/20/6 Lecture 3 - Instruction Set - Al

7 Lecture 3 - Instruction Set - Al
Pass by Reference Subroutine to search a region of memory Text in memory Want to check for sequence of characters Info needed by subroutine Starting and ending address of text Start and ending address of the string Subroutine receives addresses when called Information in call – what string to find Note: Code is not reentrant and cannot be used by an interrupt routine 9/20/6 Lecture 3 - Instruction Set - Al

8 Lecture 3 - Instruction Set - Al
Code example For using the stack to pass by reference. 9/20/6 Lecture 3 - Instruction Set - Al

9 Lecture 3 - Instruction Set - Al
Figures for example Memory contents and The stack 9/20/6 Lecture 3 - Instruction Set - Al

10 Parameter Passing on the Stack
Can use the stack to transfer the actual data Can be used to pass the addresses for pass-by-reference 9/20/6 Lecture 3 - Instruction Set - Al

11 Figures for this example
9/20/6 Lecture 3 - Instruction Set - Al

12 Stack and Local Variables
Subroutines need access to space for local variables Possible that they fit in register but may not Allocate local storage space on stack. Keeps the subroutine reentrant Also bind the variables not only to the subroutine, but to the specific invocation 9/20/6 Lecture 3 - Instruction Set - Al

13 Lecture 3 - Instruction Set - Al
Stack frame Allocation space on the stack LEA -200(SP),SP To restore stack prior to return LEA 200(SP),SP Refer to Fig 3.6 of text. 9/20/6 Lecture 3 - Instruction Set - Al

14 Lecture 3 - Instruction Set - Al
Simplification LINK and UNLINK A instruction to specifically do this MOVE.L D0-D7,/A3-A6, -SP Save the specified registers on the stack LINK A1, #-64 Allocate 64 bytes (16 longwords) of on this stack frame Start subroutine 9/20/6 Lecture 3 - Instruction Set - Al

15 Lecture 3 - Instruction Set - Al
Figures 9/20/6 Lecture 3 - Instruction Set - Al

16 Lecture 3 - Instruction Set - Al
Subroutine example Calculate R=(P2 + Q2)/(P2 – Q2) 9/20/6 Lecture 3 - Instruction Set - Al

17 Lecture 3 - Instruction Set - Al
Link Example The stack 9/20/6 Lecture 3 - Instruction Set - Al

18 Structured Programming
A semiformal method of program development 3 benefits Inproves programmer productivity Program is easier to read Program is more reliable 9/20/6 Lecture 3 - Instruction Set - Al

19 Three fundamental components
These are the basic elements of all high level functional programming languages. Sequence Action A, then action B, then action C, etc. A looping mechanism Finite loop – a set number of iterations Conditional loop – exit when condition occurs A decision mechanism If (condition) THEN action A ELSE action B 9/20/6 Lecture 3 - Instruction Set - Al

20 Lecture 3 - Instruction Set - Al
GO TO???????????????????? All algorithms can be programmed using structured programming techniques without using a GO TO!!! 9/20/6 Lecture 3 - Instruction Set - Al

21 Lecture 3 - Instruction Set - Al
Assignment 4 Problem 2-62 9/20/6 Lecture 3 - Instruction Set - Al


Download ppt "Lecture 3 - Instruction Set - Al"

Similar presentations


Ads by Google