Assembly language Note call and RET instructions

Source: Internet
Author: User
Tags mul microsoft dynamics

Reprint Address: Http://www.cnblogs.com/dennisOne

? Modular programming

    1. Modular programming

      Assembly language implements modular programming through call and RET instructions. It is possible to implement multiple interconnected, functionally independent subroutines to solve a complex problem.

    2. Sub-Program Framework
      1 Assume cs:code 2 code segment 3     main:     : 4               : 5               call Sub1          ; Calling subroutine Sub1 6               : 7               : 8               mov ax, 4c 00h 9               int 21h10     sub1:     :                  ; subroutine sub1 start               :          call sub2; Calling subroutine sub214               : +               ret                ; sub-program returned to     sub2:     :                  ; subroutine Sub2 start               : £               º               RET The                subroutine returns the code ENDS23 end main

? Call and RET directives

Instructions

Function

Compilation grammar explanation

Note

Ret

Use the data in the stack to modify the contents of the IP,

achieve near-transfer.

Pop IP

Retf

Use the data in the stack to modify CS and IP content,

Achieve far transfer.

Pop IP

Pop CS

Retn

Pop IP

Add SP, n

See the back.

Pager

The call instruction is divided into two steps:

(1) Press the current IP or CS and IP into the stack.

(2) Transfer.

Format

Compilation grammar explanation

Call label

Push IP

JMP near PTR label

Call Far label

Push CS

Push IP

JMP far PTR designator

Call 16-bit Reg

Push IP
JMP 16-bit Reg

Call word PTR memory cell address

Push IP
JMP Word PTR memory cell address

Call DWORD PTR memory Unit address

Push CS
Push IP
JMP DWORD PTR memory Unit address

Pager

Means

Make

No

Yes

Real

Is

Short

Turn

Move

? mul and DIV commands

Instruction format

Explain

Example


MUL Reg

Mul Memory Unit

(1) 8-bit multiplication: The default multiplier one is placed in Al and the other is placed in a 8-bit reg or memory byte cell. Default results are placed in Microsoft Dynamics AX

(2) 16-bit multiplication: The default multiplier is placed in Microsoft Dynamics AX and the other is placed in a 16-bit reg or memory word cell. Default results are placed in dx| In Ax.

Mul byte ptr ds:[0]

; (ax) = (AL) * ((DS) *16+0)

Mul word ptr [bx+si+8]

; (dx|ax) = (AX) * ((DS) *16+ (BX) + (SI) +8)

DIV Reg

div Memory Unit

(1) 16-bit/8-bit: Dividend By default in AX, the divisor is placed in a 8-bit reg or memory byte cell. Results Al (quotient), AH (remainder).

(2) 32-bit/16-bit: dividend default dx| In Ax, the divisor is placed in a 16-bit reg or memory word cell. Results ax (quotient), DX (remainder).

(3) Cross-border issues (how to design a security div see back)

div byte ptr ds:[0]
; (AL) = (AX)/((DS) *16+0)
; (AH) = remainder of (AX)/((DS) *16+0)

div word ptr [bx+si+8]

; (AX) = (AX)/((DS) *16+ (BX) + (SI) +8)

; (dx) = (AX)/((DS) *16+ (BX) + (SI) +8) remainder

? parameters and result passing

Position

Advantages

Disadvantages

Example

Put it in the register.

The most common method

Fast

Limited number of registers,

Register may be conflicting

; Description: Calculates the 3-th square of n

; Parameter: (BX) =n

; Results: (DX:AX) =n^3

Cube:mov AX, BX

Mul BX

Mul BX

Ret

Put it in the data segment

Delivery of bulk data

Slow speed

; Ds:si points to the first address of the space where the string (bulk data) resides

; The length of the CX store string

Capital:and byte ptr [si], 11011111b

Inc si

Loop capital

Ret

Put it on the stack.

The way of C language

Simple and convenient

Explain in detail

    • attached 1: Use the stack pass parameter

      Combined with the C language function to learn the idea of stack transfer parameters.

       

       1; Description: Calculates (A-B) ^3,a, 2 for font data, and parameter: When entering subroutines, the top of the stack holds the IP, followed by a, B 3;              Result: (Dx:ax) = (A-B) ^3 4 difcube:push BP 5 mov bp, SP 6 mov ax, [bp+4]; the value of a in the stack is fed into AX 7 Sub Ax, [bp+6];              A-B 8 mov bp, ax 9 mul bp10 mul bp11 pop BP12 ret 4 ;    Popup parameter (modify the top pointer to the value before the call) 13 14; The meaning of the directive ret N: 15;    Pop ip16; The Add sp,n 

      uses the stack for parameter passing, so the caller presses the arguments into the stack when assembling the program, and the subroutine can use RET when returning The n instruction modifies the stack-top pointer to the value before the call.

    • Attached 2: C language Processing method (local variable, also use stack)
      1 void Add (int, int, int); 2      3 void Main () 4 {5     int a = 1; 6     int b = 2; 7     int c = 0; 8     Add (A, B, c); 9     C + +;}11     ID Add (int a, int b, int c), {      + c = a + b;15}

Assembly language Note call and RET instructions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.