Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Quick question, I was skimming the code and came across this:

  xor eax, eax
  mov ax, 1
Was there a reason you didn't use the shorter

  xor eax, eax
  inc eax

?


I experimented with just mov eax, 1 and found it be slower - though I didn't try inc eax.

Using 16-bit registers is tricky because they leave the upper bits unchanged. Its easy to trigger a partial register hazard inadvertently.

p.s. the comments were nicely lined up in my editor, don't know what happened once it got to github.


> p.s. the comments were nicely lined up in my editor, don't know what happened once it got to github.

probably spaces-vs-tabs. GitHub has a different number of spaces-per-a-tab than your text editor. If one configures one's editor to expand tabs to spaces this won't happen.


> If one configures one's editor to expand tabs to spaces this won't happen.

…or you could use tabs for indentation, spaces for alignment, as they are intended to be used ;)


> …or you could use tabs for indentation, spaces for alignment, as they are intended to be used ;)

Yes. But, that is easier to stuff up. Getting rid of tabs completely, it disappoints some people who want to change the indentation amount to fit their personal preferences, but it reduces the likelihood of messes being made.


> it disappoints some people who want to change the indentation amount to fit their personal preferences

That's one issue I have with spaces, along with the fact that they are more annoying to delete. And I've seen people indent by 4n+1 spaces by accident at times (usually when there are no surrounding lines to provide context), so I wouldn't be so sure about it being the end-all, be-all for fixing indentation mistakes.


> along with the fact that they are more annoying to delete.

FWIW my editor (vscode) seems to be deleting 4 tabs at a time on when my caret is at the start of a code line.

(That said what I usually do is shift up or down to select some text on all lines I intend to move and then use tab or shift-tab to fix indetation.)


> deleting 4 tabs at a time

I assume you meant 4 spaces.


That is correct!


Or use tabs for indentation, spaces to separate words, and don't align.


That’s fine as well :) I guess you can call it a degenerate case of my more general guidelines. (FWIW, I don’t align usually; I just run a formatter over my code and make sure the indentation is right.)


Tabs are intended to be used for alignment. "Tab" is a clipping of "tabulator", as in "thing that tabulates" or "thing for tabulating", where "tabulate" means "to arrange in a tabular manner, to format as a table". But rather than risk etymological fallacy, I'll explain the history of the tab key to illuminate that it was certainly intended for alignment.

The tab key on typewriters caused the carriage to advance to the next tab stop. The tab stops were physical barriers that would stop the carriage from moving at a specific position. In many cases they were adjustable, but the specifics of how varied from model to model. Usually it would be pins, clips, sliders, vel sim. attached to a bar near the carriage. The tab key would raise the carriage and fling it across until it hit a tab stop and... stopped.

This mechanism can't have been invented terribly long after typewriters started to be sold commercially in 1868, because by 1900 Underwood was already advertising tabulating as "part of the machine, not an attachment"[1]. The Underwood No. 5 was immensely popular and became the template for what we think of today as a typewriter; earlier models were subject to much variation, and in many cases the typist couldn't even see what she was typing, because the typeheads would strike from behind the paper! Of course, nobody had to copy every feature of the Underwood machine, so the tabulator must've been somewhat popular. I imagine it was rather handy in businesses or any institution that kept typed records.

It's true that many cheaper machines only had support for a single tab stop, and that it was oftentimes used for paragraph indentation, but this is a limitation of low-cost machines rather than an intention, I think, and even a single tab stop can be used for tabulation—so long as you only need to make tables of two columns ;)

The tab's entry into computing is somewhat accidental, due to the usage of teletypewriters as terminals in the early years, and thus both the adoption of teleprinter codes such as ASCII for information encoding and the adaptation of the keyboards of such terminals for later HIDs. When the CRT terminal emulator ("glass TTY") was introduced, its tab stop functionality would often emulate that of a typewriter—in many cases even going as far as being adjustable (see e.g. [2]).

Despite the accident, there were some benefits to keeping tabs around, especially in the early days. Kernighan & Plaugher's «Software Tools in Pascal» presents them in §2.1 where it introduces a filter called "entab":

… you could convert your files to a standard representation, one that has no wasteful imbedded blank strings. As an added payoff, your files are smaller and they all look alike; that makes it easier to write programs that talk and work together.Another use for `entab` is to prepare output to be sent to a terminal. You might have a program that expects to drive a smart printer. You would like to speed up printing by tabbing whenever possible. Rather than rewrite a working program, you are better off with a separate program to filter the output before it is typed. Thus `entab`.

And although it's common these days for software to give tabs a simplistic treatment by always expanding them to a constant number of blanks, Kernighan & Plaugher's "detab" tool in §1.5 demonstrates that this is not how tabs are "supposed" to work: when you have a fixed-width typeface, are to be expanded to a variable number of blanks, depending on the distance between the current cursor column and the next tab stop. If tab stops are set to 8, then it's roughly equivalent to 8 blanks in the first column, but 7 blanks in the second column, 6 in the third, and so on, because the tab key isn't "supposed" to mean "k blanks", but rather "skip to column k". And that certainly sounds like alignment to me! (For the curious, I've translated K&P's `detab` tool to Lua (and made it a bit more flexible); you can find it at [3]).

Unfortunately, the utility of tabs for alignment is countered by their lack of portability. Since every terminal (and later every text editor/word processor) had its own notion of tab stops, and since they were often configurable (and therefore many users would change them to suit their own tastes), tabs couldn't really be relied upon for conveying pretty files. And as technology has advanced, their utility as an optimization or convenience has diminished significantly. So the only utility they're left with is as a configurable signal of indentation. For programmers especially, this is no small utility, perhaps, but it wasn't how they were intended to be used ;)

——

[1]: https://www.antiquetypewriters.com/typewriter/underwood-1-ty...

[2]: http://man.openbsd.org/POSIX-2013/tabs

[3]: http://ix.io/1Cnh.lua (whereto I hereby disclaim any warranty, and likewise disclaim all copyright—insofar as I believe it my right to do so, I make it communis juris, in solemn hope that if it be any use whatsoever that it be a benefit to all; and I am not proud, for yet this is but a humble offering, not likely to be of much use to any; but considering my own shortcomings, not least among them is a lack of foresight, so be it far from me to prophesy what benefit may come of the thoughts or actions of others who have been free to think and to act; and considering also that history has shown what great achievements whereof mankind is capable when each one is allowed to nourish and complement each other: I say let none languish in bondage, but grow to magnificence in freedom, for the good of each other and of our whole humanity; wherefore I would encourage others do likewise, even with so small a thing as this, however I do not demand it)


I wouldn't use tabs to align your comments; I use them exclusively for indentation and rely on spaces for alignment. This makes them work both with editors that do simplistic "replace tabs with spaces" and with editors that have tab stops.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: