Introduction to subroutines
This presentation is the property of its rightful owner.
Sponsored Links
1 / 26

Introduction to Subroutines PowerPoint PPT Presentation


  • 73 Views
  • Uploaded on
  • Presentation posted in: General

Introduction to Subroutines. All the combinations in which a subroutine can be written. 1. The subroutine may be: a. Internal or

Download Presentation

Introduction to Subroutines

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


Introduction to subroutines

Introduction to Subroutines


All the combinations in which a subroutine can be written

All the combinations in which a subroutine can be written

1. The subroutine may be:

a. Internal or

b. External2.  The type of CALL employed can be:    a.  NEAR (pushing the return address and flags onto the stack)    b.  FAR (as above, but in addition pushing the code segment reg)  3.  Arguments can be passed:    a.  through registers (as used by INT subroutine calls)    b.  using EXTRN and PUBLIC (global variables)    c.  using the stack (as used by all compilers)

When using the stack to pass arguments:4.  arguments can be pushed onto the stack:    a.  from left to right (as they occur in the source program)    b.  from right to left

5. a. the value of the arguments can be pushed (call by value)

b. the offsets of the arguments can be pushed (call by reference)6.  The stack can be restored to its condition before the call by:    a.  the subroutine (as in Visual Basic)    b.  the calling program (as in C/C++)


Subroutine in c

Subroutine in C++

For example, C++ uses:    External subroutines    Call type NEAR

Call by value

Passes arguments using the stack        Pushes arguments from right to left    The calling program restores the stack


Introduction to subroutines

In contrast, most other commercial compilers (such as Visual Basic, Pascal, etc) pass their arguments from left to right, and

expect the fixup of the stack to its condition before the call to be performed by the stack


Examples

Examples

Using a subroutine to evaluate X - 2*Y

Note:  this is a function, since it returns a value.

For all compilers, the convention is to put

the answer (return value) into AL, AX, EAX or

DX:AX, or EDX:EAX


Method 1 passing arguments in registers

Method 1: Passing Arguments in Registers

  • Passing arguments in registers is a convenient and efficient

  • way to pass values between procedures.

  • Preserving Registers.   Subroutines should save and

  • restore the values stored in registers, except for registers

  • that will be used to return values to the calling procedure.

  • This convention is needed to make sure that subroutines

  • do not cause unexpected errors after control returns to the

  • calling procedure.


Internal example

Internal Example

TITLE INTERNAL EXAMPLE: X – 2*Y

.MODEL SMALL

.STACK 100H

.DATA

X DW 20

Y DW 3

Z DW ?

.CODE

MAIN3 PROC

MOV AX, @DATA

MOV DS, AX

MOV BX, X

MOV CX, Y

CALL SUB1

MOV Z, AX

MOV AX, 4C00H

INT 21H

MAIN3 ENDP


Internal example cont

Internal Example (Cont.)

SUB1 PROC ; X is BX, Y is in CX

SUB BX, CX

SUB BX, CX

MOV AX, BX

RET

SUB1 ENDP

END MAIN3

Note: in the internal form, the entire program is all in one file.


External example

External Example

TITLE EXTERNAL EXAMPLE: X – 2*Y

EXTRN SUB1:NEAR

.MODEL SMALL

.STACK 100H

.DATA

X DW 20

Y DW 3

Z DW ?

.CODE

MAIN3 PROC

MOV AX, @DATA

MOV DS, AX

MOV BX, X

MOV CX, Y

CALL SUB1

MOV Z, AX

MOV AX, 4C00H

INT 21H

MAIN3 ENDP

END MAIN3 ; this is the end of MAIN3.ASM


External example cont 1

External Example (Cont. 1)

TITLE SUB1 in a separate file sub1.asm

PUBLIC SUB1

.MODEL SMALL

; no stack segment

; data segment is optional

.CODE

SUB1 PROC NEAR

SUB BX, CX

SUB BX, CX

MOV AX, BX

RET

SUB1 ENDP

END ; NOTE this is NOT: END SUB1


External example cont 2

External Example (Cont. 2)

To assemble and link the above:

ml main3.asm sub1.asm

will produce: main3.exe

To debug the above:

masm /zi main3;

masm /zi sub1;

link /co main3 sub1;

This produces main3.exe, but also allows you to debug

using: cv main3

From now on, we'll only do external examples.


Method 2 using extrn and public to pass the values of the arguments

Method 2:  Using EXTRN and PUBLIC to pass the values of the arguments

Variables can be used as global variables in

assembler programs by declaring them as EXTRN

(if defined in another module) or as PUBLIC

(if defined in the current module and globally

available for use by other modules.)


Example of method2

Example of Method2

TITLE MAIN3 USING GLOBAL VARIABLES: X – 2*Y

; comments to describe program

EXTRN SUB1:NEAR ; SUB1 comes from another file

PUBLIC X, Y ; makes X and Y accessible

