Live data from Hacker News

Inverting a binary tree using x64 assembly

sanket.tech

11–20 of 73 posts

Re: Inverting a binary tree using x64 assembly

#11
post #10

> 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.

AT&T really is annoying but it feels like the big vs little endian debate. Fairly easy to convert between the two as well.

Re: Inverting a binary tree using x64 assembly

#13
post #5

x32 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.

It's not only that either. OP used rdi because of calling convention, in x32 you would need to arguments on the stack instead and reference them +ebp instead of using registers if you want non-asm code to call your function.

Re: Inverting a binary tree using x64 assembly

#14

I 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 not well versed in assembly, so learning assembly first would be the hard part!

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
Assume 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 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

#16
post #12
post #3

You 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.

I was thinking of this from the perspective of CPU pipeline pressure, but in reality it seems prosessors are indeed smart enough to avoid burdoning the ALUs execution with these kinds of special cases.

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

#17
Being able to read and understand x86-64 assembly (or PTX/SASS for an Nvidia GPU) is much more important than being able to write it. In practice, even when you're writing assembly, you're looking at reference assembly generated by a compiler from C code you wrote.

Similarly, 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:

https://BUGFIX-66.com

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

#18
This is pretty damn cool, but unfortunately you failed the interview as you accepted the challenge to use x86 assembler, but solved the problem using a different programming language from the one we asked you to use. We'll keep your resume on file, and if there are any openings in the future we encourage you to apply for those.

Re: Inverting a binary tree using x64 assembly

#20
post #15

Assume 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…

Did you write Forth at all?
Post reply on HN