Inverting a binary tree using x64 assembly
sanket.tech
sanket.tech
Also, we'd like to remind you that you can only re-apply after our cool-off period, which is 25 years.
This made me laugh because it must be a reference to this: https://news.ycombinator.com/item?id=33652023
> I contend that the AT&T syntax is harmful and bad, and should never be used, for any reason, under any circumstances, by anyone.
Which is a real thing. There are systems that would store, say, a 32-bit word as two 16-bit words that were big endian relative to each other, but little endian within the 16-bit word.
-123 -12 -1
Here are three numbers. If we read them from left to right, they all begin with 1, but we cant tell what the 1 means, in one case it means 100, in one it means 10 and in one it means 1. We need to parse the entire number to know what the first number means. If we parse from right to left, each number always means the same thing. the first is how many once, the second how many 10s and so on.
So it makes sense to store the smallest first. In a big endian architecture, if i want to read an 8, 16, 32 or 64 bit word each byte will always mean the same, if we pad out the type with zeros. So little endian is right, and Arabic numerals are wrong.
Dispatch (if only the topmost n-bits (or bytes) are needed to make a decision, that's all you need to read).
Them's fighting words.
:invert
cmp rax, 0
jnz swap:
ret
:swap
push rax
mov rax, [rax]
call invert
mov rbx,[rsp]
xchg rax, [rbx+8]
mov [rbx], rax
call invert
pop rax
ret
The last time I used an assembler was before x86-64 was invented, I am not even sure I ever used one in protected mode. But that seems a totally reasonable whiteboard interview question. Written in notepad, might not assemble. Might even be totally incorrect and I am posting it so that the internet generates the warning and error messages.EDIT: After reading the article now, that seems rather inefficient to me, to use local variables on the stack for everything. And why is the function returning a node if it is mutating the tree in place?
EDIT: As Wikipedia describes it as a stack-oriented language, because of my comment about putting everything onto the stack?
However, when writing in assembly one must pay attention that at least RBX, RBP and R12 through R15 must be preserved by any function (on Windows also RDI and RSI must be preserved).
So in your code you should not use RBX, but a volatile register, e.g. RDX or RCX. If you would insist on using RBX, it would have to be saved and restored.
mov cr0, ...Why are variables on the stack more efficient than other memory accesses?
Only if you're calling external code that assumes that. The power of Asm largely comes from not needing to follow arbitrary conventions in your own code. The boundaries where you interface to external code are the only constraints.
I'm with GP, it's fun seeing how solutions differ between languages as a way to peek into other language communities I don't spend as much time in.
Since too many people were memorizing inversion, I switched to asking how to evert a binary tree. This leads naturally into a discussion of the 1:1 relationship between complex numbers and cohomology sets, I figure if somebody can get that right, they can be a junior programmer on the team.
Definitely not what you want to submit to someone testing your programming skills.
You typically only use Morris traversal on exceptionally large trees, and by large I mean when working with data that lives on a disk. It's definitely the exception, not the norm.
Read more here https://stackoverflow.com/questions/17981447/microarchitectu...
> [...] these zeroing instructions extremely efficient, with a throughput of four zeroing instructons per clock cycle.
Also, the xor instruction takes up the smallest amount of .text space (right?).
https://randomascii.wordpress.com/2012/12/29/the-surprising-... is almost ten years old and thus likely dated, but still may be educational.
I liked http://rayseyfarth.com/asm/ as an intro to both. I'd already had a class on computer architecture that did assembly before that, though.
Once you get going with that, you can download and read the Intel or AMD programmers manuals. Of course, this assumes x86_64.
sed -i 's/r//g' invert_tree.asm > invet_tee.asmSimilarly, the reality is that as a professional programmer you spend no time doing work like leetcode.
Instead, you spend a lot of time understanding and slightly modifying (fixing or enhancing/extending) code.
With the rise of language model code completion systems (e.g., Microsoft Copilot) even more time will be spent inspecting and understanding code to find problems.
With these facts in mind, I have been building a new form of leetcode:
Most puzzles are interesting algorithms that you will learn useful techniques from, so it's never a waste of time to think about them. And even though the bugs are all quite trivial, I can see it's very challenging for many people.
It's about half-way ready to launch, needing 30 more puzzles. I am working my way through Knuth's The Art of Programming Volume 4B and today I'll see if Algorithm X (Dancing Links Exact Cover Backtracking) can be made to fit for Bugs 38 and 39 (or whether it's too complicated).