5 ms·
Also Matt Godbolt's gcc explorer is the the bee's knees for understanding assembly https://godbolt.org/ https://godbolt.org/ I think that playing around with
by adamnemecek 10y ago
Also Matt Godbolt's gcc explorer is the the bee's knees for understanding assembly
https://godbolt.org/ https://godbolt.org/
I think that playing around with it for 2 hours will teach you more than most classes on the topic. It really drives home why interactivity is such a bit deal in education.
You should also try writing a script for counting instructions in binaries. It's pretty illuminating. Here are some sample statistics
https://webcache.googleusercontent.com/search?q=cache:j0gebKGwf5YJ:https://www.strchr.com/x86_machine_code_statistics+&cd=1&hl=en&ct=clnk&gl=us https://webcache.googleusercontent.com/search?q=cache:j0gebK...
- makmanalp 10y agoThis is amazing. After a few minutes I learned so much already - try with an empty function that returns 0, then return an argument, then return an argument +1, argument^2, etc. As an aside, I was looking at the header code generated by gcc to handle the initial function call. What's the convention on how we assign parameters to registers? I'm trying stuff out, and the first paramter always seems to be edi, then esi, eax. Except when I change the types, and it turns into xmm0 and the like. And the result returned is eax too, except when it's a float, then it's xmm0. How do things know where to look?
- adamnemecek 10y ago> How do things know where to look? The compiler generates code that uses the correct register. So the compiler picks a register into which it will put the result and then generates code after the calling location that gets the result from the correct register. And yeah, there's quite a bit of surprises. E.g. I found out that gcc is smart enough to perform tail call optimizations https://godbolt.org/g/MZDmwP https://godbolt.org/g/MZDmwP
- makmanalp 10y agoOK, so it's not a magical convention - it works it out bottom-up. First decide on registers for each parameter when you generate the code for the function, then based on that generate specific code for the instances where you call that function. Cool, thank you. Also that example, heh. I tried to go back gcc versions to see if there was a case where it didn't do TCO - nope. Also, I like how returning 0 is "xor eax, eax".
- MichaelBurge 10y agoSuppose you're right and the registers are arbitrary. Then how would foreign function calls work? If you're compiling Rust code that calls into a C library, how does it know what registers to use? So the choice of registers cannot be arbitrary, unless the compiler knows the function is only used within an object file. The registers are predetermined by a convention unless you use the 'static' keyword to signal that the function is only used internally to a module, so the compiler has complete freedom to choose registers.
- pjmlp 10y agoThat is the role of operating system ABI, which definines the calling conventions between the programming languages on the OS.
- makmanalp 10y ago> If you're compiling Rust code that calls into a C library, how does it know what registers to use? Ah, interesting, I figured that the generated object files would just store some metadata on that basically.
- MichaelBurge 10y agoThat can work for statically-linked object files, but what about dynamically linked? You can load one with a function call, get back a function pointer, and invoke it like any other function. Trying to use some metadata would slow down the function call significantly, even if you tried to cache it somewhere.
- userbinator 10y agoThen how would foreign function calls work? If you're compiling Rust code that calls into a C library, how does it know what registers to use? By using information kept with the function, or perhaps even encoded into the function name itself (as already happens when distinguishing between different calling conventions, or in the case of C++ name mangling)? Coming from an Asm background, where there basically is no one "calling convention", and programmers would document which registers (almost always registers, rarely the stack --- and that can make for some great efficiency gains) are for what, I've always wondered why that idea didn't seem to go far.
- Merad 10y agoThis is determined by the calling convention [0]. If you're interacting with code that you didn't write, you need to know the calling convention that they use in order to set up arguments correctly. 0: https://en.wikipedia.org/wiki/X86_calling_conventions https://en.wikipedia.org/wiki/X86_calling_conventions
- richchurcher 10y agoThis is such gold: thanks!
- JoshTriplett 10y agoOne great example to try: add -O3 to the compiler options, then write a function returning its argument multiplied by 2, 3, 4, ... You'll see the various ways the compiler avoids emitting an actual multiplication instruction. The smallest multiplier I found that actually produced an imul: 46.
- boterock 10y agoInteresting example, I came up with clang 3.9 producing an imul with 11 though.
- JoshTriplett 10y agoWith optimization turned on? GCC produces an lea that multiplies by 5 into %rax for the return value, then another lea that multiplies that by 2 and adds the original.
- MichaelMoser123 10y agoYou still need an overview of the instructions and registers. Knowing where the operator and operand of the instruction is also helpful. (i always mix up intel and at&t notations)
- jwr 10y agoWow, thanks for posting this — it is a fantastic resource! I love it even more for using Intel x86 assembly syntax, instead of the horrible AT&T syntax.
- Graziano_M 10y agoAT&T is still an option. Which is fantastic because it's a quick way to see how the two differ.
- pmoriarty 10y agoOne of the most useful tools that helped me learn assembly was the Ketman Assembly Language Tutorial.[1] As I step through an assembly language program, it gives me instant visual feedback of the contents of each register, flag, and memory. I'm really surprised that there's nothing remotely like this on the web yet, and that I have to resort to running dosbox or freedos to have access to this super useful tool. [1] - http://www.oocities.org/siliconvalley/office/6208/ http://www.oocities.org/siliconvalley/office/6208/
- umbs 10y agoUnfortunately, the tool linked from that page (it's archived) can't be downloaded.
- pmoriarty 10y agoI think you can still find it hosted at other sites by doing a web search for "Ketman Assembly Language Tutorial" or "asmket16.zip". I also just created a .tar.xz file of my own copy of the tutorial and uploaded it to: https://www.datafilehost.com/d/6d9086c8 https://www.datafilehost.com/d/6d9086c8 I also did a base64 encoding of the same .tar.xz file and pasted it here: https://bpaste.net/raw/2521d4e2a230 https://bpaste.net/raw/2521d4e2a230 The sha256 checksum of the .tar.xz file is: 41e5d16b2e0d15f67535d725baa2fa2fec00f65003e4745b60a3fb082111f5ff Please note that this is not an official version of this tutorial. It's just what I had lying around on my disk after who knows how many years. Use at own risk. If you want an official copy, you could try contacting the author at btketman@btinternet.com