13 ms·
X86 assembly doesn’t have to be scary
- secure 8y agoI love the built-in demos!
- zokier 8y agoIf your intention is to avoid scaring newbies off, I'm not sure if 16bit real mode, PC boot process, BIOS services and all that arcana that follows is the best place to begin.
- pjmlp 8y agoThat arcana felt liberating coming from a Timex 2068. Different times, I know.
- linker3000 8y agoDifferent times - in X86 Assembly: https://github.com/linker3000/Historic-code-PC-Pascal-and-ASM-/blob/master/QT2.A86 https://github.com/linker3000/Historic-code-PC-Pascal-and-AS...
- JustSomeNobody 8y agoWhy not? If they're determined enough, they'll get it. I learned a lot back in the day by spending many hours with the book PC Intern and some other book on x86 Assembler. Granted, I had a little experience with assembler on a C=64, but, I learned _that_ in a similar way.
- xboxnolifes 8y ago> If they're determined enough, they'll get it. That's the exact opposite mindset of someone trying to "not scare newbies away."
- krylon 8y agoOn the other hand, I think it is fair to assume that people who are interested in assembly programming at all bring a certain degree of determination to the table.
- JustSomeNobody 8y agoI think the determination of newbies is being underestimated. A lot of us "old timers" didn't have the internet. Just a few books and a whole lot of "Imma figure this crap out!".
- msla 8y agoI agree with you to an extent: The IBM PC was not a very elegant design, and other computers certainly had less complexity to them. But there is a point I want to make which defends this choice somewhat: Newbies like me are more frustrated by thinking there's no path from introductory material to something useful or realistic. Something that's more immediately friendly would be a simplified virtual machine with no or trivial peripheral hardware, like Redcode in Core War: http://vyznev.net/corewar/guide.html http://vyznev.net/corewar/guide.html The death of education is "So What?": "OK, I've learned to play Core War and I know enough to write a warrior that can occasionally beat other warriors written by beginners. So far, so good... so what? I want to program computers in assembly, not play games using assembly programs, so where's the path from where I am now to where I want to be?" In this case, the pathway from VM opcodes to native opcodes is hardware interface and, while the IBM PC has some funky hardware, it's heartening to know that your code could, in principle and barring emulation errors, run unmodified on real hardware and do something. Not something useful, but you can get to useful. You can ramp up to it, now that you have the pathway in front of you. It might be a long pathway, but it's there.
- zokier 8y agoWell, I had in mind something more useful and realistic, not less; namely teaching assembly in normal Linux environment. Sure, there are all sorts of complexities there too, but in general I feel like they are also more worthwhile.
- msla 8y ago> Well, I had in mind something more useful and realistic, not less; namely teaching assembly in normal Linux environment. This might sound odd, but as someone who's done some assembly programming under Linux, it usually isn't different enough from C to be worth the effort. Even if you eschew libc, the kernel APIs are still fairly high-level and, more to the point, there are no new concepts relative to C: You have pointers and pointer arithmetic, you have fixed-size buffers, you have ints and doubles, and the rest is just syntax. The exception is doing something like a really tight high-performance kernel using SIMD opcodes, which necessarily involves learning a lot about the specific SIMD hardware and data organization to optimize cache use and other details C can't express.
- jonhendry18 8y agoIt seems like a better introduction would have the aim of helping the newbie learn assembly that would be useful in debugging or working around bugs in code that you don't have source to.
- pjmlp 8y agoLoved the article. Then one can follow up with some MS-DOS classics. https://www.amazon.com/Peter-Nortons-Assembly-Language-Book/dp/0136619010 https://www.amazon.com/Peter-Nortons-Assembly-Language-Book/... https://www.amazon.com/Advanced-S-DOS-Programming-Microsoft-Programmers/dp/0914845772/ref=pd_lpo_sbs_14_t_2 https://www.amazon.com/Advanced-S-DOS-Programming-Microsoft-... https://www.amazon.com/Peter-Norton-Programmers-Bible-programming/dp/1556155557/ref=pd_lpo_sbs_14_t_0 https://www.amazon.com/Peter-Norton-Programmers-Bible-progra... https://www.amazon.de/PC-Intern-Programming-Encyclopedia-Developers/dp/1557551456/ref=sr_1_2 https://www.amazon.de/PC-Intern-Programming-Encyclopedia-Dev...
- zerr 8y agoObligatory mention: John Socha is the author of Peter Norton books as well as Norton Commander.
- rzzzt 8y agoOne more from Mr. Norton: https://www.amazon.com/Peter-Norton-Programmers-Guide-IBM/dp/0914845462 https://www.amazon.com/Peter-Norton-Programmers-Guide-IBM/dp... It covers a wide range of interrupt routines, both DOS and the BIOS ones. Also, some PCJr sound chip programming got snuck in.
- mhd 8y agoI see your Norton and raise you an Abrash: https://www.amazon.com/Zen-Assembly-Language-Knowledge-Programming/dp/0673386023/ https://www.amazon.com/Zen-Assembly-Language-Knowledge-Progr... https://www.amazon.com/Zen-Graphics-Programming-2nd-Applications/dp/1883577896/ https://www.amazon.com/Zen-Graphics-Programming-2nd-Applicat... https://www.amazon.com/Zen-Code-Optimization-Ultimate-Software/dp/1883577039/ https://www.amazon.com/Zen-Code-Optimization-Ultimate-Softwa... Zen of Asm is also online, I think a few of the other works, too. http://www.jagregory.com/abrash-zen-of-asm/ http://www.jagregory.com/abrash-zen-of-asm/
- pjmlp 8y agoThat is unfair. :)
- krylon 8y agoI think to many programmers assembly is the "GOTO" of programming languanges: From the day you start learning to program, you are told that all this fancy high-level-language stuff is there so you do not have to deal with assembly. So most people never go there. I did go there, briefly, about ten years ago. It was wicked fun. But all in all, I may have written maybe 20 or 30 instructions of assembly in total. I did try to rewrite a few small, heavily-used functions from our code base in assembly only to discover that the code I came up with was practically identical to what the compiler emitted. At that point I figured that the people who told me that "you can't beat the compiler" were probably right and called it a day[0]. Alas, I never had the hardcore performance requirements that would make go back there. But it was fun to get a taste of it. [0] At the same time, I was kind of proud that I did not get beat by the compiler. Then again, those functions were fairly trivial.
- ahartmetz 8y agoI always describe it as: Assembly is dead simple, it is only hard to write nontrivial programs in it. Manual register allocation, other micromanagement, and the necessary changes when you change the logic, that is hell.
- scottlamb 8y ago> At that point I figured that the people who told me that "you can't beat the compiler" were probably right and called it a day On that subject...my understanding [0] is: These days, the best bet for beating the compiler is to use vendor intrinsics (for SIMD, encryption, bit-twiddling, etc). Shaving an instruction off the inner loop might give you a few percent; using SIMD lets you operate on 256 or 512 bits per instruction instead of 8, 16, 32, or 64. You might be able to show your inner loop is memory-bound (and thus prove further improvements have to come from algorithmic improvements / better cache locality, rather than continuing to fiddle with instructions). The compiler automatically uses SIMD sometimes, but it can't do so reliably: * The transformations require things the compiler isn't allowed to do, like increasing alignment of key variables or altering the larger algorithm. * code that might run on older processor revisions needs multiple implementations selected at runtime. I think gcc has some magic extension ("target_clones"?) to do this relatively easily; otherwise you might need to write your own logic to decide which function pointer to use. Note that each "vendor intrinsic" matches one assembly instruction, and it's valuable to understand assembly while writing them, but the actual code you check in can end in .cc (C++) or .rs (Rust) or whatever. Doing so means it can be inlined into functions written in the higher-level language, you don't have to encode knowledge about the platform's calling convention into your code, etc. [0] Not from personal experience. Corrections welcome.
- ChrisSD 8y agoI wonder if the history of x86 is holding us back in a big way. It started out being close to the metal but now it's an abstraction that can mislead you if you think processors are literally working the way x86 assembly describes. And surely the whole spectre issue could be lessened if we could be less reliant on CPUs having to guess what to keep in cache, which code paths are most likely, etc?
- krylon 8y agoWell, Intel has tried repeatedly to replace x86 (AXP32, i860/i960, Itanium), but in a way, they were a victim of their own success.
- jecel 8y agoActually, the iAPX432 came first in 1976. When it became obvious that this project was going to take longer than expected (it was only launched in 1981) Intel did a quick extension to the 8080 to keep customers from going to the competition while they waited.
- krylon 8y agoThank you for pointing that out!
- convolvatron 8y agothere was at least one non-realized research project where the cache was exposed as part of the architecture to be managed by the compiler (ppc lets you do some of that). i think thats a potentially very fruitful approach, but would it have helped the spectre situation in any way? oh, what you're suggesting in an analog for compiler driven speculation? that may have helped, and is also probably worth thinking about
- ryanianian 8y ago> but would it have helped the spectre situation in any way? Presumably a compiler could put cache-lookups and/or cache-waits in non-predictably (perhaps choosing to do so when it suspects a cache-miss anyway).
- kowdermeister 8y agoI wasn't really familiar with webassebly, but when I've seen this source for fibonacci I immediately recognized familiar features which can't be said of the rest of the assembly implementations: https://github.com/nebulet/nebulet/blob/master/wasm/fibonacci.wat https://github.com/nebulet/nebulet/blob/master/wasm/fibonacc... https://github.com/Hanks10100/wasm-examples/blob/master/simple/math.wast https://github.com/Hanks10100/wasm-examples/blob/master/simp...
- tvmalsv 8y agoWhen I was about 14 I was enthralled with programming my new Commodore 64, first in BASIC, then 6510 assembly. I had the opportunity to accompany my mother to a one-day class on programming. Being just an intro on the subject, I was well ahead of what they would be discussing, but thought it would interesting to talk to some adults that were also into programming. I was talking to a couple of guys about what I had been doing on my C=64, and when I mentioned the assembly stuff I was writing, one of them said, "How can you possibly write anything with only three registers?!" (just the accumulator and x/y registers). I was wondering what the big deal was since that was the only architecture I had known at that point. Every game and utility I had were only using three registers, so it was already proven to me that three were "enough". It's funny how you can just adapt and work with whatever is available, and that becomes your norm. Especially when you don't even realize there are other options out there. Those were the days!
- gruez 8y ago>"How can you possibly write anything with only three registers?!" AFAIK you can even go down to 1 register, which is how stack machines work. You might even say it’s 0 registers because it’s not something you can directly access.
- poizan42 8y agoThere are many simple microcontroller architectures with only one register, usually called A (accumulator) or W (work register). You just need to constantly load memory to and from the register, nothing weird in that. Sometimes they will call their memory locations registers, in which case they have lots of registers - the terminology gets quite unclear when everything is on the same chip anyways. A true stack machine does not have any registers, so that would be 0.
- blattimwind 8y agoRPN calculators are simple stack machines.
- MaxBarraclough 8y ago
- setquk 8y agoI started with 6502, then ARM in 1989 (thanks acorn!), then a little bit of x86. The latter was a culture shock. It’s was like grooming the devil’s genitals in comparison to ARM. It drove me to C and Unix where I’ve been happy ever since (occasionally a bit of PIC assembly as well). It’s not scary just nasty.
- nsxwolf 8y agoIt's hard to find a tutorial that really tells you what is going on. You have to start with whatever operating system you're on, then figure out what the heck you're reading. What is x86 and what is some reserved word peculiar to the assembler you're using? Here's macOS Hello, world code I see floating around a lot, and I can't make heads or tails of it: global start section .text start: push dword msg.len push dword msg push dword 1 mov eax, 4 sub esp, 4 int 0x80 add esp, 16 push dword 0 mov eax, 1 sub esp, 12 int 0x80 section .data msg: db "Hello, world!", 10 .len: equ $ - msg
- pjc50 8y agoBasically everything left-aligned is for the assembler, and the indented material is the mnemonics for the x86 set. The assembler breaks down as: push dword msg.len push dword msg push dword 1 Place a pointer to the message, its length, and 1 on the stack. Each of these decrements esp by 4. mov eax, 4 We'll be doing syscall 4 in a sec sub esp, 4 Reserve 4 bytes for the return int 0x80 Do the syscall! See https://opensource.apple.com/source/xnu/xnu-2782.20.48/bsd/kern/syscalls.master https://opensource.apple.com/source/xnu/xnu-2782.20.48/bsd/k... ; this is 'write' add esp, 16 Discard the return value and arguments from the stack. push dword 0 mov eax, 1 sub esp, 12 int 0x80 Similarly setup and do syscall 1 to exit.
- nsxwolf 8y agoBut then there's that "msg.len", which I assume is some sort of assembler shortcut for getting the length of an array and not part of the x86.
- pjc50 8y agoThat's referring to the label msg.len, which is a value computed at compile time in the 'data' section at the bottom. msg: db "Hello, world!", 10 .len: equ $ - msg 'msg' is a label, which will be assigned to an address at compile time. 'db' short for 'declare bytes' puts some bytes at that address. '.len' is then defined as the current output address ($) minus the start of the string.
- dcomp 8y agoWhat would be more interesting is the UEFI boot process. I don't think any new computer comes booting into 16bit real mode anymore. I would love to see a "Write your own UEFI bootable kernel" I'm sure going straight to a semi sane 32bit environment is much easier to deal with than starting at 16 and working your way up.
- kccqzy 8y ago> I don't think any new computer comes booting into 16bit real mode anymore It most certainly does. See the chapter on 8086 emulation in the Intel Software Developer's Manual, Volume 3.
- wruza 8y ago>32bit environment is much easier to deal with than starting at 16 and working your way up. How much bytes do you think one requires for an OS loader? The complex part is loading all the code into memory from a file on disk partition without having a MB-sized fs driver, stdlib and loader in it yet. 16, 32 or 64 it doesn’t really matter.
- eadmund 8y agoI would love to see a 'write your own UEFI kernel in assembly,' but every resource seems to assume that one is writing in C. I want my system to be in assembly, and to grow it from there.
- adricnet 8y agoThanks for the share this looks great! It aligns quite well with the study (in diagrams and commented assembly) of x86 in POC || GTFO starting in pocorgtfo04.pdf chapter 3 provided by Shikhin Sethi.
- draw_down 8y agoIt can lack appeal for reasons other than being "scary". Such as, it's very tedious to write and debug.
- bogomipz 8y agoTangential question - does anyone know what model IBM that is?
- wglb 8y agoIt appears to be the original IBM-PC.
- AnIdiotOnTheNet 8y agoYes, the IBM 5150 PC. In fact, the image is the one used on the Wikipedia page for "Desktop Computer".
- bogomipz 8y agoAh yes, the 5150. Thank you.
- partycoder 8y agoSome free quality resources, ready to use: - Intel CPU programming guide https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf https://www.intel.com/content/dam/www/public/us/en/documents... - Calling conventions https://en.wikipedia.org/wiki/X86_calling_conventions https://en.wikipedia.org/wiki/X86_calling_conventions https://en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions https://en.wikibooks.org/wiki/X86_Disassembly/Calling_Conven... - Web compiler output viewer https://godbolt.org/ https://godbolt.org/ - Operation code (opcode) reference http://ref.x86asm.net/ http://ref.x86asm.net/ - Disassembler/debugger https://github.com/eteran/edb-debugger https://github.com/eteran/edb-debugger, https://x64dbg.com https://x64dbg.com - Disassembler/Reverse engineering tools http://hte.sourceforge.net/ http://hte.sourceforge.net/ https://github.com/radareorg/cutter https://github.com/radareorg/cutter And this: https://github.com/codilime/veles https://github.com/codilime/veles (binary analysis tool)
- buzzier 8y ago+ "Reverse Engineering for Beginners" free book https://beginners.re/ https://beginners.re/
- zshrdlu 8y agoProbably also need some syscall references: https://fresh.flatassembler.net/lscr/ https://fresh.flatassembler.net/lscr/
- partycoder 8y agoNeatly organized. The "man" command also has documentation on syscalls. A good introduction to syscalls, with some explanation is "The Linux Programming Interface", http://man7.org/tlpi/ http://man7.org/tlpi/ (book)
- glangdale 8y agoAnyone who really wants to dig into this, and is willing to be a bit scared, should review the materials for the 15-410 operating systems course at Carnegie Mellon University. The fall edition of the page isn't up yet but there are probably some older versions kicking around. If you are at CMU you should definitely take this course, as long as your pain tolerance is high. This course teaches students to write a preemptive operating systems kernel from scratch. It is quite an experience (I TA'd it after finishing my PhD while trying to figure out what to do next). The kernels the students wrote used to be able to boot on standard (but old) PC hardware. Sadly the modern USB stack is so complex that a conformant thing to talk to a USB keyboard is pretty much as complex as the student's whole project (but less educational). So non-legacy hardware that lacks a PS/2 keyboard no longer has this nice easy path to read/write stuff to console without a lot of setup.
- inamberclad 8y agoThe problem I keep running into is that everybody's asm is different looking, and not even internally consistent. Trying to parse all of this stuff: [BITS 16] ;tell the assembler that its a 16 bit code Okay, so this is an instruction to the assembler saying that we're only working with 16 bit registers and to emit 16 bit code. Straightforwards enough here. [ORG 0x7C00];Origin, tell the assembler that where the code will ;be in memory after it is been loaded This means that the first instruction will be at physical address 0x7c00 when the code loads, right? What's the first instruction, the one below? mov ah, 0x0A ; Set the call type for the BIOS mov al, 66 ; Letter to display mov cx, 3 ; Times to print it mov bh, 0 ; Page number Makes enough sense, just mov instructions al refers to the top 8 bits of register ax and al refers to the bottom 8 bits. I see that register bh is zeroed, but what about bl? Why not xor bx, bx or something like that? Don't instructions normally go in a section named .text? How does the BIOS know where to find this code and begin execution in the first place? ... TIMES 510 - ($ - $$) db 0 ;fill the rest of sector with 0 Wtf is that? What's a sector? What the syntax here? Are we repeating this 510 times, or 510 minus some value? The db command is for writing bytes, but where are the bytes written to? DW 0xAA55 ; add boot signature at the end of bootloader Where are these bytes written? Why is DW capitalized here when db was left in lowercase before?
- wruza 8y agoORG shifts all addresses below it to given offset. That means that external loading code is supposed to put it there, otherwise all non-relative instructions will load/store at wrong locations. BL is not set because it is irrelevant. How you clear/save a register was up to you. https://en.wikipedia.org/wiki/INT_10H https://en.wikipedia.org/wiki/INT_10H .text is a section of executable format, but what you got is a boot loader. It doesn’t have sections, it’s just a first 512-byte boot disk sector that is loaded at 7C00h by BIOS and directly jmp’d into. I forgot what exactly $-$$ is, but one of those is “current” address and another should be ORG address, but I’m not sure, so this expression turns into e.g. TIMES 410 DB 0, if 100 bytes used by so far. AA55 is a boot sector end marker, something like canary value. Never looked into that in detail. Assemblers were mostly case-insensitive, like Pascal and BASIC. Case is up to you, if you have any preference.
- 8y ago
- chaotic_clanger 8y agoit seems to me that x86 assembly is the less readable kind of assembly. i always found powerpc set more thought through.
- ericnyamu1234 8y agowow.check out this cool forum http://mawazo.byethost7.com/index.php http://mawazo.byethost7.com/index.php
- mmjaa 8y agoSometimes its necessary to return to the past to remember the things we've abandoned in the rush to modernity. I'm speaking, of course, of the wonderful Prince of Persia and its delights. So many treasures to be discovered for anyone interested in even a little bit of assembly-language programming.. it definitely sharpens my chops, anyway: http://fabiensanglard.net/prince_of_persia/index.php http://fabiensanglard.net/prince_of_persia/index.php PRINCE OF PERSIA CODE REVIEW.
- CrankyCanuck 8y agoBack in the late 80's, when I was in University, we had second year course where we had to do x86 assembly on PC XT or AT clones. Assignment #1 was messing with keyboard interrupts...easy peasy. Assignment #2? Write a video game...basically we had to do the "snake game" with a never ending line you could steer as it grew. Anyhow, I have to admit pretty much the whole class figured it out...assembly was not nearly as insane as we thought. By the end of that course, I was almost as comfy writing x86 assembly as Pascal. Fun times.
- AnnoyingSwede 8y agoThe key to x86 assembler/machine code was avoiding the software interupts as they were painfully slow. Sure, when you are dealing with a 512byte boot sector you are better of offloading as much code as you can to software interupts, but for everything else the battle was to come up with something faster than the software interupts. In many cases some basic routines used in qbasic was actually faster than their counterparty interupts in asm. A grand example of this would be using mode 13h (320x200) and interupt 10 to set all pixels on a screen to a single color, which could take up to 2 seconds using machine code and bios interupts (as it verifies vertical and horizontal refresh prior to setting the pixel). Using interupts however is relativly painfree as the author pointed out.