Live data from Hacker News

A fundamental introduction to x86 assembly programming

nayuki.io

31–40 of 78 posts

Re: A fundamental introduction to x86 assembly programming

#31
post #29
post #27

Earlier quoted context omitted.

I only have a vague understanding of assembly in general, and x86_64 in general - but I thought that with so many more registers, the recommended style of programming can change quite a bit (hence aggressive use of registers for passing arguments to function calls in the x86_64 C ABIs)?

That's less of an assembly issue and more of a compiler/calling convention issue. Assembly allows you to pass parameters on the stack or in registers. Calling conventions simply define a protocol for doing this consistently.

Actually Id'd say that it's mostly the crappy processor design issue: for example, UltraSPARC has 32 physical registers in 32-bit mode, and 256 virtual registers (through register windows, specifically designed for compilers). Even Motorola 68000 with eight general purpose address registers and eight general purpose data registers is far more elegant than a 32-bit four register intel CPU.

intel CPU is just crap from a design standpoint, and since they had to remain backward compatible, it's gotten a lot faster with lots and lots of tricks, but it still sucks in 32-bit mode. No amount of tricks will change that. It has to be run in 64-bit mode to gain a performance boost and simplify the code, whereas processors with fixed 32-bit instruction encoding run faster in 32-bit mode and code simplicity is a constant.

Re: A fundamental introduction to x86 assembly programming

#32
post #17

This is a nice fundamental introduction, but where would I go to see how to actually run code?

I wonder if there is a nice and easy to use x86 simulator like the MARS simulator for MIPS [1]? Some place where I can run (step through) little x86 assembly programs study their effect of the each register. [1] http://courses.missouristate.edu/KenVollmar/MARS/

GDB?

Re: A fundamental introduction to x86 assembly programming

#33
post #30
post #9

Is there something like SPARC or other RISC architectures?

Yes, The SPARC architecture manual. Punch that into Google or DuckDuckGo and it should be the second link (you want the SPARC V9 instruction set architecture). I would post the link I found, but since I actually have the physical book, I'm uncertain as to whether posting a link to a PDF of a possibly copyrighted book would get me in trouble with Hacker News or not. I've done some SPARC assembler programming for fun,…

How much does the hard copy run you? I am thinking of porting my hobby OS to the architecture so it's probably worth having around in hard copy form.

Re: A fundamental introduction to x86 assembly programming

#34
A very nice book about assembly programming is "Assembly Language Step-by-Step: Programming with Linux, 3rd edition" (http://www.amazon.com/dp/0470497025).

The nice thing about this book is that it guides the reader at understanding how the machine works first, and only then to assembly programming.

The sad thing about this book is that it references 32 bit intel-compatible processors.

My guess is that the original author has grown old and is not interested in producing a fourth edition of such book.

On this matter, I would like to ask: is it worth learning assembly for the x86/32-bit instructions, now that pretty much every computer is built on the amd64 architecture ?

Re: A fundamental introduction to x86 assembly programming

#35
I was into asm back when Amiga was around and lost the will and knowledge over time (mostly since programming is more of a hobby now, over the last couple of years). But, I had a strong desire to get into asm again and I did it a bit unconventionally. It worked, though.

Get a TIS-100 game and play it (that got me interested again). After that, I compiled simple(r) C programs with gcc and looked at their .S output. After that (along with that) grab a tool like ollydbg, x64dbg or (if you can!) IDA Pro and open up your favorite programs and modify them. Whenever you stumble upon an unknown (to you) instruction, look it up in the intel manual and google for it to see idioms people use. This process has worked really well for me, for now, albeit it feels like I'm cracking software or something like it (it's fun though). Along with that you can start writing asm blocks in your programming language of choice and/or full asm with any of the assemblers (flat, yasm, nasm, whatever).

Only thing you need to know beforehand are the basics of C and data/memory manipulation.

Re: A fundamental introduction to x86 assembly programming

#36
post #13

Earlier quoted context omitted.

Probably no. I think when you reach some level of understanding, you'll use manuals. Then you know what you're looking for. :)

And once you start reading the docs on the more efficient but far less consistent 64-bit calling convention, you may find yourself choosing words to describe it other than the "improved" that this author opted for.

I take it you're a fan of the plan9 calling convention (f. ex.: all arguments and return value(s) are on the stack)?

Curiously, it doesn't actually appear all that ineffecient. Go uses it AFAIK. I wonder whether anyone has studied it. I also wonder whether gccgo uses that convention or defaults to SysV x64.

Re: A fundamental introduction to x86 assembly programming

#37

If you want to learn x86 assembly, I recommend one of my favorite books Programming From The Ground Up: http://savannah.nongnu.org/projects/pgubook/ (free pdf!) This is a practical book and teaches assembly programming on Linux. Author Jonathan Bartlett wrote this book because he was frustrated to no end with the existing books. At the end of them he could still ask, "How does the computer really work?" and not have…

Ha, that page is frozen in 2004, its like a web time capsule! The book is really good though. Not sure I would have found this otherwise, thanks for sharing.

Re: A fundamental introduction to x86 assembly programming

#38
post #22

Earlier quoted context omitted.

Yes there are... I wouldn't recommend starting with SPARC Asm though, it's nowhere near as fun as a CISC like x86, nor as easy as MIPS (which tends to be the "boring" go-to architecture CS courses use.) ARM is more interesting than MIPS and easier than x86.

What do you mean by ARM being more interesting than MIPS?

- More ARM systems shipping today than MIPs

- The ARM has a standard MMU (well, two major revs). MIPs has quite a few variants, and they involve TLB invalidation / reload. They're both worth working with.

- ARM has made some interesting architectural choices over the years, and it's worth studying what they've done. MIPs is more a static platform, not as much market pressure or will to innovate.

Re: A fundamental introduction to x86 assembly programming

#39
post #34

A very nice book about assembly programming is "Assembly Language Step-by-Step: Programming with Linux, 3rd edition" ( http://www.amazon.com/dp/0470497025 ). The nice thing about this book is that it guides the reader at understanding how the machine works first, and only then to assembly programming. The sad thing about this book is that it references 32 bit intel-compatible processors. My guess is that the original…

I work in an IT dept which supports almost a dozen departments that all told use about 30 or 40 apps, almost all of which are still 32-bit. The hardware is recent and all 64-bit (as is our OS) but even the MS Office we use is 32-bit because of interaction with other apps. We also have to default the browser to the 32-bit IE executable rather than the 64-bit because of plugins (even MS recommends this). Most vendors still aren't up to 64-bit yet because they don't want to shut out the customers that are still years behind on upgrading. I'm thinking 32-bit will still be around for another 10 years to be on the safe side.

Re: A fundamental introduction to x86 assembly programming

#40
post #9

Is there something like SPARC or other RISC architectures?

Yes there are... I wouldn't recommend starting with SPARC Asm though, it's nowhere near as fun as a CISC like x86, nor as easy as MIPS (which tends to be the "boring" go-to architecture CS courses use.) ARM is more interesting than MIPS and easier than x86.

I've done a little bit of x86 and I have to say I'm not very impressed by some of it. It seems like the lowest common denominator where nothing 'fun' happened.

ARM has a few ecosystem problems that I'd rather not deal with. From what I understand there is a lack of hardware discovery. ARM is more embedded then 'user' computer. Nothing is swapable.

Post reply on HN