小编UPi*_*nar的帖子

C 和 C++ 使用 goto 时汇编代码中的 jmp 指令差异

我在 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)

c c++ assembly visual-studio-2022

2
推荐指数
1
解决办法
267
查看次数

如果我的头文件已经使用 std::cout,则在哪里添加 #include &lt;iostream&gt;

我对在项目中添加的位置不太了解#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>

c++

2
推荐指数
1
解决办法
282
查看次数

为什么 int&amp; 作为函数参数使用 QWORD(8 字节)内存,而 int 参数使用 DWORD

在下面的代码中,

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)

c++ assembly gcc reference x86-64

1
推荐指数
1
解决办法
158
查看次数

MOV r/m32、imm32 和 MOV r32、imm32 之间的操作码差异

这些是MOV来自Intel\xc2\xae 64 和 IA-32 架构软件开发人员手册的指令操作码:

\n

B8+ rd id MOV r32, imm32 OI Valid Valid Move imm32 to r32

\n

C7 /0 id MOV r/m32, imm32 MI Valid Valid Move imm32 to r/m32

\n

我拆解如下:

\n
0:  b8 44 33 22 11          mov    eax, 0x11223344\n
Run Code Online (Sandbox Code Playgroud)\n
0:  67 c7 00 44 33 22 11    mov    DWORD PTR[eax], 0x11223344\n
Run Code Online (Sandbox Code Playgroud)\n

我想问的问题是:

\n
    \n
  • 为什么C7操作码是寄存器/内存(r/m32, imm32)而不是仅内存(m32, imm32)?

    \n
  • \n
  • 是否有任何时候我们使用 …

assembly x86-64 machine-code opcode instruction-encoding

1
推荐指数
1
解决办法
237
查看次数