12 ms·
Say hello to x86_64 assembly, part 4
- _RPM 12y agoAt my University, I really wished our assembly class was taught in x86_64. Unfortunately, we learn IBM System/360. I'm not well versed enough in Assembly to know if learning one "flavor" of assembly would help me learn another "flavor" faster. But I do know that I use a x86_64 machine myself. Perhaps, Knowing how to write assembly for that instruction seems like it would be more useful.
- nightcracker 12y agoThe important things you should learn in your computer architecture class is how assembly and other low-level details work in a pipelined register machine. The exact architecture doesn't really matter as the knowledge is easily transferable.
- wglb 12y agoI think it will. In any case, it is good to know at least two. I don't mean master, necessarily, but will give you broader perspective on how stuff works.
- acqq 12y agoThe dump doesn't have to be AT&T syntax: objdump -M intel -d program_name
- timtadh 12y agoDoes anyone, (the author?), know the best reference for x86-64 assembly? I have been a loath to teach it to students because there I haven't found great material for it. I use "Programming from the Ground Up"* as my main text for 32 x86 but haven't found anything comparable for 64 bit. I also use: * http://download.savannah.gnu.org/releases/pgubook/ProgrammingGroundUp-1-0-lettersize.pdf http://download.savannah.gnu.org/releases/pgubook/Programmin... http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/ http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in... Sun Microsystem Assembler Reference http://docs.oracle.com/cd/E19455-01/806-3773/index.html http://docs.oracle.com/cd/E19455-01/806-3773/index.html (not because we use that assembler, we use gas for gcc compat, but it very clearly describes all of the usual 32 bit opcodes) The Intel manuals http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html http://www.intel.com/content/www/us/en/processors/architectu... Another Intel manual http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-1-manual.pdf http://www.intel.com/content/dam/www/public/us/en/documents/... EDIT: I agree with everyone one, I also learned via doing, decompiling small C programs, gdb, and manuals. However, I am looking for something a bit gentler. That process will work for the top 10% but the rest will struggle and at the end of the day they need to write an x86 code generator so without knowing how to write assembly they are in trouble.
- stephencanon 12y agoI learned x86_64 by (a) reading assembly other people wrote, (b) disassembling compiled code, (c) the Intel architecture manuals, and (d) writing a ton of assembly and fixing my mistakes. This is something of the sink-or-swim approach, but I find it works really well; at the end of the day, assembly is really, really simple--the key thing is to just start writing it. Of course, it's hard to do this sort of immersion in a course where some of the students aren't really interested and just want to know what they need to complete the assignments, so the efficacy depends a lot on the nature of the class.
- Cyph0n 12y agoMay I ask why one would learn x86-64 assembly other than for extreme use cases like compiler design and reverse engineering? Modern languages and their compilers can usually produce the best possible machine code most of the time. Further, embedded systems run on ARM nowadays which is a bit simpler to deal with due to the smaller instruction set.
- 0xAX 12y agoMaybe for understanding how computer works? What occurs when you launch program, what occurs between turning on computer and that moment when you see login screen, what is it printf("Hello world") and etc... Many interesting questions. Of corse assembly is not answer on all questions but the way to it
- psykotic 12y agoIf you care about optimizing your C/C++ code, you need to be reading the assembly language output from the compiler on a regular basis. You will constantly stumble over cases where the compiler isn't doing what you expected. For example, it might be reloading memory in an inner loop instead of caching its value in a register outside the loop because its alias analysis was inconclusive. Or not enough inlining is happening where you expected it. Or there's too much inlining like when a side exit to a cold function is inlined within an inner loop. This last case exemplifies a general issue that ahead-of-time compilers have with identifying code paths as hot or cold. You can sometimes give hints to the compiler, but you must verify that the hints were actually taken in the way you expected. Obviously you only dive into this level of detail with a piece of code once you have reason to believe it's warranted (e.g. profiler information).
- drv 12y agoI am befuddled by the idea of saving the return address explicitly (mov rdi, $ + 15) while also using call/ret. Maybe there is something clever going on here, but I don't understand the purpose. calculateStrLength is not only calculating the string length; it's also pushing the contents of the string onto the stack (and expanding each byte to 8 bytes, because that's the only valid size of push on x64). The stack pointer is no longer pointing to the return address, so the code puts the return address (which was calculated by hand by looking at the dissasembly and counting bytes rather than using a label for some reason, but I digress) back onto the stack and then using RET to pop it right back off and jump to it (PUSH RDI + RET is equivalent to JMP RDI). Then reverseStr loops over the string length, popping a character off the stack one at a time and writing all eight bytes into the output buffer (running off the end of the reserved buffer for the last 7 characters, incidentally). I suppose this is an interesting exercise to understand how the stack and call/ret mechanisms work, but it's not the way to write understandable code. Using the stack in this way will almost certainly also break the return address prediction that modern CPUs perform, making the code slower than the straightforward version that doesn't use the stack at all.
- phire 12y agoIt won't break return address prediction, as long as the call/returns are matched prediction will still work. Its a symptom of doing really ugly things with the stack
- lgeek 12y ago> Its a symptom of doing really ugly things with the stack Yes, that's spaghetti code. In general I strongly recommended to write assembly functions which follow the platform's ABI (I'm a TA for a course using assembly programming). At the very least, function calls and returning values should be consistent and function calls shouldn't have this kind of side effects. In this case * calculateStrLength effectively leaks stack space because it pushes the input string and doesn't balance the stack before returning - this is why the return value is passed using rdi. At least the return value pushed by call could have been preserved in calculateStrLength itself. It also increments the size counter without resetting it (it's reset to 0 in _start) which is a bit of a weird choice. * then reverseStr (which is not a function but just a label in _start) pops the string off the stack and copies it to the OUTPUT buffer (but doesn't copy an end of string NULL character - which works because the buffer is in the bss section, but it's not a good practice). The return address pushed by the call to calculateStrLength is left on the stack. The whole thing is designed pretty weirdly because reversing a string is trivial to do in place (or with just an input and output buffer), there's no need to use the stack.