> I will be using x64 assembly with the AT&T syntax as it is objectively superior than the Intel syntax. 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.
Inverting a binary tree using x64 assembly
11–20 of 73 posts
Re: Inverting a binary tree using x64 assembly
#12You can use the 32bit xor to reset the register. Also TEST REG,REG might be better for checking if it’s zero.
Re: Inverting a binary tree using x64 assembly
#13x32 converter for GNU/Linux: sed -i 's/r//g' invert_tree.asm > invet_tee.asm
It's not that easy, you also have to account for the pointer sizes (4 bytes instead of 8). Also alignment requirements are different on x32.
Re: Inverting a binary tree using x64 assembly
#14I love seeing people solve leetcode challenges in asm, are there any more blogposts like this?
The hard bit of solving them is usually the algorithm though - when you know that you can code it in anything.
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.
Re: Inverting a binary tree using x64 assembly
#15 :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?
Re: Inverting a binary tree using x64 assembly
#16You can use the 32bit xor to reset the register. Also TEST REG,REG might be better for checking if it’s zero.
Is 32 bit cheaper? Still 1 cycle.
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?).
Re: Inverting a binary tree using x64 assembly
#17Similarly, 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).
Re: Inverting a binary tree using x64 assembly
#18Re: Inverting a binary tree using x64 assembly
#19Re: Inverting a binary tree using x64 assembly
#20Assume RAX points to the root node and nodes just contain two child pointers and everything is aligned and whatever. :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 w…