我在 C 和 C++ 中使用相同的代码片段。
#include <stdio.h>
int main() {
goto myLabel;
printf("skipped\n");
myLabel:
printf("after myLabel\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用 Visual Studio 2022 IDE 和编译器。
C++ 的汇编代码
0000000140001000 sub rsp,28h
0000000140001004 jmp 0000000140001014
0000000140001006 jmp 0000000140001014
0000000140001008 lea rcx,[0000000140004230h]
000000014000100F call 0000000140001090
0000000140001014 lea rcx,[0000000140004240h]
000000014000101B call 0000000140001090
0000000140001020 xor eax,eax
0000000140001022 add rsp,28h
0000000140001026 ret
Run Code Online (Sandbox Code Playgroud)
C 的汇编代码
0000000140001000 sub rsp,28h
0000000140001004 jmp 0000000140001012
0000000140001006 lea rcx,[0000000140006000h]
000000014000100D call 0000000140001090
0000000140001012 lea rcx,[0000000140006010h]
0000000140001019 call 0000000140001090
000000014000101E xor eax,eax
0000000140001020 …Run Code Online (Sandbox Code Playgroud) 我对在项目中添加的位置不太了解#include <iostream>。
这是LinkedList.h,有一个成员函数 declarationprint_list()
#include <iostream>
class LinkedList {
public:
void print_list() const;
};
Run Code Online (Sandbox Code Playgroud)
这就是LinkedList.cpp,有成员函数的定义。
#include "LinkedList.h"
void LinkedList::print_list() const
{
// code prints something
std::cout << "There is no element in list\n";
}
Run Code Online (Sandbox Code Playgroud)
这是main.cpp
#include <iostream>
#include <LinkedList.h>
int main()
{
// code
}
Run Code Online (Sandbox Code Playgroud)
问题是,我确实认为(可能是错误的)只有main.cpp文件必须使用#include <iostream>.
有人可以解释如何处理这种情况以及在哪里添加#include <iostream>
在下面的代码中,
int firstFunction(int& refParam)
{
std::cout << "Type of refParam is: " << typeid(refParam).name() << '\n';
return refParam;
}
int secondFunction(int param)
{
std::cout << "Type of param is: " << typeid(param).name() << '\n';
return param;
}
int main()
{
int firstVar{ 1 };
int secondVar{ firstFunction(firstVar) };
int thirdVar{ secondFunction(firstVar) };
}
Run Code Online (Sandbox Code Playgroud)
控制台输出是
int
int
Run Code Online (Sandbox Code Playgroud)
当我检查Godbolt 链接中的汇编代码时。
firstFunction(int&):
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-8], rdi
mov rax, QWORD PTR [rbp-8]
mov eax, DWORD PTR [rax] …Run Code Online (Sandbox Code Playgroud) 这些是MOV来自Intel\xc2\xae 64 和 IA-32 架构软件开发人员手册的指令操作码:
B8+ rd id MOV r32, imm32 OI Valid Valid Move imm32 to r32。
C7 /0 id MOV r/m32, imm32 MI Valid Valid Move imm32 to r/m32。
我拆解如下:
\n0: b8 44 33 22 11 mov eax, 0x11223344\nRun Code Online (Sandbox Code Playgroud)\n0: 67 c7 00 44 33 22 11 mov DWORD PTR[eax], 0x11223344\nRun Code Online (Sandbox Code Playgroud)\n我想问的问题是:
\n为什么C7操作码是寄存器/内存(r/m32, imm32)而不是仅内存(m32, imm32)?
是否有任何时候我们使用 …