Search code examples
assemblytasm

Tasm procedure declaration


What is a difference between these two declarations(not sure if I'm using right word here) of procedures in TASM:

procName proc

and

procName proc near

Solution

  • If you don't specify a distance (with NEAR or FAR in the procedure declaration) the default is inferred from the current model.

    For TINY, COMPACT and SMALL models the default distance is NEAR. For all other models FAR is the default.

    This is true only if you use the simplified segmentation directives (e.g. .CODE, .DATA, .STACK) otherwise NEAR is always assumed.
    You can also specify NEAR or FAR in the .MODEL directive.

    You can override the distance of a procedure by specifying NEAR or FAR in its declaration.


    Specify the distance of a procedure automates the generation of two instructions: the rets used inside the procedure and the calls used to invoke it.

    proc1 PROC NEAR
       ret                 ;This generates the C3 opcode (near return)
    proc1 ENDP
    
    proc2 PROC FAR
       ret                 ;This generates the CB opcode (far return)
    proc2 ENDP
    
    call proc1             ;This generates opcode E8 (call near relative direct)
    call proc2             ;This generates opcode 9A (call far absolute direct)
    

    You can always be explicit by using the retf and retn instructions and using the call NEAR PTR proc1, call FAR PTR proc2 specifiers.


    When the assembler encounters a call to a procedure declared later in the source code (technically said forward declared) it must use multiple passes to resolve the call.
    It first assumes it is a near call, when it encounters the declaration of the called procedure and its distance is not near the assembler need another pass to correct its guess and generate a far call instead.

    Multiple passes can be enabled with the /m switch, otherwise in such situations you will encounter the error

    forward reference needs override.


    I was unable to find a PDF version of the TASM 5 manual online, the only source is this scanned version of the manual.
    Chapter 10 (at page 128 of the pdf, 115 of the printed copy) is dedicated to the procedures declaration.