Echo – Assembly program that prints the first positional argument to stdout
github.com
github.com
Even for echo, this one is extremely minimalist: first argument only, and a maximum of 255 characters.
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.
But you can also pass arguments to execve(2) which are not null-terminated.
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.
Not 100% free, I think I need to finish my compiler training and forth bootstraping before I can claim that.
I can't really suggest others to follow the lisp, ml, prolog road though, so I'll just state what I wrote above.
[1] SICP especially, with its gradual pedagogy. From substitution, to environment, to register machines. You can see the relationships up close.
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.
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.)
[1]: https://github.com/kelseyhightower/echo/blob/53d84ea4e79db3d...
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...
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.
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.
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...
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.
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.
touch foo.txt; chmod 400 foo.txt; echo ouch > foo.txt; echo $?
.., but it appears this asm returns zero always.
Eg. -EINVAL.
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?)