要学习汇编 - 我应该从 32 位还是 64 位开始?

发布于 2024-08-23 09:53:37 字数 1757 浏览 12 评论 0原文

我真的很想学汇编。我非常擅长 c/c++,但希望更好地了解较低级别上发生的情况。

我意识到以前曾有人问过与程序集相关的问题,但我只是在寻找一些适合我的情况的方向:

我正在运行 Windows 7,并且对应该如何开始使用程序集感到困惑。因为我运行的是 Windows 7,所以我必须从 x64 开始吗?有些人说“先从 32 位开始”——我该如何去做呢?我的操作系统与我为“32”或“64”位编写程序集的能力有什么关系。事实上,“n 位”汇编意味着什么,其中 n 是一个数字?


编辑:

这里有一些帮助我开始组装的链接;其他刚刚开始使用的人可能会发现它们很有帮助。当我继续我的汇编之旅时,我将不断更新此列表:)

注意:正如我一直在学习的那样,我决定专注于使用 masm32 进行编程。因此,以下大部分资源都集中于此。

  • tag wiki(初学者指南、参考手册、ABI 文档等。)
  • www.masm32.com
  • X86 汇编 WikiBook
  • X86 反汇编 WikiBook(非常适合理解一些约定,以及高级代码如何转换为汇编的基础知识)
  • WinAsm IDE(与 masm32 配合良好)
  • 简介:Windows 程序集(所有代码示例均适用于 masm32)
  • 中断列表
  • 汇编教程(非常有助于理解核心概念)
  • x86 组装指南
  • < a href="http://agner.org/optimize/" rel="noreferrer">Agner Fog 的软件优化资源,包括一些关于不同平台上调用约定的好东西(Windows 与 Linux/OS X) ,以及很多如何高效地完成特定事情的示例。不太适合初学者,但适合中级到高级读者。

    (他还提供了针对 Intel 和 AMD CPU 的每条指令的详细性能信息,非常适合严格的性能微优化。一些初学者可能想查看其中的一些内容,以开始思考 CPU 的工作原理以及为什么可能会出现这种情况。以一种方式而不是另一种方式做某事。)

I'm really wanting to learn assembly. I'm pretty good at c/c++, but want a better understanding of what's going on at a lower level.

I realize that assembly related questions have been asked before, but I'm just looking for some direction that's particular to my situation:

I'm running windows 7, and am confused about how I should start working with assembly. Do I have to start with x64 because I'm running windows 7? Some people have said 'start with 32 bit first' - how do I go about doing this? What does my operating system have to do with my ability to write assembly for '32' or '64' bit. In fact, what does 'n bit' assembly mean, where n is a number??


Edit:

Here are some links that have helped me get started with assembly; others who are just getting started may find them helpful. I'll keep updating this list as I continue on my assembly journey :)

