Subroutines … passing data

Slides:



Advertisements
Similar presentations
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Advertisements

The University of Adelaide, School of Computer Science
Lecture 9: MIPS Instruction Set
680XX Program Examples Outline –Binary to BCD –Matrix Addition –Ones Count –String Compare –Sector Map –Raster Graphics –Subroutine Calls Goal –Understand.
The University of Adelaide, School of Computer Science
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
Procedures and Control Flow CS351 – Programming Paradigms.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
EECC250 - Shaaban #1 lec #8 Winter Recursive Subroutine Calls Example The purpose of this example is to examine how all parameters, local variables,
Chapter 8 :: Subroutines and Control Abstraction
CPE/EE 421 Microcomputers: Motorola 68000: Assembly Language and C Instructor: Dr Aleksandar Milenkovic Lecture Notes.
7/23 C Programming in Embedded Systems Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Assembly Language Macros Most assemblers include support for macros. The term macro refers to a word that stands for an entire group of instructions. Macro.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
9/20/6Lecture 3 - Instruction Set - Al1 Program Design Examples.
Functions in Assembly CS 210 Tutorial 7 Functions in Assembly Studwww.cs.auckland.ac.nz/ ~mngu012/public.html/210/7/
Addressing Modes1 Addressing modes are concerned with how the CPU accesses the operands used by its instructions.
ECE Lecture 21 Typical Assembly Language Program Bugs.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
1 Subroutines Advanced Programming. 2 Top-Down approach to problem solving Algorithm step by step description of how to solve a problem Divide and Conquer.
SPACE COMPLEXITY & TIME COMPLEXITY 1. ALGORITHMS COMPLEXITY 2.
C Calling Conventions parameters are passed on the run-time or system stack, SP (or A7) parameters pushed on stack in “right to left” order of call A6.
Functions.
Computer Science 210 Computer Organization
Figure 8.1 of course package
e. g. Write a program to count the spaces in a string
Principles of programming languages 4: Parameter passing, Scope rules
Procedures (Functions)
Stack Frames Stack frame = block of memory located in the system stack that contains: return address input parameters (from calling program to subroutine)
Lecture 3 - Instruction Set - Al
Subroutines … a 1st look procedures and functions in high level languages are modeled on subroutines typically, assembly code is very modular with.
Chapter 9 :: Subroutines and Control Abstraction
Interfacing with I/O Devices
CPE/EE 421 Microcomputers: Motorola 68000: Assembly Language and C
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSC 533: Programming Languages Spring 2015
Passing Parameters Data passed to a subroutine is called a parameter.
September 5 Fang-Yi’s Office Hours Friday 10am  12pm (and another TBD) Programming Language Issues (JAVA vs. C, Pointers vs. Arrays) Representations Instructions.
The University of Adelaide, School of Computer Science
Subroutines – parameter passing
Self-modifying Code program intentionally modifies code in the program by overwriting machine code may be used by code designers to obscure branch addresses.
Reentrant Code a reentrant procedure can have several calls open to it at the same time in an interrupt environment, different ISRs may call the same routine.
; main program ; main move Param2, -(sp) move Param1, -(sp) Call Sub1
EECE.3170 Microprocessor Systems Design I
Write a program to calculate x**y, given x and y.
EECE.3170 Microprocessor Systems Design I
Figure 8.1 of course package
Topic 3-a Calling Convention 1/10/2019.
Logical Operations bitwise logical operations AND, OR, EOR, NOT
Subroutines … passing data
Addressing Modes Immediate #<data> or Imm
Basic Instruction Cycle
Developing a bicycle speed-o-meter
Assembly Language Programming
Chapter 9: Pointers and String
Where is all the knowledge we lost with information? T. S. Eliot
Introduction Lab1 A crash course in assembler programming
CSC 533: Programming Languages Spring 2018
Lecture 3 - Instruction Set - Al
CSC 533: Programming Languages Spring 2019
ECE511: Digital System & Microprocessor
Lecture 3 - Instruction Set - Al
Presentation transcript:

Subroutines … passing data Mechanisms: pass by value pass by result pass by value result/ value return/ copy restore pass by reference pass by name pass by lazy evaluation … Techniques: in registers in memory locations in a parameter block on stack in the code stream

Passing data in registers simple programs with few parameters complex programs, e.g. operating systems, with an established standard or ‘parameter convention’ usually, the low numbered registers, e.g. D0, D1, … etc, are used

Passing data in registers e.g. variable delay routine … move.w #5,D0 ; init delay count jsr v_delay ; and call delay stop #$2700 * Insert comments for subroutine here v_delay move.l D0,-(SP) ; save context d_loop nop dbra D0,d_loop ; kill some time move.l (SP)+,D0 ; restore context rts What registers should be saved? Who decides? Why not save all registers all the time?

