Introduction to Subroutines

26
Introduction to Subroutines

description

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

Transcript of Introduction to Subroutines

Page 1: Introduction to Subroutines

Introduction to Subroutines

Page 2: Introduction to Subroutines

All the combinations in which a subroutine can be written

1. The subroutine may be: a. Internal or b. External

2.  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++)

Page 3: Introduction to Subroutines

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

Page 4: 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

Page 5: Introduction to Subroutines

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 orDX:AX, or EDX:EAX

Page 6: Introduction to Subroutines

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.

Page 7: Introduction to Subroutines

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

Page 8: Introduction to Subroutines

Internal Example (Cont.)

SUB1 PROC ; X is BX, Y is in CX SUB BX, CX SUB BX, CX MOV AX, BX RETSUB1 ENDP END MAIN3

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

Page 9: Introduction to Subroutines

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

Page 10: Introduction to Subroutines

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

Page 11: Introduction to Subroutines

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 debugusing: cv main3

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

Page 12: Introduction to Subroutines

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.)

Page 13: Introduction to Subroutines

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 ?

Page 14: Introduction to Subroutines

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

Page 15: Introduction to Subroutines

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

Page 16: Introduction to Subroutines

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

Page 17: Introduction to Subroutines

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

Page 18: Introduction to Subroutines

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.

Page 19: Introduction to Subroutines

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.

Page 20: Introduction to Subroutines

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.

Page 21: Introduction to Subroutines

Using Memory Models The “.model” statement sets the

default attribute for procedure calls:

For instance, for “.model small” the default is NEAR procedures 

Page 22: Introduction to Subroutines

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.

Page 23: Introduction to Subroutines

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.

Page 24: Introduction to Subroutines

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.

Page 25: Introduction to Subroutines

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

Page 26: Introduction to Subroutines

Textbook Reading (Jones):

Chapter 6  Subprograms

Homework. Write 2 programs on the computerto evaluate X + Y – Z employing an external subroutine, and (a) passing arguments thru registers (b) passing arguments via the PUBLIC and EXTRN declarations