Live data from Hacker News

Echo – Assembly program that prints the first positional argument to stdout

github.com

31–40 of 50 posts

Re: Echo – Assembly program that prints the first positional argument to stdout

#31
post #17
post #10

Earlier quoted context omitted.

I feel it's kind of interesting as a minimal Unix program that does something useful without linking to the C library, just with syscalls. Even for echo, this one is extremely minimalist: first argument only, and a maximum of 255 characters.

I don't understand why it is limited to 255 chars. The kernel copies the string(s) into the programs memory so it would be a kernel bug if the program got a non null-terminated or too long string. More importantly this program has a bug in that it doesn't check if there is an argument passed to it at all. Good effort but can improve a lot. I would praise the documentation but it is rather imprecise. All in all i woul…

This is great feedback, which I plan to use to improve the echo program. I'm just learning (on my own), and I figured I would just post my progress and I would get some feedback; it worked!

echo is far from finished, and it's safe to say "I don't know what the hell I'm doing", but hey, I gotta start somewhere.

Re: Echo – Assembly program that prints the first positional argument to stdout

#33

It looks like Kelsey didn't write a lot of assembly before. There are quite a few things you either wouldn't do -- like `cld` for no reason -- or most people (and compilers) would do otherwise -- e.g., `xor ebx, ebx` instead of `mov ebx, 0`. Besides, 32 bit (⊙_⊙')

What caught my attention was the segment register use --- besides the fact that Linux runs processes in flat mode, the more common way to es = ds is push ds; pop es.

That said, it does look better than compiler output and distinctly has the style of hand-written Asm; the 3 pops at the beginning, for example, would be something no compiler I've seen can do. (Minor "optimisation" --- rethinking your register use can eliminate some superfluous moves.)

Re: Echo – Assembly program that prints the first positional argument to stdout

#34
post #12

Earlier quoted context omitted.

How do "raw" system calls pass back error information on Linux? errno is strictly a C/POSIX abstraction, right?

They just return the negative error number. Eg. -EINVAL.

...which I think is a far superior system to errno. I commented on this before somewhat recently: https://news.ycombinator.com/item?id=13062421

In fact, it's puzzling to me why the errno mechanism was even conceived, as it seems to offer no advantages over returning errors directly (does anyone happen to know?)

Re: Echo – Assembly program that prints the first positional argument to stdout

#35
post #5

Serious question: why is this on HN front page? Am I missing something?

Serious answer: Because many developers regard assembly as some sort deep magic only understood by elder gods. This, of course, comes from some vague (and not entirely correct) understanding of "assembly" running beneath everything else, and thus being fundamental, yet not immediately useful to a large category of developers today. Hence it seems important but archaic. Archaic + difficult = elder knowledge.

I've actually had a few coworkers think I'm some sort of elder god when I find the root cause of subtle bugs that would've either required deep knowledge of the C++ standard, or not-as-deep knowledge of Asm. These are bugs that others have spent many hours staring at the source and stepping through in a debugger without any better idea of why they occur, but are solved in minutes by a glance at the Asm. IMHO if you are working with native code at all, it's a very useful skill to have.

Re: Echo – Assembly program that prints the first positional argument to stdout

#36

The instruction `repne scasb`[1] stood out. `repne X` means "while (not equal) { X; }". How is `repne` implemented? Is `repne scasb` assembly shorthand for a `scasb` then a `jne`? Or is `repne` some fancy higher-order instruction which takes another instruction as its argument? [1]: https://github.com/kelseyhightower/echo/blob/53d84ea4e79db3d...

How is `repne` implemented?

It's a microcode loop. Here is the appropriate pseudocode (this is extracted from the Intel manuals):

http://qcd.phys.cmu.edu/QCDcluster/intel/vtune/reference/vc2...

Re: Echo – Assembly program that prints the first positional argument to stdout

#37
post #11

The source code includes this notice: "Copyright 2017 Google Inc. All Rights Reserved." I wonder if the couple of dozens of lines of assembly code could be trivial enough to be public domain. Assuming a straightforward implementation, surely there is far less freedom in expressing the simplest version of the echo program in ASM compared to, say, C?

surely there is far less freedom in expressing the simplest version of the echo program in ASM compared to, say, C?

I'd say it's the opposite, since it is often the case that more instructions (and thus ways to select and arrange them) are required to express an operation in Asm compared to an HLL like C. This implies that there is room for more creativity when e.g. writing a "Hello world" in Asm vs. C.

Re: Echo – Assembly program that prints the first positional argument to stdout

#38

Earlier quoted context omitted.

Serious answer: Because many developers regard assembly as some sort deep magic only understood by elder gods. This, of course, comes from some vague (and not entirely correct) understanding of "assembly" running beneath everything else, and thus being fundamental, yet not immediately useful to a large category of developers today. Hence it seems important but archaic. Archaic + difficult = elder knowledge.

I've actually had a few coworkers think I'm some sort of elder god when I find the root cause of subtle bugs that would've either required deep knowledge of the C++ standard, or not-as-deep knowledge of Asm. These are bugs that others have spent many hours staring at the source and stepping through in a debugger without any better idea of why they occur, but are solved in minutes by a glance at the Asm. IMHO if you a…

Everyone who works with native code, not just C or C++, should at least understand how linkers and loaders work.

Re: Echo – Assembly program that prints the first positional argument to stdout

#39
post #5

Serious question: why is this on HN front page? Am I missing something?

Yes. From experience, many developers, namely newly-graduated college students from not-so-rigorous programs, have little idea of Assembly. The same applies with theoretical computer science (Turing Machines, FSMs, PDAs etc.), algorithmic analysis and fundamentals of computing hardware (flip-flops, half/full adders, basic CPU design).

Re: Echo – Assembly program that prints the first positional argument to stdout

#40

Earlier quoted context omitted.

Serious answer: Because many developers regard assembly as some sort deep magic only understood by elder gods. This, of course, comes from some vague (and not entirely correct) understanding of "assembly" running beneath everything else, and thus being fundamental, yet not immediately useful to a large category of developers today. Hence it seems important but archaic. Archaic + difficult = elder knowledge.

I've actually had a few coworkers think I'm some sort of elder god when I find the root cause of subtle bugs that would've either required deep knowledge of the C++ standard, or not-as-deep knowledge of Asm. These are bugs that others have spent many hours staring at the source and stepping through in a debugger without any better idea of why they occur, but are solved in minutes by a glance at the Asm. IMHO if you a…

Once you are tired being praised, teach them some valgrind. It will solve most of their problems.
Post reply on HN