Note: As I've been learning, I've decided to focus on programming with masm32. Therefore most of the below resources focus on that.

  • tag wiki (beginner guides, reference manuals, ABI documentation, and more.)
  • www.masm32.com
  • X86 Assembly WikiBook
  • X86 Dissassembly WikiBook (great for understanding some conventions, and the basics of how higher level code translates into assembly)
  • WinAsm IDE (plays nicely with masm32)
  • Intro: Assembly for Windows (all code examples are for masm32)
  • List of Interrupts
  • Assembly Tutorial (great for helping to understand core concepts)
  • x86 Assembly Guide
  • Agner Fog's Software optimization resources, including some good stuff about calling conventions on different platforms (Windows vs. Linux/OS X), as well as a lot of examples of how to do specific things efficiently. Not great for total beginners, but great for intermediate to advanced readers.

    (He also has detailed performance info for each instruction for Intel and AMD CPUs, excellent for serious performance micro-optimization. Some beginners might want to look at some of that to get started thinking about how CPUs work, and why you might do something one way instead of another.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝眼泪 2024-08-30 09:53:37

当人们提到 32 位64 位 程序集时,他们谈论的是您将使用哪个指令集 - 它们有时也称为 Ia32x64 在英特尔案例中,我猜你正在问这个问题。 64 位情况下还有很多事情要做,因此从 32 位开始可能会更好;您只需要确保使用 32 位汇编器将程序汇编成 32 位二进制文​​件即可。 Windows 仍然知道如何运行它。

对于开始使用汇编,我真正建议的是使用更简单的指令集来掌握。去学习 MIPS 程序集 - spim 模拟器非常棒且易于使用。如果您确实想直接进入 Intel 汇编世界,请自己编写一个小 C 程序来为您调用汇编例程;为“真实程序”进行所有设置和拆卸是一团糟,您甚至无法从那里开始。因此,只需编写一个包含 main() 的 C 包装器,然后将其与通过编写汇编代码获得的目标文件进行编译和链接。

请不要养成在 C 代码中编写内联汇编的习惯 - 这是代码可移植性的噩梦,而且没有理由这样做。

您可以下载所有英特尔 64 和 IA-32 架构软件开发人员手册以获取开始了。

When people refer to 32-bit and 64-bit assembly, they're talking about which instruction set you'll use - they're also sometimes called Ia32 and x64 in the Intel case, which I presume you're asking about. There is a lot more going on in the 64-bit case, so starting with 32-bit is probably good; you just need to make sure you're assembling your program with a 32-bit assembler into a 32-bit binary. Windows will still know how to run it.

What I really recommend for getting started with assembly would be something with a simpler instruction set to get a handle on. Go learn MIPS assembly - the spim simulator is great and easy to use. If you really want to dive straight into the Intel assembly world, write yourself a little C program that calls your assembly routines for you; doing all the setup and teardown for a 'real program' is a big mess, and you won't even be able to get started there. So just write a C wrapper with main() in it, and compile and link that with the object files you get from writing your assembly code.

Please don't get in the habit of writing inline assembly in your C code - it's a code portability nightmare, and there's no reason for it.

You can download all of the Intel 64 and IA-32 Architectures Software Developer's Manuals to get started.

青朷 2024-08-30 09:53:37

我从 1977 年开始编写汇编,走了一条漫长的道路:首先学习基本运算(与、或、异或、非)和八进制数学,然后为具有 OS/8 和 8k 内存的 DEC PDP-8/E 编写程序。那是在 1977 年。

从那时起,我发现了一些关于如何学习我不熟悉的架构的汇编的技巧。包括:8080/8085/Z80、x86、68000、VAX、360、HC12、PowerPC 和 V850。我很少编写独立的程序,它通常是与系统的其余部分链接的功能,这些功能通常是用 C 编写的。

所以首先我必须能够与需要学习参数传递的软件的其余部分进行交互,堆栈布局,创建堆栈帧,参数位置,局部变量位置,丢弃堆栈帧,返回值,返回和堆栈清理。执行此操作的最佳方法是编写一个函数来调用 C 中的另一个函数,并检查编译器生成的代码列表。

为了学习汇编语言本身,我编写了一些简单的代码,查看编译器生成的内容并在原始调试器中单步执行它。我附近有指令集手册,这样我就可以查找我不确定的指令。

值得了解的一件好事(除了前面提到的堆栈处理之外)是编译器如何在给定某种高级语言构造的情况下生成机器代码。其中一个序列是如何将索引数组/结构转换为指针。另一个是循环的基本机器代码序列。

那么什么是“原始调试器”?对我来说,它是一个调试器,是简单开发包的一部分,并且不会像可视化调试器那样试图保护我免受硬件的影响。在其中我可以轻松地在源代码调试和汇编调试之间切换。它还可以从开发 IDE 内部快速启动。它没有三千个功能,更有可能有三十个,而这些功能将是您 99.9% 的时间使用的功能。开发包通常是安装程序的一部分,您单击一次进行许可证批准,一次批准默认设置(当有人为您考虑并完成这项工作时,您不喜欢它吗?),最后一次安装。

我最喜欢的 x86-32 (IA-32) 简单开发环境就是 OpenWatcom。您可以在 openwatcom.org 上找到它。

我对 x86-64 (AMD64) 相当陌生,但过渡似乎很简单(很像从 x86-16 迁移到 x86-32 时),有一些额外的花招,例如额外的寄存器 r8 到 r15 以及主寄存器是 64 位宽的。我最近刚刚遇到了 XP/64、Vista/64 和 7/64 的开发环境(可能也适用于服务器操作系统),它被称为 Pelles C (pellesc.org)。它是由瑞典的 Pelle Orinius 编写和维护的,从我所花费的几个小时来看,我可以说它注定会成为我最喜欢的 x86-64。我尝试过 Visual Express 软件包(它们安装了很多垃圾 - 你知道之后需要卸载多少次吗?超过 20 个),并且还尝试从一个地方获取 gcc 来与 IDE 一起使用(eclipse 或其他东西) )来自另一个。

