News for hackers. Stuff that matters.
Re:Inverting a binary tree using x64 assembly by danbruc on Thursday November 24, 2022 @01:16PM (#33734518)
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.
Re:Inverting a binary tree using x64 assembly by pyinstallwoes on Thursday November 24, 2022 @02:02PM (#33735050)
Did you write Forth at all?
Re:Inverting a binary tree using x64 assembly by danbruc on Thursday November 24, 2022 @02:03PM (#33735059)
No. Why do you ask?

EDIT: As Wikipedia describes it as a stack-oriented language, because of my comment about putting everything onto the stack?

Re:Inverting a binary tree using x64 assembly by adrian_b on Thursday November 24, 2022 @03:28PM (#33735862)
The variables on the stack are the most efficient after registers, so you are right that a local variable should be kept into a register if possible, otherwise in the stack, and only then in other places (e.g. if it is too large for 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.

Re:Inverting a binary tree using x64 assembly by danbruc on Thursday November 24, 2022 @04:00PM (#33736177)
I know none of the calling conventions in any detail anymore and just used the registers in alphabetical order. Totally expected that this would violate something.
Re:Inverting a binary tree using x64 assembly by PixelOfDeath on Thursday November 24, 2022 @04:17PM (#33736331)

  mov cr0, ...
Re:Inverting a binary tree using x64 assembly by roperj on Thursday November 24, 2022 @08:21PM (#33738071)
> The variables on the stack are the most efficient after registers

Why are variables on the stack more efficient than other memory accesses?

Re:Inverting a binary tree using x64 assembly by MobiusHorizons on Friday November 25, 2022 @02:12AM (#33740019)
The top of the stack should generally be already present in the cache, so stack memory would be faster than heap memory where objects aren’t necessary as close together or accessed as frequently.
Re:Inverting a binary tree using x64 assembly by xgkickt on Friday November 25, 2022 @05:39AM (#33741088)
Register reordering. Stack references are more likely to be in the register file.
Re:Inverting a binary tree using x64 assembly by menaerus on Friday November 25, 2022 @05:48AM (#33741152)
I am not familiar with any other reason besides the fact that stack-pointer arithmetics are done through a dedicated HW, called stack-engine, which sits in the CPU frontend. This effectively means that stack-pointer uOps are going to be get executed quicker because they do not have to go all the way through the CPU backend processing. This also saves some of the bandwidth on the CPU backend side allowing other uOps to be processed sooner.
Re:Inverting a binary tree using x64 assembly by userbinator on Thursday November 24, 2022 @08:22PM (#33738073)
However, when writing in assembly one must pay attention that at least RBX, RBP and R12 through R15 must be preserved by any function

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.