.MODEL SMALL

.STACK 100H

.DATA

X DW 20

Y DW 3

Z DW ?


Example of method2 cont 1

Example of Method2 (Cont. 1)

.CODE

MAIN3 PROC

MOV AX, @DATA

MOV DS, AX

CALL SUB1

MOV Z, AX

MOV AX, 4C00H

INT 21H

MAIN3 ENDP

END MAIN3 ; this is the end of MAIN3.ASM


Example of method2 cont 2

Example of Method2 (Cont. 2)

TITLE SUB1 in a separate file sub1.asm

; comments to describe SUB1 module

PUBLIC SUB1 ; SUB1 can be used by other modules

EXTRN X:WORD, Y:WORD ; X and Y come from external module

.MODEL SMALL

; no stack segment

; data segment is optional

.CODE

SUB1 PROC NEAR

MOV AX, X

SUB AX, Y

SUB AX, Y

RET

SUB1 ENDP

END ; end of SUB1.ASM


Terminology

TERMINOLOGY

  • function - a subroutine that returns a result

  • subroutine - any block of instructions that can be called (with a return) from another place

  • procedure - same as subroutine


Proc and endp directives

PROC and ENDP Directives

  • PROC marks the beginning of a procedure

  • ENDP marks the end of a procedure

  • Both are used with the label that names the beginning of the procedure


Near and far procedures 1

NEAR and FAR Procedures (1)

NEAR Calls

  • Offset of instruction to transfer to is in the same code segment

  • When the CALL instruction is executed,

    . the return address (i.e., address of next instruction to be executed) is pushed onto stack,

    . then IP is reset to the address of the subroutine.

  • When the RET instruction is executed,

    . the return address is popped from the stack into the IP register and control thus automatically returns to the next statement to be executed.


Near and far procedures 2

NEAR and FAR Procedures (2)

FAR Calls

  • The instruction to transfer to can be in a different code segment

  • When the CALL instruction is executed,

    . the full return address (CS and IP) is pushed onto the stack,

    . then the CS and IP registers are reset to the address of the subroutine.

  • When the RET instruction is executed,

    . the return address (IP and CS) are popped from the stack, then control automatically returns to the next statement to be executed.


Near and far procedures 3

NEAR and FAR Procedures (3)

When FAR Calls are needed:

  • If the subroutine will be called by a high-level language that requires FAR calls.

  • If the program is very large, so that the subroutine has to be in a separate code segment.

  • If the program calls a library routine that was assembled for FAR calls.


Using memory models

Using Memory Models

  • The “.model” statement sets the default attribute for procedure calls:

  • For instance, for “.model small”

    the default is NEAR procedures


Terminology for program modules 1

Terminology for Program Modules(1)

  • Using separate files (modules) makes building large projects easier

  • The projects can be written, assembled, and tested separately.

  • Separate modules can use the same names for variables and statement labels because the names can be local to the file and not conflict with other modules.


Terminology for program modules 2

Terminology for Program Modules(2)

  • Assembly module:

    An .ASM file that contains at least one segment definition.

  • Object module:

    A binary file produced in "object code format" when the assembler translator converts the assembly source file into a machine language file. An object module has the .OBJ file extension.

  • Executable file:

    The .EXE file that is produced by the linker, which combines the contents of the separate .OBJ files and any library files to produce the actual executable program.


Terminology for program modules 3

Terminology for Program Modules(3)

  • NEAR Procedure:

    A procedure is NEAR if its code appears in the same code segment as the procedure that called it.   When a NEAR procedure is CALLed, the IP (2 bytes) containing the return address for the program is pushed onto the stack.

  • FAR Procedure:

    The code for a FAR procedure may appear in a different (code) segment from the procedure that called it. When a FAR procedure is CALLed, the CS:IP (4 bytes) containing the return address for the program is pushed onto the stack.

    A procedure must have the FAR type if it cannot fit into the same memory segment as the procedure that called it, or if the procedure will be used by a high-level language program that requires FAR procedures.


Terminology for program modules 4

Terminology for Program Modules(4)

  • EXTRN pseudo-op

    Used to identify externally declared names and labels that will be used in the current program module.

        EXTRN PROC1:NEAR, PROC2:FAR, VAR1:WORD

    declares externally defined names PROC1 (a near procedure), PROC2 (a far procedure), and VAR1 (a word variable).

  • PUBLIC pseudo-op

    Used to identify procedures and variables that will be "public" for use in other modules.  Any procedure names or variable names not made public will be "local" to the program module (file).

        PUBLIC PROC1, PROC2, VAR1


Textbook reading jones

Textbook Reading (Jones):

Chapter 6  Subprograms

Homework. Write 2 programs on the computer

to evaluate X + Y – Z employing an external

subroutine, and

(a) passing arguments thru registers

(b) passing arguments via the PUBLIC

and EXTRN declarations


  • Login