The meaning and function of PTR in assembly language

Source: Internet
Author: User

The meaning and function of PTR in assembly language

 

 

 

MoV ax, BX; is to assign the value of the Bx register "in" to ax, because both are word type, so there is no need to add "word"
MoV ax, word PTR [BX]; is the data stored in the place where the memory address is equal to the value of the Bx register, and is assigned to ax. Because we only provide a memory address and do not know whether it is byte or word that we want to assign to ax, we need to use word to clearly point out it!

Therefore, PTR is used when the width of the two operands is different.

That is to say

* P is represented as dword ptr [p] in assembly.

* P is the value of the memory address referred to by P.

 

Int n = 100; <br/> int * P = & N; <br/> assert (* P = 100); <br/>

* P = 100 is true

Dword ptr [EBP-xx] is a common usage and is often used to obtain local variables:

Write a piece of code at will

Base * P = new derive; <br/> base * D = P; <br/> D-> F ();

The disassembly code is as follows:

31: base * D = P; <br/> 00401469 mov eax, dword ptr [ebp-18h] <br/> 0040146c mov dword ptr [ebp-1Ch], eax <br/> 32: d-> F (); <br/> 0040146f mov ECx, dword ptr [ebp-1Ch] <br/> 00401472 mov edX, dword ptr [ECx] // obtain the virtual function table pointer <br/> 00401474 mov ESI, esp <br/> 00401476 mov ECx, dword ptr [ebp-1Ch] // ECx is the this pointer <br/> 00401479 call dword ptr [edX] // call the first virtual function, here is F <br/> 0040147b cmp esi, esp <br/> 0040147d call _ chkesp (00402750) <br/>

Let's take a look at the address operators in C and how to handle them.

27: int N; <br/> 28: int * PP = & N; <br/> 0040141d Lea eax, [ebp-10h] <br/> 00401420 mov dword ptr [ebp-14h], eax <br/>

 

Lea command I saw someone in the snow Forum say this:

Lea is Intel's rather proud command (although most programmers do not agree ). In intel optimization referfence manual, special advantages of this command are mentioned.

0. The LEA command has a single clock cycle, and the execution efficiency is very high.

1. It is the CPU address generation unit involved in the operation, rather than ALU involved in the operation, so it will not be related to the flow of contextual Arithmetic Logic commands;

Lea is not executed in ALU, but in AGU (address generation unit ).

ALU and Agu are parallel integer computing units. The Lea and add are cleverly separated and executed in parallel, greatly improving the throughput.

2. the intel instruction set does not contain the Three-operand arithmetic operation commands provided by many server-defined CPUs, such as arm's "add r0, R1, R2", and the lea command exactly provides the same functions, to simulate the "Ternary Arithmetic Logic command ".

For example, to calculate the sum of two registers, but do not want to destroy the original value, you can run the lea EBX, [eax + EDX] command, the addition operation EBX = eax + EDX is executed. If the Add command is used, it cannot be completed in one command.

3. In assembly language programming, it is very convenient to use Lea when a variable address needs to be obtained. MoV commands often have errors, because in Microsoft's MASM Assembly syntax, label and variable are different.

It seems thatLea can be used.

Summary:

Value: mov eax, [ebp-18h] mov [ebp-1ch], eax is equivalent to: int A = B;

Get address: Lea ECx, [ebp-18h] mov [EBP-1ch], ECx equivalent: int * P = & B;

Always remember.

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.