Live data from Hacker News

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

github.com

11–20 of 50 posts

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

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

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

#12

echo returns nonzero if it cannot write ... touch foo.txt; chmod 400 foo.txt; echo ouch > foo.txt; echo $? .., but it appears this asm returns zero always.

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.

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

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

And for syscalls that also have a meaningful return value, the ABI requires that valid errno values fall in the range -1 to -4095, to disambiguate them from any possible return value. Those values can't conflict with valid userspace pointers (since they'd point into kernel space), and syscalls must not allow them to conflict with valid numeric return values.

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

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

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

#15
post #5

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

I think this is a pretty interesting piece of code. Something trivial via non-trivial (something that we don't do everyday) set of calls.

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

#16

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.engr.illinois.edu/ece390/archive/spr2002/boo...

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

#17
post #10
post #5

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

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 wouldn't put it on the front page of HN yet.

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

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

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.

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

#19

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're still the x86-64 implementation for things like strlen on a bunch of platforms. They're not always the fastest, but have code size advantages.

http://agner.org/optimize/optimizing_assembly.pdf

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

#20
post #19

Earlier quoted context omitted.

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're still the x86-64 implementation for things like strlen on a bunch of platforms. They're not always the fastest, but have code size advantages. http://agner.org/optimize/optimizing_assembly.pdf

That's a really interesting document - I am going to spend some time studying it, thanks!

Good point about the code size. I imagine there are likely to be cases where that would let some algorithm run faster overall because it fits in the instruction cache, even if the string operation considered on its own is slower.

Post reply on HN