Live data from Hacker News

Assembly Language Programming: Still Relevant Today (2015)

wilsonminesco.com

31–40 of 100 posts

Re: Assembly Language Programming: Still Relevant Today (2015)

#31

"...self-modifying code, [is] sometimes appropriate to solve certain problems that have no other solution, or for improving efficiency, as in double-indirect addressing." I have read that self-modifying code on the x86 architecture is pretty dangerous at the assembly level. More broadly, this kind of comes back to all the issues in the "C is not a low level language" thread. Some level of assembler certainly gives th…

> I have read that self-modifying code on the x86 architecture is pretty dangerous at the assembly level. Dangerous in what way?

Having written self modifying x86 code. The most annoying thing is that instructions are not a fixed width. This means patching code requires you are able to parse every instruction, or a look up table where each instruction starts or just regenerate the entire code whole cloth. This also can cause problems if self modifying code has more than one thread since you suddenly may need to update 1 to 15 bytes atomically.

Generally, easiest to do the last or put NOPs or doing something like windows hot patch point for functions. Where hot patchable functions are preceded with a 5 bytes of nops, and the function always starts with MOV EDI, EDI which again is a pretty much a NOP, but takes two bytes.

This allows one to replace MOV EDI, EDI to a short jump to the start of those 5 bytes which is large enough to hold a long jump to any code. Windows went this route because originally multi byte NOPs where not part of the spec so if you used the one byte NOPs not only would each nop need to be execute slowing down function calls, but in multi-threaded code you would have to lock all threads to edit the code since it would be fetching on byte at time ect...

Re: Assembly Language Programming: Still Relevant Today (2015)

#32
post #24

Earlier quoted context omitted.

I could be mistaken, but, don't JVM languages avoid assembly steps? They use bytecode and are translated directly to machine code; I'm not sure there is an translate-to-assembly step. I'm also not sure how much of this confusion is semantics vs actually different components of the compilation and running process.

They still have assemblers in them - they're just done with method calls rather than text. https://github.com/openjdk/jdk/blob/e73ce9b406c34bd460f0797f... Also if you look at the compiler's intermediate representation at the very last phases you'll see it basically looks like an assembly program.

Pure interpreters like CPython?

Re: Assembly Language Programming: Still Relevant Today (2015)

#33

Earlier quoted context omitted.

They still have assemblers in them - they're just done with method calls rather than text. https://github.com/openjdk/jdk/blob/e73ce9b406c34bd460f0797f... Also if you look at the compiler's intermediate representation at the very last phases you'll see it basically looks like an assembly program.

Pure interpreters like CPython?

The interpreters themselves go through assembly language. Assembly is relevant because the whole stack is still actively built on it.

Re: Assembly Language Programming: Still Relevant Today (2015)

#34
post #29
post #28

Earlier quoted context omitted.

I read that line as suggesting that when using HLL the programmer generally does not have control over the assembly that is generated. I guess that is why sometimes, as the author suggests, the programmer may write part of a program in HLL and another part in assembly in order to achieve increase execution speed.

It's more than just execution speed. It could something as simple making a function using assembly that swaps the virtual memory page table on a processor. That's not something you gonna find in a high level language.

Yup. The example I implemented in college is stack pointer manipulation to implement multithreading (think pthread).

Some things can only be done in assembly.

Re: Assembly Language Programming: Still Relevant Today (2015)

#35
I'm rarely keen on posting negatives on articles that clearly took a lot of time to make, but I think this requires a bit of correction.

I think this article is very, very simplistic. All of it relates to a 8 bits CPU that is 40+ years old.

I switched to HLL as soon as I could get my hand on a compiler, namely, UCSD Pascal at the time! Then the Pascal, then to C and then myriads of other languages. I covered 6502, Z80, 68k (all of them, to 68040), PowerPC (all of them from 601 prototypes to G5s), ARMs (more than I can count) and x86s (same).

True to be told, the assembly language I started with /helped a LOT/ with be becoming an efficient developer; a developer who understand what 'code' is being generated when he writes an expression, a statement, a loop, and one who understands what the runtime implication are for most of the 'sugar coating' HLL gives.

However, starting (a bit) with the 68k, then even more so with the PowerPC, it became pretty much impossible to write /from scratch/ an assembly equivalent that was QUICKER than the compiler generated code. That was 20+ years ago. DRAM latency happened, pipelining happened and SIMD happened.

