C ++ (Embedded Assembly) 07 from the perspective of Assembly

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

Embedded Assembly is a useful supplement to pure assembly files. The following situations are usually used in assembly: (1) Improve Code For example, MMX commands; (2) perform some special operations on the hardware, such as disconnection, this is common in kernel-level code. However, we will discuss some of the basics of Embedded Assembly with you. We recommend that you do experiments with me and prove everything with facts. (1) Register protection when using Embedded Assembly [CPP] View plaincopy
  1. IntProcess ()
  2. {
  3. _ ASM
  4. {
  5. PUSH AX
  6. Push BX
  7. Push CX
  8. Pop CX
  9. Pop BX
  10. Pop ax
  11. }
  12. Return1;
  13. }

(2) When calling a function, you can directly use call. The returned values are transmitted using eax. Pay attention to the stack balance.[CPP]View plaincopy

  1. IntAdd (IntA,IntB)
  2. {
  3. ReturnA + B;
  4. }
  5. IntProcess ()
  6. {
  7. IntValue = 0;
  8. _ ASM
  9. {
  10. Push eax
  11. Push 2
  12. Push 3
  13. Call add
  14. Add ESP, 8
  15. MoV value, eax
  16. Pop eax
  17. }
  18. Return1;
  19. }

(3) Pay attention to the difference between pointer and value during calculation.[CPP]View plaincopy

  1. VoidProcess ()
  2. {
  3. IntValue = 0;
  4. Int* Address = & value;
  5. _ ASM
  6. {
  7. Push EBX
  8. MoV value, 1
  9. MoV EBX, address
  10. MoV [EBX], 2
  11. Pop EBX
  12. }
  13. Assert (value = 2 );
  14. Return;
  15. }

(4) access to global data[CPP]View plaincopy

  1. Static IntGlobal = 10;
  2. VoidProcess ()
  3. {
  4. _ ASM
  5. {
  6. Push eax
  7. MoV eax, global
  8. Add eax, 1
  9. MoV global, eax
  10. Pop eax
  11. }
  12. Assert (Global = 11 );
  13. Return;
  14. }

(5) loop jump[CPP]View plaincopy

  1. VoidProcess ()
  2. {
  3. IntValue = 0;
  4. _ ASM
  5. {
  6. Push eax
  7. Start:
  8. MoV eax, Value
  9. Add eax, 1
  10. MoV value, eax
  11. CMP value, 10
  12. JNE start
  13. Pop eax
  14. }
  15. Assert (value = 10 );
  16. Return;
  17. }

(6) commands for Guanzhong disconnection cannot be executed in VC, and exceptions may occur.[CPP]View plaincopy

    1. void process ()
    2. {
    3. _ ASM {
    4. CLI
    5. }
    6. return ;
    7. }

(7) vc6.0 supports MMX instruction sets, however, vs2005 supports MMX and SSE instruction sets (8) if the project exists *. ASM Assembly file, you need to follow the steps below A) select [project]-> [setting] B) click project on the left and select *. in the ASM file, two options are displayed in the tab on the right, namely general and custom build C.) enter the corresponding compilation command in commands, so the MASM or NASM type is as follows, see the corresponding Compilation Manual.

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.