e.g. find median v1 lea data,A0 clr.l D0 move.b data_l,D0 jsr find_median move.b D1,median stop #$2700 * Preconditions: * A0 – pointer to list * D0 – list count (byte) * Postconditions: * D1 – median (byte) find_median: movem.l D0/A0,-(sp) lsr.b #1,D0 scs flag add.l D0,A0 move.b (A0),D1 tst.b flag bne done add.b -(A0),D1 lsr.b #1,D1 done movem.l (sp)+,D0/A0 rts median ds.b 1 flag ds.b 1 data dc.b 1,3,5,7,8 data_l dc.b 5 data dc.b 1,3,5,7,7,8 data_l dc.b 6 Median is the middle value if odd number of values or the average of the 2 middle values

e.g. find median v2 lea data,A0 clr.l D0 move.b data_l,D0 jsr f_median move.b D1,median stop #$2700 * Same pre/post conditions * for all 3 subroutines * D1 is still output!! f_median: movem.l D0/A0,-(sp) btst #0,D0 bne odd_r jsr even bra done odd_r jsr odd done movem.l (sp)+,D0/A0 rts even lsr.b #1,D0 add.l D0,A0 move.b (A0),D1 add.b -(A0),D1 lsr.b #1,D1 rts odd lsr.b #1,D0 add.l D0,A0 move.b (A0),D1 rts data dc.b 1,3,5,7,7,8 data_l dc.b 6 median ds.b 1

e.g. find median v3 lea data,A0 clr.l D0 move.b data_l,D0 jsr f_median move.b D1,median stop #$2700 * Same pre/post conditions * for all 3 subroutines * D1 is still output!! f_median: movem.l D0/A0,-(sp) lsr.b #1,D0 bcs odd_r jsr even bra done odd_r jsr odd done movem.l (sp)+,D0/A0 rts even add.l D0,A0 move.b (A0),D1 add.b -(A0),D1 lsr.b #1,D1 rts odd add.l D0,A0 data dc.b 1,3,5,7,7,8 data_l dc.b 6 median ds.b 1

Passing data in memory OK for simple programs e.g. test programs, one-time use severely limits how a subroutine is called unusual in commercial code makes the procedure non-reentrant only one instance possible

Passing data in memory e.g. variable delay routine … move.w #5,delay_cnt ; init delay count jsr v_delay ; and call delay stop #$2700 * Comments for subroutine here v_delay move.l D0,-(SP) ; save context move.w delay_cnt,D0 ; init count d_loop nop dbra D0,d_loop ; kill some time move.l (SP)+,D0 ; restore context rts delay_cnt ds.w 1

Passing data in parameter blocks used for large amounts of data stored in arrays or data structures pass the address of the data structure for complex data, i.e. a table containing data and pointers pass the address of a parameter block typically, the address is passed in an address register usually, the low numbered registers, e.g. A0, A1, … etc, are used

Passing data in parameter blocks e.g. add increment to every element in an array … move.w #5,arr_inc ; init increment lea array,A0 ; init table pointer jsr inc_lst ; and do variable incr … stop #$2700 inc_lst movem.l ; save context move.w ; get increment move.w ; and length . inc_lp . ; increment element dbra ,inc_lp ; and loop til done movem.l ; restore context rts array equ * arr_inc ds.w 1 arr_l dc.w 100 arr_d ds.w 100

Mechanisms for Passing Data pass by value used in C, Java, Pascal, Ada (in param) copy of argument passed to S/R infamous swap example void swap (int x, int y) { int t = x; x = y; y = t; } int main(void) { int a = 5; int b = 7; swap( a, b ); printf("a= %d; b= %d\n", a, b);

Mechanisms for Passing Data pass by reference used in Fortran, Pascal (var param), Delphi, C++, C (simulates pass by reference with pointers) address of argument passed to S/R "reference" is not the same as Java "reference"; Java is passing references to objects by value void swap ( int *px, int *py) { int t = *px; *px = *py; *py = t; } int main(void) { int a = 5; int b = 7; swap( &a, &b ); printf("a= %d; b= %d\n", a, b);

Mechanisms for Passing Data pass by sharing / pass by object used in Python where all data are objects with an identity (essentially an address), a type and a value. immutable objects behave like pass by value; mutable objects behave like pass by reference pass by value result / value return / copy restore used in Ada (in/out param), Fortran address of argument passed to S/R S/R makes a copy of argument when S/R is finished, copies the argument copy back to original parameter

Reading, Expectations Reading: Expectations: M68000 Assembly Language [pdf, 92p; N. Znotinas] review operation of JSR (BSR) and RTS instructions covered in presentation Expectations: you can explain the operation of JSR/BSR, RTS you can read/write code that uses the above instructions you can read/write code that passes data to a subroutine via registers, via memory locations and using parameter blocks