Today, hand writing assembly is pretty much stupid on modern CPUs. Given the register files, timings, shadow registers, bus latencies etc etc the compiler will ALWAYS be better because there is so much criteria to think about when generating code...

I'm not saying that having the knowledge is not useful; the best use of assembly is to write some code il HLL, one that is supposed to be super-mega-critical-quick, then disassemble it and see how it looks. More often then not, you can't make it better than it is in situ -- most of the time you will gain is to prepare your data better, align it better etc etc -- basically, 'hinting' the compiler to do a better job. You can do serious code butchery like that, without a hint of assembler [0].

But really, I haven't written any assembly for /performance reasons/ in 15 years, and that was Altivec on PowerPC.

For 8 bits, it's all smooth as butter, but the article also doesn't take into account the massive progress in compilers; I'm the author of SimAVR [1] and I've seen my load of generated code for that CPU, and the GCC toolchain is /very hard to beat/ by hand these days.

[0]: critical audio loop on one of my old PCI card driver, converting floatint, applying gain etc while using the register file to the max, and making most use of the pipelining of the G4 (at the time) https://gist.github.com/buserror/0a3a69cca927b8da6c9c7ee1605... -- note, the inner loop was generated by a script that was doing the cycle calculations (!)

[1]: https://github.com/buserror/simavr

Re: Assembly Language Programming: Still Relevant Today (2015)

#36
post #34
post #29

Earlier quoted context omitted.

It's more than just execution speed. It could something as simple making a function using assembly that swaps the virtual memory page table on a processor. That's not something you gonna find in a high level language.

Yup. The example I implemented in college is stack pointer manipulation to implement multithreading (think pthread). Some things can only be done in assembly.

Yea done that myself, save all the register states, save the stack pointer, and then set a new stack pointer and load all that tasks register states.

Although doing the same on windows is kinda annoying. Also the amount callee saved registers is kinda like what the heck. Here is some code I wrote doing that for window's ABI. https://pastebin.com/jnxeMRcV

Re: Assembly Language Programming: Still Relevant Today (2015)

#37
post #12

Earlier quoted context omitted.

When I was young I imagined that when I'll be an advanced programming/CS student I would learn advanced stuff like self-modifying code. Too bad real life is not that exciting...

If you’re interested, JIT compilers or malware reversing is a “real” place where this knowledge is useful.

Also don't forget binary translation which basically is assembly level JIT. Although static binary translation is a thing but has limitations.

Re: Assembly Language Programming: Still Relevant Today (2015)

#38
There is many kind of assembly language, some for the actual computer hardware, some for VMs, and some used as both.

I have used and sometimes still do use assembly language, including 6502 (specifically, NMOS 6502 without decimal arithmetic, including unofficial opcodes), and a little bit of x86 stuff (although the modern x86 is very messy, I think), but also Z-machine and Glulx. I have also used MIX and MMIX assembly (and may use MMIX more if I would actually make a computer with it). And then some other programs (such as ZZ Zero, which is similar to ZZT) has its own kind of assembly language.

One feature not mentioned is the relative numbered labels such as 1H and 2H available in MIXAL and MMIXAL; you can then use 2F to find the next 2H label forward, or 2B to find the next 2H label backward. My own assemblers for Glulx and ZZ Zero support the similar feature too.

Re: Assembly Language Programming: Still Relevant Today (2015)

#39

I'm rarely keen on posting negatives on articles that clearly took a lot of time to make, but I think this requires a bit of correction. I think this article is very, very simplistic. All of it relates to a 8 bits CPU that is 40+ years old. I switched to HLL as soon as I could get my hand on a compiler, namely, UCSD Pascal at the time! Then the Pascal, then to C and then myriads of other languages. I covered 6502, Z8…

In my experience, you can easily beat a compiler for size, which may or may not correspond to speed.

Re: Assembly Language Programming: Still Relevant Today (2015)

#40

Earlier quoted context omitted.

> I have read that self-modifying code on the x86 architecture is pretty dangerous at the assembly level. Why's that? I'm not aware of any issues specific to x86.

x86 is actually one of the easier platforms to have self-modifying code on, because you don’t have to flush the instruction cache.

Which is kind of interesting considering how removed the assembly which was written is from what is actually executed. You'd think x86 would be even less likely to notice you changed the plan out from underneath it.
Post reply on HN