Modular assembler subroutine (near & far)

1: Near the proximal end use

 C language: 

 
 

#include <stdio.h>
#include <stdlib.h>

void print(){
    printf("proc");
}
int main(int argc, char *argv[]) {
    print();
    return 0;
}

The compilation includes:

datas segment
    x db 'proc$';
datas ends
stacks segment stack
    dw 100 dup(?)
stacks ends
codes segment
assume cs:codes,ss:stacks,ds:datas
main proc ; 主程序
start:
    mov ax,datas;
    mov ds,ax;
    call max 
    mov ah,4ch;
    int 21h
main endp
print proc near ;子程序
    push bp;
    mov bp,sp;
    mov dx,offset x;
    mov ah,09;
    int 21h
    pop bp
    ret 0
print endp    
codes ends
end start

 

 No near call segment address: offset address is used call 000C address is the address of the entry subroutine

2: Far distal end use

print function that we implemented in another file, the default function extern C language is global

// A文件
#include<stdio.h> 
void print(){
    printf("proc");
}
// master file call 
#include <stdio.h> 
#include <stdlib.h>
 extern   void Print ();
 int main ( int argc, char * the argv []) { 
    Print (); 
    return  0 ; 
}

The compilation includes:

; File A 
public printx 'is declared as a function of the distal end DATAS segment X DB
' proc $ ' ; DATAS ends the ASSUME CS: Codes, DS: DATAS Codes segment Print proc FAR Push DX Push AX Push BP ; MOV BP, SP ; MOV AX, DATAS ; here we directly in the process of the present paragraph, if this section is not required to process the low addresses of the main memory, the operation content to the stack Push MOV DS, AX MOV DX, X offset; offset acquisition MOV AH, 09 ; int 21H ; POP dx pop ax pop bp retf print endp codes ends end

 

; Main file 
Stacks Stack segment 
    DW 100 dup ( 0 ) 
Stacks ends ;
 
EXTRN PrintX: FAR; identify the remote procedure will not write the compilation error, 

Codes segment 
the ASSUME cs: Codes, SS: Stacks 
main proc 
Start: 
    Call FAR ptr PrintX
     mov AH, 4CH
     int 21H ;
     HLT 
main ENDP 
Codes ends 
End Start

 

Note: The above two documents compiled without problems, but if the link following error will appear in our past thinking: that we need to declare a function call or can not link

 

 

Note: Calls to the number of links so far subroutines when needed

+ + pro2.obj pro1.obj main.obj Link + ' ' 'and so on

 debug: 

The main program is: Calling address 0779: 0000

 

 

 Look at the address of a subroutine call address:

 

 

 3: Far use of process

  If declared span called because segment is limited to 64k, need to make calls using call far ptr

The following statement is wrong form: 

stacks segment stack
    dw 100 dup(0)
stacks ends;

extrn printx:far
codes segment
assume cs:codes,ss:stacks
start:
    call far ptr printx
    mov ah,4ch
    int 21h;
    hlt
codes ends
end start

 

Correct declaration form:

Stack segment Stacks 
    DW 100 dup ( 0 ) 
Stacks ends ;
 
EXTRN PrintX: FAR 

Codes segment 
the ASSUME cs: Codes, SS: Stacks 
main proc ; the main program must also be declared procedure call near here have a lot of differences 
Start: 
    Call FAR PrintX PTR
     MOV AH, 4CH
     int 21H ;
     HLT 
main ENDP 
Codes ends 
End Start

 

4: parameter passing problem:

  4.1 transfer parameters register

segment DATAS 
    the X-db ' proc $ ' ;
 DATAS ends 
Stacks Stack segment 
    DW 100 dup (?) 
Stacks ends 
Codes segment 
the ASSUME cs: Codes, SS: Stacks, ds: DATAS 
main proc ; the main program 
Start: 
    mov AX, DATAS ;
     mov ds , AX ;
     MOV DX, offset X ; directly transmitted to the register DX 
    Call FAR PTR Print
     MOV AH, 4CH ;
     int 21H 
main ENDP 

      
Print proc 
     MOVAH, 09 ; using a register DX
      int 21H
      RETF 
Print ENDP     

Codes ends 
End Start

 

  4.2 Memory passing parameters

segment DATAS 
    the X-db ' proc $ ' ;
 DATAS ends 
Stacks Stack segment 
    DW 100 dup (?) 
Stacks ends 
Codes segment 
the ASSUME cs: Codes, SS: Stacks, ds: DATAS 
main proc ; the main program 
Start: 
    mov AX, DATAS ;
     mov ds , AX ;
     Call FAR ptr Print
     mov AH, 4ch ;
     int 21H 
main ENDP 

      
Print proc 
         mov dx, offset the X- ; obtaining the first data segment address 
     movAH, 09 ; outputs the data value of dx 
     int 21H
      RETF 
Print ENDP     

Codes ends 
End Start
 

 

  4.3 Improved memory transmission parameters (stack Recovery)

      If you use the ax bx cx dx si di needs to be restored

     Why do I need to recover:

      If used in the main program a register xx, then the subroutine is also used in this register xx, then when the subroutine returns to the main program, the main memory address stored parameter has no record, program error

segment DATAS 
    the X-db ' proc $ ' ;
 DATAS ends 
Stacks Stack segment 
    DW 100 dup (?) 
Stacks ends 
Codes segment 
the ASSUME cs: Codes, SS: Stacks, ds: DATAS 
main proc ; the main program 
Start: 
    mov AX, Stacks ; initialize the stack 
    MOV SS, AX ;
     MOV AX, DATAS ; initial data segment 
    MOV DS, AX ;
     XOR AX, AX
     MOV AX, offset X ;
     Push AX ;ax offset address is stacked 
    Call Print
     MOV AH, 4CH ;
     int 21H 
main ENDP 

      
Print proc 
     Push BP ;
      MOV BP, SP
      MOV DX, [BP + . 4 ] ; find address 
     MOV AH, 09 ;
      int 21H
      POP BP
      RET  2 ; holding Recovery 
ENDP Print     

Codes ends 
End Start

 

 Improve information: https: //wenku.baidu.com/view/3109f194690203d8ce2f0066f5335a8103d2665a.html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/dgwblog/p/11914837.html