一旦您走到这一步并且遇到了一个新的架构,您将能够花一两个小时查看生成的列表,然后几乎知道它与其他架构有何相似之处。如果索引和循环结构看起来很奇怪,您可以查看生成它们的源代码,也许还可以查看编译器优化级别。

我想我应该警告你,一旦你掌握了窍门,你就会发现,在附近的办公桌旁、咖啡机旁、会议中、论坛上以及许多其他地方,都会有人等着嘲笑你、取笑你由于您对装配感兴趣,因此向您抛出不完整的报价并提供不知情/不称职的建议。我不知道他们为什么这样做。也许他们自己就是失败的汇编程序员,也许他们只知道 OO(C++、C# 和 Java),而根本不知道汇编程序是什么。也许他们“认识”的人(或者他们的朋友认识的人)“非常优秀”,可能在论坛上读到过一些东西,或者在会议上听到过一些东西,因此可以说出一个绝对的事实,说明为什么集会完全是浪费时间。时间。 stackoverflow 上有很多这样的内容。

I started writing assembly in 1977 by taking the long route: first learning basic operations (and, or, xor, not) and octal math before writing programs for the DEC PDP-8/E with OS/8 and 8k of memory. This was in 1977.

Since then I have discovered a few tricks on how to learn assembly for architectures I am unfamiliar with. It's been a few: 8080/8085/Z80, x86, 68000, VAX, 360, HC12, PowerPC and V850. I seldom write stand-alone programs, it's usually functions that are linked with the rest of the system which is usually written in C.

So first of all I must be able to interface to the rest of the software which requires learning the parameter passing, stack layout, creating the stack frame, parameter positions, local variable positions, discarding the stack frame, returned values, return and stack cleanup. The best way to do this is to write a function that calls another function in C and examine the code listing generated by the compiler.

To learn the assembly language itself I write some simple code, seeing what the compiler generates and single-stepping through it in a raw debugger. I have the instruction set manuals close by so I can look up instructions I am unsure of.

A good thing to get to know (in addition to the stack handling mentioned previously) is how the compiler generates machine code given a certain high-level language construct. One such sequence is how indexed arrays/structures are translated into pointers. Another is the basic machine code sequence for loops.

So what is a "raw debugger?" To me it's a debugger that is part of a simple development package and that doesn't try to protect me from the hardware like the Visual debugger(s). In it I can easily switch between source and assembly debugging. It also starts quickly from inside the development IDE. It doesn't have three thousand features, more likely thirty and those will be the ones you use 99.9% of the time. The development package will typically be part of an installer where you click once for license approval, once for approving the default setup (don't you love it when someone has thought about and done that work for you?) and a last time for install.

I have one favorite simple development environment for x86-32 (IA-32) and that is OpenWatcom. You can find it at openwatcom.org.

I am fairly new to x86-64 (AMD64) but the transition seems straightforward (much like when moving from x86-16 to x86-32) with some extra gimmicks such as the extra registers r8 to r15 and that the main registers are 64 bits wide. I just recently ran across a development environment for XP/64, Vista/64 and 7/64 (probably works for the server OS:s as well) and it is called Pelles C (pellesc.org). It is written and maintained by one Pelle Orinius in Sweden and from the few hours I've spent with I can say that it is destined to become my favorite for x86-64. I've tried the Visual Express packages (they install so much junk - do you know how many uninstalls you need to do afterwards? more than 20) and also tried to get gcc from one place to work with an IDE (eclipse or something else) from another.

Once you've come this far and you come across a new architecture you will be able to spend an hour or two looking at the generated listing and after that pretty much know what other architecture it resembles. If the index and loop constructs appear strange you can look over the source code generating them and perhaps also the compiler optimization level.

