Modular Programming

Call instruction-call subroutine

Intrasegmental metastasis:
Format: call label
Principle:
1) Press the current IP onto the stack
(sp) = (sp) – 2
((ss)*16+(sp)) = (IP)
2) Transfer to the label to execute instructions
call instruction transfer method and jmp instruction principle are similar
(IP) = (IP) + 16-bit displacement
Equivalent to jmp near ptr label
Intersegmental transfer:
Format: call far ptr label
Principle:
1) Put the current CS and IP on the stack
(sp) = (sp) – 2
((ss) ×16+(sp)) = (CS)
(sp) = (sp) – 2
((ss) ×16+(sp)) = (IP)
2) Transfer to the label to execute instructions
(CS) = the segment address where the label is located
(IP) = the offset address of the label
Equivalent to jmp far ptr label
Transfer address in register:
Format: call 16-bit register
Amount to:
push IP
jmp 16-bit register
Transfer address in memory:
1. Callword PTR memory unit address
Amount to:
push IP
jmp word ptr memory unit address
2. Calldword PTR memory unit address
Amount to:
push CS
push IP
jmp dword ptr memory unit address (low address offset address, high address segment address)

ret and retf-return instructions

ret directive
Function: Modify the content of IP with the data in the stack, so as to realize the return in the near-transfer segment, equivalent to pop IP
retf instruction
Function: Modify the contents of CS and IP with the data in the stack, so as to realize the return between distant transfer segments, which is equivalent to pop IP and popCS.

Parametric Transfer Problem

  1. Register transfer parameters
  2. Memory unit batch transfer data
  3. Passing parameters by stack

Examples of modular programs

Title Requirements: To compile a subroutine to find y=x^4 and store the results in the corresponding data area
data segment

 x db 1,2,3,4,5,6,7,8

 y dw 0,0,0,0,0,0,0,0

data ends

assume cs:codesg, ds:datasg,ss:stacksg
datasg segment
     x db 1,2,3,4,5,6,7,8
     y dw 0,0,0,0,0,0,0,0
datasg ends
stacksg segment
    db  32 dup (0)
stacksg ends
codesg segment

main proc
start: mov ax,datasg
       mov ds,ax
       mov ax,stacksg
       mov ss,ax
       mov sp,16
       mov cx,8
       lea si,x   ;Obtain x The label as the source offset address
       lea di,y   ;Obtain y The label as the target offset address
    op:mov bl,[si]  ;Preparing parameters for calling subroutines
       call subp
       ;Processing to be done after subroutine call returns
       mov [di],ax  ;Store in the corresponding unit
       inc si
       inc di
       inc di
       loop op
       mov ax,4c00h
       int 21h
main endp

;Subprogram function: y=x^4
;Entry parameters: x The value of bl provide
;Return value: y Value from ax Return, and y Values will not exceed the range of one word
subp proc
    push bx
    mov cx,4
    mov bh,0
    mov ax,1
    l:mul bx
    loop l
    pop bx
    ret
subp endp
codesg ends
end start

Multi-file storage call
p1.asm file

extrn subp:far
assume cs:codesg, ds:datasg,ss:stacksg
datasg segment
     x db 1,2,3,4,5,6,7,8
     y dw 0,0,0,0,0,0,0,0
datasg ends
stacksg segment
    db  32 dup (0)
stacksg ends
codesg segment

main proc
start: mov ax,datasg
       mov ds,ax
       mov ax,stacksg
       mov ss,ax
       mov sp,16
       mov cx,8
       lea si,x   ;Obtain x The label as the source offset address
       lea di,y   ;Obtain y The label as the target offset address
    op:mov bl,[si]  ;Preparing parameters for calling subroutines
       call subp
       ;Processing to be done after subroutine call returns
       mov [di],ax  ;Store in the corresponding unit
       inc si
       inc di
       inc di
       loop op
       mov ax,4c00h
       int 21h
main endp
codesg ends
end start

In the p2.asm file:

public subp
assume cs:codesg
codesg segment
subp proc
    push bx
    mov cx,4
    mov bh,0
    mov ax,1
    l:mul bx
    loop l
    pop bx
    retf
subp endp
codesg ends
end

Note the compilation and linking process here:
Compile separately:
masm p1.asm; generate p1.obj
masm p2.asm; generate p2.obj
Connect together:
link p1.obj+p2.obj
The name of the exe file generated will be prompted during the connection
debug xx.exe

mul-multiplication instruction

Format: mul register or mul memory unit

Posted by DevXen on Wed, 03 Jul 2019 13:51:41 -0700