10 ms·
Even if you can't write assembly, you can read disassembly
- patrakov 4y agoWell, at least 30 years ago I could write assembly. But a 188 KB driver for a camera sensor is something that is still too big for me to use as a basis for reverse engineering.
- User23 4y agoThis is about 90% of how I use Common Lisp’s DISASSEMBLE function: checking to see if my optimization declarations are having the desired effect.
- ChancyChance 4y ago[flagged]
- retrac 4y agoIt's not that hard to write assembly. Tedious is the word I would use. Learn how to express the basic operators of C in assembly (functions and parameters with the stack, do/while/if/for, array and struct indexing) and then write everything in pseudo-C and hand translate it to assembly. Writing optimized assembly is a whole other question. But some variant of the above - pseudo-code or flowcharting, followed by close translation into assembly, is how most software in assembly was written, back in the day. Assuming your program logic is correct to begin with, it's almost mindless. So mindless you could program a computer to do it.
- waynesonfire 4y agodo you have any examples of the flowcharts used for asm writing? i also recall someone using an excel spreadsheet when writing for itanium.
- retrac 4y agohttps://www.ibiblio.org/apollo/Documents/GeminiMinnickMathFlow.pdf https://www.ibiblio.org/apollo/Documents/GeminiMinnickMathFl... (large PDF) This is the flight software used in the Gemini spacecraft (1960s). The actual assembly code did not survive. It was considered sort of incidental. For the designers and coders then, the flowcharts were the program, the real source. There's more information about the Gemini software and how the programmers worked here: https://www.ibiblio.org/apollo/Gemini.html#Evolution_of_the_Flight_Software https://www.ibiblio.org/apollo/Gemini.html#Evolution_of_the_...
- pdpi 4y agoJust literally draw a flowchart of what your program is supposed to do, then translate that flowchart. Something like each box gets its own label, diamonds are just some variation on `jz some-box-label`, etc. The problem with assembly isn't that it's too complex, it's that it's way too simple, so it's exceedingly easy to lose track of how your higher-level thought process maps to assembly code. The flowchart serves as an intermediate representation between the two — something you can reason about at a high level, but can almost mindlessly, mechanically translate to assembly.
- kevincox 4y agoIt's not that hard to write assembly. It is hard to write good assembly. In order to produce maintainable software you need incredible discipline (and probably some advice what that discipline should be). To a large extent you can consider high level languages opinionated. They have decided how to pass arguments, how to return values and how to lay out memory. The programmer no longer needs to think about these things, and they can't mess them up if they try. Doing this yourself you need to pick to those conventions and stick to them if you want maintainable software. Of course the benefit of assembly is that it is trivial to break those conventions when there is good reason (optimization for example) but then you will pay the price of more difficult maintenance for that unconventional code.
- labrador 4y agoI was hired to write assembly (68000) so I didn't know any better. I liked developing the discipline and the knowledge of the machine. I still miss it sometimes. But like another comment said, it's tedious, so I wouldn't go back to it.
- retrac 4y agoGreat point. The only time I've been required (rather than for fun/hobby purposes) to write assembly was for a controller where we didn't have a C compiler. Like I hinted, I followed the approach of writing in C (including testing it as much as possible on my desktop in C) and then hand-translating precisely because of what you mention -- by default there's no structure. I basically adopted the structures and conventions of the C language and C compilers and imposed them on my assembly code, since I was familiar with those, and know how to make working software using them. You could use another language's conventions. Or you could veer off into the wild, but there be dragons.
- titzer 4y agoWriting is almost never the issue. It's debugging that is the absolute nightmare, because it is so tedious to reconstruct what happened in terms of low-level things. Without names, data structures, functions, human memory quickly becomes overwhelmed. The most complicated assembly I've written recently is a fast Wasm interpreter. The only way I could tackle it effectively was to write the implementation of just one or two bytecodes at a time, test them thoroughly and in isolation. We're talking about writing only a handful of instructions per commit. There's just no way you can debug much more than that at a time. You need to build little bricks of fully-working code and stack them, ratcheting up the progress, so that when something breaks, most of the things you don't need to debug.
- artemonster 4y agoI find debugging assembly a breeze IF you have a full trace of all registers (ie in a simulation)
- dmoy 4y agoWell, and you remember what all the registers do. I once burned a couple hours on a microcontroller because I had transposed bits 5 & 4 on some register that dictated a certain i/o behavior and caused things to be really weird. One register out of like 11 registers that affected how that i/o operated, one bit transposed, whole thing was inscrutable.
- MichaelZuo 4y agoIt seems like having a diagram would help a lot, maybe some kind of 2D AutoCAD with a visual representation and their logical relationships? Or a Verilog for software?
- emrah 4y ago> Writing is almost never the issue. It's debugging that is the absolute nightmare, because it is so tedious to reconstruct what happened in terms of low-level things. Without names, data structures, functions, human memory quickly becomes overwhelmed. People claim GPT is creative and when I disagree, they say mixing to come up with a new combo is creative and that humans do it for creativity too While that might be true, what's even better is if GPT could read assembly code, find things that can be fixed and optimized and write new more optimal better code. Of course this can be done in any language. If we could get GPT to optimize our code libraries to reduce code size, memory footprint etc that would be a huge win for humanity and also show creativity. In short, it should tackle the "why is hello world app in X language taking up 100MB" problem :)
- delta_p_delta_x 4y ago> Writing optimized assembly is a whole other question. To be fair, that's the compiler's job; everything from dead-code elimination to loop auto-vectorisation and auto-parallelisation is pretty hard when done manually.
- yawpitch 4y agoReally liked this post and overall the entire site’s content seems great; especially like the simple way of expanding additional detail — though maybe that could be done with more semantic <details> and <summary> tags rather than <div>? — but I really wish it was more mobile friendly… might be the arbitrary initial-scale value, but not sure at a glance… on my iPhone, at least, the page loads with a good portion of the content overflowing offscreen and I need to manually pinch to zoom and drag all over to read the content.
- indigoabstract 4y agoIt may be a dying art today, but up until around 20 years ago there were still PC games coming out written entirely in assembly. RollerCoaster Tycoon is probably the most complex one, but also Transport Tycoon and Locomotion, all by Chris Sawyer. Though fun to play, I think it would be a nightmare to be fixing bugs in such mammoth assembly projects..
- indigoabstract 4y agoThis post stirred up some memories from long ago, when I was trying to understand assembly. I had written a program to test my knowledge, not very long, a couple of pages, and when I thought I was finished, I ran it to see how it worked. It didn't. It was crashing and I didn't know how to use a debugger with assembly programs back then. I combed frantically for the bug. Some hours later, it turned out I was missing a 'ret' in some function.. The assembler didn't complain, of course, and the program happily continued executing whatever was there after reaching the end of the said function.
- deathanatos 4y agoIt's all fun and games until you single-step into something like strchr: https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/multiarch/strchr-avx2.S;h=3d4a659b93f32948b20393e1ab8eab9c8614ddb4;hb=HEAD https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86... (And that's the source: remember the disassembly will lack comments and macros.)
- CodeAthlete 4y agoAI will do this for us, no?
- okaleniuk 4y agoYes. But you will have to disassembly the AI to see if it does this correctly :-P
- makach 4y agonot necessarily. also need to stop calling it AI because it is not. It works on existing knowledge. Anything new or unfamiliar it will not be able to help you.
- m_mueller 4y ago> Anything new or unfamiliar it will not be able to help you. Isn't this being disputed for GPT-4 [1]? [1] https://arxiv.org/abs/2303.12712 https://arxiv.org/abs/2303.12712
- lionkor 4y agoSorry, but you can get emergent behavior that LOOKS intelligent anywhere, and the only way you cant know is if all you get is a single way to observe it. As long as people weren't ready to look at human anatomy, they could only observe symptoms and make super wild guesses. That is what this is. Youre observing a thing in a black box with a screen, and youre reading too much into it if anything in the past is to be believed. Show a person a cellular automaton and dont explain it to them, leave them with it in a room for a month, see what their theories are. You bet it wont be "theres three rules and a random number generator", even if thats all there is.
- m_mueller 4y agoIf there is such emergent behavior that looks intelligent, how can you tell it apart from human intelligence? Devising a test that can answer this is an area of active research. Let's say for instance that it can come up with a novel mathematical proof that was not part of its training data - would you accept that as evidence towards AGI?
- ggm 4y agoI had to do clean room disassembly as my first post uni job, documenting a serial line comms protocol for a guy emulating a hospital information system. Naivety led me to burn a week reverse engineering the BIOS jump table for IO processing and the half byte shuffle to pass data out a serial port (this was a pre msdos system, it wrote the byte as nybbles to the serial UART). The actual data structure framing was easy once I got my wind. Fortran rewrite from the assembly from a memory dump. I still have the annotated listing 41 years later. It's like the rocky horror picture show: you take a JMP to the left, and turn around...
- coolandsmartrr 4y agoI love the allusion to Rocky Horror.
- bitwize 4y agoBut it's the arithmetic shift that really drives 'em insane...
- pjc50 4y agoGood principle. Writing assembly is painful and very rarely necessary. Automated RE tools like Ghidra can do a lot, but there have been more circumstances where I've benefited from reading assembly than writing it. Especially on "embedded" systems, or little bits of reverse engineering of existing systems.
- throwaway85858 4y agoBack when I had to use MSVC++ instead of stepping through using the debugger I would just open the dissambler and find the JMP instruction, saved a ton of time when dealing with templated C++.
- isaacfrond 4y agoOn my first job, I wrote a Sha-1 implementation in 8088 assembly. The platform was a smart card, but there were a few additional proprietary instructions. So, a real chance to beat the compiler, who didn't know those instructions. Ten years later, when I met someone from my old job, it turned out the assembly was still used. Probably the only consequential thing I did in all the years I worked there.
- saagarjha 4y ago> Most of the time we read disassembly only to answer one simple question: does the compiler do what we expect it to do? Actually, most of the time I read disassembly to figure out why something crashed. It's a useful skill in many contexts!
- deleted 4y ago[deleted]
- kalal21 4y ago[flagged]
- Xophmeister 4y agoMeta-comment: I really love the expanding explanation hyperlinks in this article. It's a much better alternative (imo) than say, linking in a new tab, or a pop-up, etc.
- matthberg 4y agoMe too, I found that aspect of the site design really cool. Just another perk of hand-made personal sites instead of things like medium. I think it might be even more useful with a faint background color so you could skip ahead to the end of the parenthetical if you figured out what you wanted to a few words in. Overall really cool feature though, which I'm tempted to steal haha.
- okaleniuk 4y agoBackground colors are a great idea, I'll do that!
- okaleniuk 4y agoDone! All the expandables have background colors now. https://github.com/akalenuk/wordsandbuttons/commit/22ef6295cade5a81fd36c041f3827b22661265bc https://github.com/akalenuk/wordsandbuttons/commit/22ef6295c... Thanks for the idea!
- nkurz 4y agoWhereas I didn't realize they were links, and never thought to click on the oddly worded text below the examples. For others like me, the plain text label in a square box below the listings that says stilted things like "The one on the left does" expands when clicked to give a useful explanatory paragraph. Seems like a good approach, but I could have used a better affordance: a cursor change on hover, underlined blue text, even maybe a tool tip that says "click me for the answer you idiot".
- okaleniuk 4y agoGood point! I'll think what I can do.
- deleted 4y ago[deleted]
- bitwize 4y agoI began my journey learing AArch64 assembly by writing little C programs on my Raspberry Pi and examining the compiler-generated assembly. This gave me some vital clues, not only in terms of the instructions to use, but also conventions like how the stack is handled and how system calls are done (on Linux, anyway).
- amrb 4y agoI like Binary Ninja for it's IL output, at least you get the option to zoom in and out depending on how fine you need to step through a section.
- psychoslave 4y agoWhy is assembly locked into an ocean of cryptic sequence of letters that no respectable dictionary will try to pretend to be a word? Or is there an assembly that get rid of this gibberish tradition?
- okaleniuk 4y agoI tried to make a readable assembly once: https://github.com/akalenuk/milasm https://github.com/akalenuk/milasm But apparently, this is not a win-win, but a lose-lose since this notation is hated by both assembly programmers and not assembly programmers :-D
- psychoslave 4y agoInteresting project, thanks for sharing. That seems to be a bit tangent to my remark though: this seems to be more about adding syntactic structures, embodied with miscellaneous brackets, and there are still some obfuscating abbreviations like "ldstr".
- okaleniuk 4y agoAh yes, but the whole point of the project is to go away from this: ldstr "Hello!" call void [mscorlib]System.Console::Write (string) And come to that: ((write: "Hello!")) So `ldstr` does indeed feature in the example but only as a "before" picture. But you're right, it is about adding syntactic features to the existing language.
- userbinator 4y agoBecause it's its own language. Or is there an assembly that get rid of this gibberish tradition? Look up Randall Hyde's HLA. The famous author of various Asm books thought he could make a better syntax, and... almost no one wanted it.
- pclmulqdq 4y agoAssemblers for DSPs go so far as having arithmetic operators like "+" and "=" in their vocabulary, as well as allowing things like hierarchical braces as syntactic sugar if you want them. But you are intended to write assembly for them, so making it almost C-like has a lot of benefits. The real answer is that nobody is supposed to write assembly, and the way the instructions are written is very easy to parse. The only core that doesn't have the "it's ancient and we didn't know better" excuse is RISC-V, IMO.
- twic 4y agoThere's some flag you can give the JVM which will make it dump out the assembly for JITted code. That is an enlightening read. There is all sorts of weird stuff going on in there to do with safepoints, dynamic deoptimisation, etc.
- kaba0 4y agoYou can also use JITwatch to do it from a GUI, similarly to what godbolt does (showing corresponding byte code)!
- secondcoming 4y agoI don't expect people to write assembly code, but I do expect them to at least understand it. So I've put an 'explain what this code dump does' part in my team's interview process. (This obviously wouldn't apply to web dev roles.)
- lionkor 4y agoOh, interesting, could you share an example? It could be anything from strlen() to some SIMD optimized super loop of doom, so I'm curious what it is like
- secondcoming 4y agoIt’s nothing too complex really, they’d be expected to know what a memory access looks like and the branching mnemonics. So they’re given the code and explain their understanding of it and maybe try reverse-engineer it back into C/C++ etc. SIMD might be too masochistic!
- xxs 4y agoreasoning about SIMD code on an interview is a really high ask for a non-specialized position. I'd not even know all of the instructions w/o a reference. OTOH Assembly (w/o the vector instructions) is generally like a very simple C [I'd exclude some of the memory barrier on weaker memory modes as well]
- klelatti 4y agoGiven the popularity of Compiler Explorer there must be lots of people out there reading assembly language part time! I’ve personally found it incredibly helpful in trying to understand exactly what AVX512 code compilers can be coaxed into generating. Thanks Matt!
- ajsnigrutin 4y agoI had a few year of expericene with AVR assembly (x86 was a pain)... but getting younger people to learn it is a pain. What would be the best way for someone today to learn assembly? Which architecture, chip, devkit etc. would be an ok start, without too many complications (init process, toolchain, good documentation, etc.), preferably on linux?
- delta_p_delta_x 4y agoIf you just want to teach assembly (assuming this includes the datapath/control units, the CPU pipeline, hazards, etc) today, you can't go wrong with Patterson and Hennessy's Computer Organisation and Design. Pick your version: MIPS[1], ARM[2], or RISC-V[3]. There's really no need to delve into hardware when teaching assembly, as software simulators/interpreters exist; there's QtSPIM[4], a LEGv8 simulator from ARM itself[5], and a RISC-V interpreter by Cornell[6]. [1]: https://www.amazon.com/Computer-Organization-Design-MIPS-Architecture-dp-0128201096/dp/0128201096/ref=dp_ob_title_bk https://www.amazon.com/Computer-Organization-Design-MIPS-Arc... [2]: https://www.amazon.com/Computer-Organization-Design-ARM-Architecture/dp/0128017333/ref=d_pd_vtp_sccl_4_4/130-0221064-4572619?content-id=amzn1.sym.8e065679-52e9-4d16-ae63-fa3d08b93cef&psc=1 https://www.amazon.com/Computer-Organization-Design-ARM-Arch... [3]: https://www.amazon.com/Computer-Organization-Design-RISC-V-Architecture/dp/0128203315/ref=d_pd_vtp_sccl_4_1/130-0221064-4572619?content-id=amzn1.sym.8e065679-52e9-4d16-ae63-fa3d08b93cef&psc=1 https://www.amazon.com/Computer-Organization-Design-RISC-V-A... [4]: https://sourceforge.net/projects/spimsimulator/files/ https://sourceforge.net/projects/spimsimulator/files/ [5]: https://github.com/arm-university/Graphical-Micro-Architecture-Simulator https://github.com/arm-university/Graphical-Micro-Architectu... [6]: https://www.cs.cornell.edu/courses/cs3410/2019sp/riscv/interpreter/# https://www.cs.cornell.edu/courses/cs3410/2019sp/riscv/inter...
- sureglymop 4y agoAt my school we had to write a base64 encoder/decoder in x86 assembly last year. Then we moved on and wrote a small shell in C. We had a lot of theory but the practical parts are what made it work imo. When working on the C stuff i often used compiler explorer to see which assembly is generated.
- 4y ago