I think I should warn you that once you get the hang of it you will notice that at desks close by, at the coffee machine, in meetings, in fora and lots of other places there will be individuals waiting to scorn you, make fun of you, throw incomplete quotes at you and give uninformed/incompetent advice because of your interest in assembly. Why they do this I don't know. Perhaps they themselves are failed assembly programmers, perhaps they only know OO (C++, C# and Java) and simply don't have a clue as to what assembler is about. Perhaps someone they "know" (or whom a friend of theirs knows) who is "really good" may have read something in a forum or heard something at a conference and therefore can deliver an absolute truth as to why assembly is a complete waste of time. There are plenty of them here at stackoverflow.

ζ澈沫 2024-08-30 09:53:37

获取 IDA pro。 它是处理汇编的最佳工具。

我个人认为 32 位和 64 位之间没有太大区别。它与位无关,而是与指令集有关。当您谈论汇编时,您谈论的是指令集。也许他们是在暗示 32 位指令集更适合学习。但是,如果这是您的目标,我建议您阅读 Donald Knuths 的算法书籍 - 他们以 7 位指令集汇编的形式教授算法:D

对于可移植性问题,我建议您学习如何使用编译器内在函数,而不是内联汇编 -这将是非嵌入式优化的最佳优化。 :D

Get IDA pro. It's the bees knees for working with assembly.

I personally don't see much of a difference between 32-bit and 64-bit. It is not about the bits but the instruction set. When you talk about assembly you talk about instruction sets. Perhaps they are implying that a 32-bit instruction set is better to learn from. However if that is your goal I suggest Donald Knuths books on algorithms -- they teach algorithms in terms of a 7-bit instruction set assembly :D

For portability issues, I suggest that instead of inline assembly you learn how to use compiler intrinsics -- it will be the best optimization for non-embedded optimizations. :D

烟沫凡尘 2024-08-30 09:53:37

但想要更好地了解较低级别正在发生的事情

如果您确实想了解 x86/x64 处理器/系统上较低级别上发生的所有事情,我真的建议从基础知识开始,即 286/386实模式代码。例如,在 16 位代码中,您被迫使用内存分段,这是一个需要理解的重要概念。如今的32位和64位操作系统仍然以实模式启动,然后切换到相关模式/在相关模式之间切换。

但如果您对应用程序/算法开发感兴趣,您可能不想学习所有低级操作系统的内容。相反,您可以立即从 x86/x64 代码开始,具体取决于您的平台。请注意,32 位代码也可以在 64 位 Windows 上运行,但反之则不然。

but want a better understanding of what's going on at a lower level

If you really want to know everything that's going on at a lower level on x86/x64 processors/systems, I would really recommend starting with the basics, that is, 286/386 real mode code. For example, in 16-bit code you are forced to use memory segmentation which is an important concept to understand. Today's 32-bit and 64-bit operating systems are still started in real mode, then switch to/between the relevant modes.

But if you're interested in application/algorithm development, you might not want to learn all the low-level OS stuff. Instead you can start right off with x86/x64 code, depending on your platform. Note that 32-bit code will also run on 64-bit Windows, but not the other way round.

无妨# 2024-08-30 09:53:37

开始 C 编程(不是 C++ 或 C#)将帮助您基本了解“自己做这一切”所需的内容,例如寄存器、堆栈帧和数据处理。我获得了计算机科学硕士学位,我最喜欢的主题之一是编译器构建(是的 yacc 和 lex!),这确实帮助我深入了解所有高级语言。我仍然珍惜那些定义我自己的语言并将其编译为低级结构的时刻。事实上,我设计了一种在虚拟处理器上执行的面向对象语言。

所以:学习汇编没有捷径。这可能很乏味。但非常满足。

Start programming C, (not C++ , or C#) will help you to get a basic understanding of what is needed to 'do it all yourself', like register, stack frame, and data processing. I did a master on computer science and one of my favourite topics is compiler building (yes yacc and lex!) which did help me to understand all higher level language in a deep intimate level. I still cherish those moments defining my own language and compiling it to low level constructs. Indeed I designed a object oriented language to be executed on a virtual processor.

So: there are no shortcuts learning assembler. It can be tedious. But very satisfying.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文