Live data from Hacker News

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

github.com

21–30 of 50 posts

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

#21

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...

The latter. The various REPxx prefixes cause a string instruction like SCASB to be repeated until some condition is satisfied. These date back all the way to the 8086/8088. They were the fastest way to do string operations on those early CPUs, but I don't think this the case on modern CPUs. https://www.google.com/search?q=intel+rep+prefix http://wiki.osdev.org/X86-64_Instruction_Encoding#REPNE.2FRE... https://courses…

I also remember them being called "software machine gun" by Duntemann in his assembly book. Using REP STOSW could save you 5 instructions.

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

#22
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…

> 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.

But you can also pass arguments to execve(2) which are not null-terminated.

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

#24
post #6

I'm curious as to the why. Kubernetes doesn't keep Kelsey busy enough? :-)

I guess it's because the echo command included in the shell was slow.

The cost of fork+exec of a separate binary will make even the most efficient possible external echo slower than the shell builtin, I suspect. (This is why echo is a builtin in the first place, though there's no requirement for it to be so.)

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

#25

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...

The latter. The various REPxx prefixes cause a string instruction like SCASB to be repeated until some condition is satisfied. These date back all the way to the 8086/8088. They were the fastest way to do string operations on those early CPUs, but I don't think this the case on modern CPUs. https://www.google.com/search?q=intel+rep+prefix http://wiki.osdev.org/X86-64_Instruction_Encoding#REPNE.2FRE... https://courses…

> They were the fastest way to do string operations on those early CPUs, but I don't think this the case on modern CPUs.

Modern CPUs still have a fast path for string operations, and I recall hearing that they even had some improvements not too long ago (in Sandy Bridge or other recent arch).

CPUs may be smart enough to detect a memcpy done with a loop, but REPxx is the preferred way - even on modern CPUs.

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

#26
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?

I'm guessing that the author wants to be sure not to get in trouble with their legal department.

My contract has a similar clause (all copyright assigned to employer) but it's void because my local (non-US) legislation overrides it. Not that I want to go head to head with our legal dept to test whether it holds.

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

#27
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 (⊙_⊙')

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

#28
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.

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

#29

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 (⊙_⊙')

Yes, this is my first assembly program. I had to look up every instruction and it took me hours to understand even the basics, but it was worth it. I have a much better understanding of x86 assembly and plan to write larger programs to continue learning in 2017.

I went with 32 bit because all the examples were 64 bit so I forced to learn the nasm and ld flags to get my program to compile, link, and run. I also learned a lot about the different registers available to 32 and 64 bit programs.

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

#30
post #18
post #17

Earlier quoted context omitted.

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…

Maybe the 255-char limit is a feature? If this "fast echo" is meant to be used in a script that writes entries to a log where you wouldn't want long text anyway, or something like that... So having a known upper bound for the output size can be useful.

[deleted]
Post reply on HN