0%

2024-03-21-c++调用汇编

CMakeLists.txt

1
2
3
4
5
6
7
8
cmake_minimum_required(VERSION 3.24)
project(cplusplus_demo)

set(CMAKE_CXX_STANDARD 11)

ENABLE_LANGUAGE(ASM)

add_executable(main main.cpp test.S)

main.cpp

1
2
3
4
5
6
7
8
9
10
#include <iostream>

using namespace std;

extern "C" int addJohnSong(int a,int b);

int main(int argc, char *argv[]) {

cout << addJohnSong(2, 3) << endl;
}

test.S

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	.globl	_addJohnSong
.p2align 4, 0x90
_addJohnSong: ## @addJohnSong
.cfi_startproc
pushq %rbp
movq %rsp, %rbp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl -4(%rbp), %eax
addl -8(%rbp), %eax
addl -8(%rbp), %eax
popq %rbp
retq
.cfi_endproc