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…
Echo – Assembly program that prints the first positional argument to stdout
21–30 of 50 posts
Re: Echo – Assembly program that prints the first positional argument to stdout
#22Earlier 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…
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
#23Doesn't echo print all the arguments?
Re: Echo – Assembly program that prints the first positional argument to stdout
#24I'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.
Re: Echo – Assembly program that prints the first positional argument to stdout
#25The 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…
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
#26The 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?
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
#27Besides, 32 bit (⊙_⊙')
Re: Echo – Assembly program that prints the first positional argument to stdout
#28Serious question: why is this on HN front page? Am I missing something?
Re: Echo – Assembly program that prints the first positional argument to stdout
#29It 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 (⊙_⊙')
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
#30Earlier 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.