C++嵌入汇编demo (_asm用法)

// ASM_demo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 //

#include <iostream> #include <Windows.h>

using namespace std; void showResult(int a, LPCSTR b) { std::cout << std:: endl; std::cout <<a<<b<< std::endl; }

boolean isEqual(int a, int b) { if (a == b) { return true; } return false; }

void demo1() { // SetConsoleTitle(L"kernel Demo2222"); const char* kernelMessage = "Hello kernel"; const char* kernelMessage3 = "相等"; const char* kernelMessageResult = "L2:Success!!"; const char* kernelMessageTitle = "Hello"; const char* kernelMessageTitle2 = "world";

_asm { mov eax, kernelMessage push eax call printf pop eax mov eax, 1 push eax mov ebx, kernelMessageTitle push ebx mov ecx, kernelMessage push ecx mov edx, 0 push edx call MessageBoxA //there is no need to pop eax ,ebx,ecx,or edx for MessageBox mov eax, kernelMessageTitle2 mov ebx, 0x2 push eax push ebx call showResult pop ebx pop eax mov eax, 0x1 mov ebx, 0x1 push eax push ebx cmp ebx, eax jnz L1 //Determines whether two values in the stack area are equal mov ecx, kernelMessage3 push ecx call printf //print not equal msg pop ecx

L1 :call isEqual jz L2 pop ebx pop eax

L2:pop ebx pop eax mov eax, kernelMessageResult push eax call printf pop eax

} } int main() { demo1() return 0; }

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单

经验分享 程序员 微信小程序 职场和发展