编程与软件工程andrewkelley.me
用 LLVM 和 Go 把 NES 游戏静态重编译为原生可执行文件
Statically Recompiling NES Games into Native Executables with LLVM and Go
2013 — 2017,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
作者介绍如何用 LLVM 和 Go 将 NES 游戏静态重编译成原生可执行文件:通过反汇编 6502 机器码、处理间接跳转等难题,把 ROM 翻译成本地代码而非逐条模拟指令,从而实现高性能的原生 NES 模拟。文章详细记录了整个过程中的技术挑战与解决思路。
为什么经久不衰:它把编译器技术、逆向工程和复古游戏模拟结合在一起,是少见的深入浅出的技术长文,多年来反复登上 HN 首页,至今仍是学习静态重编译概念的绝佳入门材料。
HN 评论精选
分享反汇编间接跳转的实用技巧,并推荐 IDA Pro 作为逆向分析工具
关于他遇到的反汇编难题(间接引用),可以稍微“作弊”来获得非常好的效果:不必真正完整执行代码路径,只需在初次遍历代码块时粗略跟踪寄存器赋值即可(如果还能跟踪值的潜在范围就更好了)。我用 SuperH 反汇编器做过类似的事,靠基本的寄存器跟踪加一些粗糙的启发式规则,得到了非常可用的结果。另外强烈推荐了解一下 IDA Pro,虽然它的界面很糟糕,但逆向分析领域没有比它更好的工具。
logic查看英文原文
Just a quick note about the disassembly challenge he faced (indirect references), having gone through this before: you can get amazingly good results by cheating a bit. That is to say, rather than assuming you actually have to properly execute through the code path, you can get very close by roughly tracking register assignments when making your initial pass through a block of code. (Even better, if you can track potential ranges of values with later calls into a given block. Some of this depends on how you've implemented your disassembler, though.) I ended up doing this with a SuperH disassembler (with SH2, due to its two-byte opcode layout, indirect addressing is the order of the day), and by doing basic register assignment tracking and adding a few crude heuristics, I was able to get very usable results. No, the end result won't be "pretty"; you'll be moderately embarrassed to show it off., but it will work. :) (Heuristics: one structure that I had to manually handle were compiler-generated jump tables; thankfully, for my project, I'd had a bit of help from the compiler that was used, and there were distinct signatures I could key off of.) If you're even remotely interested in the disassembly aspect of this, I'd recommend learning a bit about a piece of software called IDA Pro: https://www.hex-rays.com/products/ida/ As horrible as the UI of it is, there is simply nothing better on the market for reverse engineering analysis.
深入探讨文中遗留的两大难题:间接跳转和代码对齐歧义,并提出可能的解决思路
我最近对这个问题很着迷。文中他遇到几个自认为无法解决的问题,听起来像是挑战!特别是那条相当于计算 goto 的指令——跳转目标是“存储在某个地址里的地址”,由于编译期无法知道那里存了什么,似乎只能动态模拟整个 NES 内存空间,这就违背了静态重编译的初衷。但真的没有别的解法吗?还有代码对齐的问题:同一串字节在不同对齐位置会被 CPU 解释成完全不同的指令,不知道能否用流分析为每种对齐生成不同的代码路径来解决。
ZenPsycho查看英文原文
I have actually been rather obsessed with this lately. You'll see that there's a few problems he runs into that he deems insurmountable, which sounds like a challenge! Specifically, there's the issue with an instruction which is effectively a computed goto: A jump instruction that takes an address and then jumps to the address STORED at that address. Since there is no way to know at compile time what addresses are going to stored at a place, you're forced to then dynamically emulate the whole memory space of the actual NES to accurately calculate it, thus defeating the whole point. Is that the only solution though? a head scratcher! Further are issues with parts of code that, on some level seems to be taking inspiration from genetics: Jump to one alignment, and the instructions get interpreted one way, jump to a different alignment and the same sequence of bytes is interpreted by the CPU as an entirely different set of instructions. I wonder if that could be resolved by creating a different source code path for each alignment using flow analysis- A space saving technique effectively getting uncompressed.
赞叹这是高深的“编程黑魔法”,并决定动手实践学习
太棒了。这就是我即使再学 20 年也未必能完全搞懂的“编程黑魔法”,但值得一试!我打算照着这篇文章在终端里动手实践,顺便正好想更好地学习 Go 和 C。感谢作者提供了学习的素材。
tibbon查看英文原文
This is amazing. Also, this is the Dark Magic of programming that I don't think I'll 100% grok in 20 years, but its good to try! edit: now that I think of it, I really need to keep expanding my knowledge. I'm going to go through this post in my terminal and try to at least make the stuff work, so I can start understanding this process. I've been trying to learn Go and C better anyway. Thanks for providing a ground to learn more.
称赞文章的写作质量和行文风格
这是我很久以来见过的最好的技术文章之一。虽然我不会假装自己完全理解了其中的原理,但写作风格非常出色,简洁到位,行文和排版读起来都很舒服。
VeejayRampay查看英文原文
This is one of the best technical articles I've seen in a long long time congratulations. I won't go and pretend I understand what is really going on but the writing style is excellent, to the point and the general flow and formatting are a pleasure. Props dude.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选