Question about assembler commands in games on the Unity engine
Question about assembler commands in games on the Unity engine
(Google translation)
Question for knowledgeable users.
In games using the Unity engine, I often come across sections of code that look incomprehensible to me, even meaningless.
For example, here is the code from the game RimWorld:
Please explain the meaning of the instructions surrounded by a colored frame. There are more than a thousand such code sections in the game. Why is this double round-trip conversion necessary?
Or here are other examples on the same piece of code:
6 sequential instructions are highlighted in green, although in this case only two instructions are sufficient. Why is such a long method used?
Above the first colored frame are two instructions: the first writes the value of a register into memory (movss [rbp-10],xmm0), and the second writes back from the same memory to the same register. Why is the second instruction needed?
Under the first colored frame is the instruction lea rbp,[rbp+00], which does not change anything. Why are these instructions here?
It is also very common to encounter comparison instructions where the flags set after the comparison are not used in any way. There are hundreds of such instructions in the code. What is the point of these “empty” comparisons?
Question for knowledgeable users.
In games using the Unity engine, I often come across sections of code that look incomprehensible to me, even meaningless.
For example, here is the code from the game RimWorld:
Please explain the meaning of the instructions surrounded by a colored frame. There are more than a thousand such code sections in the game. Why is this double round-trip conversion necessary?
Or here are other examples on the same piece of code:
6 sequential instructions are highlighted in green, although in this case only two instructions are sufficient. Why is such a long method used?
Above the first colored frame are two instructions: the first writes the value of a register into memory (movss [rbp-10],xmm0), and the second writes back from the same memory to the same register. Why is the second instruction needed?
Under the first colored frame is the instruction lea rbp,[rbp+00], which does not change anything. Why are these instructions here?
It is also very common to encounter comparison instructions where the flags set after the comparison are not used in any way. There are hundreds of such instructions in the code. What is the point of these “empty” comparisons?
Re: Question about assembler commands in games on the Unity engine
That’s a standard method/function found in all Unity games, not just a RimWorld thing. It calculates the absolute value of a given value, as the name suggests. Both pictures depict the exact same method, so I’m not sure what you mean by other examples. Some of those are just redundancies; they do absolutely nothing. The lea rbp,[rbp+00] is just Cheat Engine showing it wrong there should not be anything there.
Re: Question about assembler commands in games on the Unity engine
CE isn't showing anything wrong. That's the mnemonic representation of the ASM bytes displayed on that line. Look for the same bytes in this snippet: [Link].
Code: Select all
00007FF6DF084D1E | 48 8D 65 00 | lea rsp,qword ptr ss:[rbp] | ;test1.asm:82
Re: Question about assembler commands in games on the Unity engine
Ahh my bad I had the idea it was the same thing as 00 00 coming out asSunBeam wrote: ↑Sat May 11, 2024 10:46 pmCE isn't showing anything wrong. That's the mnemonic representation of the ASM bytes displayed on that line. Look for the same bytes in this snippet: [Link].
Code: Select all
00007FF6DF084D1E | 48 8D 65 00 | lea rsp,qword ptr ss:[rbp] | ;test1.asm:82
add [rax],al
Thank you for correcting me.
Re: Question about assembler commands in games on the Unity engine
(Google translation)Metanoia wrote: ↑Fri May 10, 2024 6:03 amThat’s a standard method/function found in all Unity games, not just a RimWorld thing. It calculates the absolute value of a given value, as the name suggests. Both pictures depict the exact same method, so I’m not sure what you mean by other examples. Some of those are just redundancies; they do absolutely nothing. The lea rbp,[rbp+00] is just Cheat Engine showing it wrong there should not be anything there.
I was not talking about the entire method, but about the sections of code I highlighted in the screenshot, the meaning of which is not clear to me. Probably because of my bad English they didn't understand me...
These instructions perform a double round-trip conversion, the contents of the xmm0 register are not changed (the tail in the high part of the register remains, but it is not used):
Code: Select all
cvtss2sd xmm0,xmm0
cvtsd2ss xmm0,xmm0
The second instruction loads the value from memory back into the register it came from:
Code: Select all
movss [rbp-10],xmm0
movss xmm0,[rbp-10]
After this instruction, the contents of the rbp register do not change:
Code: Select all
lea rbp,[rbp+00]
There are 6 instructions circled in green in the screenshot:
Code: Select all
cvtss2sd xmm0,xmm0
cvtsd2ss xmm0,xmm0
cvtss2sd xmm0,xmm0
cvtsd2ss xmm5,xmm0
movss [rbp-04],xmm5
movss xmm0,[rbp-04]
Code: Select all
movss [rbp-04],xmm0
movss xmm5,xmm0
Re: Question about assembler commands in games on the Unity engine
After some research here. SunBeam can correct me if I got anything wrong.AlexS wrote: ↑Sun May 12, 2024 10:28 am(Google translation)Metanoia wrote: ↑Fri May 10, 2024 6:03 amThat’s a standard method/function found in all Unity games, not just a RimWorld thing. It calculates the absolute value of a given value, as the name suggests. Both pictures depict the exact same method, so I’m not sure what you mean by other examples. Some of those are just redundancies; they do absolutely nothing. The lea rbp,[rbp+00] is just Cheat Engine showing it wrong there should not be anything there.
I was not talking about the entire method, but about the sections of code I highlighted in the screenshot, the meaning of which is not clear to me. Probably because of my bad English they didn't understand me...
These instructions perform a double round-trip conversion, the contents of the xmm0 register are not changed (the tail in the high part of the register remains, but it is not used):Why is there a round-trip conversion needed here?Code: Select all
cvtss2sd xmm0,xmm0 cvtsd2ss xmm0,xmm0
The second instruction loads the value from memory back into the register it came from:Why is the second instruction needed here?Code: Select all
movss [rbp-10],xmm0 movss xmm0,[rbp-10]
After this instruction, the contents of the rbp register do not change:Why is this instruction needed here?Code: Select all
lea rbp,[rbp+00]
There are 6 instructions circled in green in the screenshot:Instead, it is enough to use only 2 instructions, for example like this:Code: Select all
cvtss2sd xmm0,xmm0 cvtsd2ss xmm0,xmm0 cvtss2sd xmm0,xmm0 cvtsd2ss xmm5,xmm0 movss [rbp-04],xmm5 movss xmm0,[rbp-04]
Why was it necessary to use 6(!) instructions?Code: Select all
movss [rbp-04],xmm0 movss xmm5,xmm0
Code: Select all
cvtss2sd xmm0,xmm0
cvtsd2ss xmm0,xmm0
Code: Select all
movss [rbp-10],xmm0
movss xmm0,[rbp-10]
Code: Select all
lea rbp,[rbp+00]
"Why was it necessary to use 6(!) instructions?" No one can tell you why a compiler does its black magic it just does.
Code: Select all
cvtss2sd xmm0,xmm0
cvtsd2ss xmm0,xmm0
cvtss2sd xmm0,xmm0
cvtsd2ss xmm5,xmm0[code]
Just rounding. I think...
Re: Question about assembler commands in games on the Unity engine
This is wrong. In this case, no rounding occurs because the lower precision is converted to the higher precision first. The value in the lower part of the register does not change after these two conversions (you can check).
For the experiment, I tried to remove all sections of the code:
Code: Select all
cvtss2sd xmm0,xmm0
cvtsd2ss xmm0,xmm0
Did I understand correctly that all these sections of code are a consequence of the “strange” work of the compiler, and not the programmer’s intention?
Re: Question about assembler commands in games on the Unity engine
This is for code alignment, as it is JIT compiled, and the same will be true for games made by Flash
Who is online
Users browsing this forum: No registered users