Call register, stdcall Convention

Source: Internet
Author: User

Preface

Original article: http://websoso.bokee.com/4693792.html

As a fast and efficient development platform, Delphi is used by more and more people, but I am familiar with programmers who embed ASM code in Delphi code.

I don't think much about it, because there is too little information in this area. On the other hand, it also requires basic assembly language knowledge. It is really about the Assembly Language tutorial.

Too many. If you are not familiar with the assembly language, we suggest you download the intersection tutorial and read it first. Therefore, this document assumes that you are familiar with the assembly language.
(Note: The functions and procedures listed below are collectively referred to as functions .)

1. How to add a piece of assembly code to the Delphi program?
It's easy to use ASM... end to encapsulate your assembly code, and then put it where you need it. This location can be a letter.

Between the begin and end of the number, or between the begin and end of the program, of course, it can be between initialization and end or

In one sentence between finalization and end, any place where the code can be executed by Delphi.
  
Example 1: implement a logical loop of 8 bits to variable X, which tells you how to insert a piece of ASM code in the process.

Procedure tform1.button1click (Sender: tobject );
VaR
X: DWORD;
Begin
X: = $ ff000000;
Showmessage (format ('before shift: %. 8x', [x]);
ASM
MoV eax, X
Ror eax, 8
MoV X, eax
End;
Showmessage (format ('after shift: %. 8x', [x]);
End;

What is it, isn't it easy?

2. How to call a function in assembly code?

First, you need to talk about the function call method.

In Delphi, there are five function calling methods: Register, Pascal, cdecl, stdcall, and safecall.

The register and stdcall methods. How to differentiate these five methods based on three aspects? The first is the parameter transfer order.

(Parameter order), the second is the stack cleaner (clean-up), and the third is whether to use registers to pass parameters (Passes parameters in)

Registers ?). You can find relevant information in Delphi help.

In Delphi, the default parameter passing method is register, that is, when the Declaration is not added, the parameters are all in the register mode. Register mode.

The order in which parameters are transmitted is from left to right. The caller clears the stack and uses registers to pass parameters. How to use registers to pass parameters?

? The first parameter uses eax, the second parameter uses edX, and the third parameter uses ECx. The fourth and later parameters are passed using stacks, and

The stack parameters are left to right.

Stdcall is the default parameter transfer mode in windows. It does not use registers to transmit parameters. In this mode, the parameter transfer sequence is from right

Left, that is, the first incoming stack of the last parameter, in turn forward and backward.

Example 2: Use the ASM code to call the MessageBox function. It tells you how to call the stdcall function in ASM.
Procedure tform1.button2click (Sender: tobject );
VaR
Sztitle: string;
Szcaption: string;
Begin
Sztitle: = 'Hello! ';
Szcaption: = 'This is an example of calling the stdcall type function in the embedded assembly .';
ASM
Push mb_ OK + mb_iconinformation
Push sztitle
Push szcaption
Push 0
Call MessageBox
End;
End;

Let's take a look at the Declaration of the MessageBox function:
Function MessageBox (hwnd: hwnd; lptext, lpcaption: pchar; utype: uint): integer; stdcall;
The call method of this function is stdcall, and the parameter must be from right to left, so we first import the utype parameter into the stack. In this example, the value of this parameter is

Mb_ OK + mb_iconinformation, that is, push mb_ OK + mb_iconinformation, and then import the lpcaption, lptext, and hwnd to the stack in sequence.

Finally, the call command is used to call the MessageBox function.

Example 3: Use the ASM code to call the register function. The declaration of strlen is: function strlen (const STR: pchar ):

Cardinal; its call method is the default register method.
Procedure tform1.button3click (Sender: tobject );
VaR
STR: pchar;
Ilen: integer;
Begin
STR: = 'abcdefghijklm ';
ASM
MoV eax, STR // use eax to pass the first parameter
Call strlen
MoV ilen, eax
End;
Showmessage (inttostr (ilen ));
End;

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.