Page 1 of 1

ASM Output Question regarding push/pop

Posted: Tue Apr 10, 2018 11:11 am
by Crusiatus Black
Hi All,

I'm working on a simple tool for my own optimization purposes and while I was working on this tool, I noticed something I don't understand. Now, I think this might be some alignment or optimization method I don't know, however I figured I'd ask the question here;

I have a structure named derp within a procedure, and I'm accessing index 100 like so:

Code: Select all

single = derp\chars[100]
The produced assembly code on x64 Linux is:

Code: Select all

; single = derp\chars[100]
  MOV    dword [CLN],60 ; pbcompiler -l option
  LEA    rbp,[rsp+48]
  PUSH   rbp
  POP    rbp
  MOVSX  rax,byte [rbp+100]
  PUSH   rax
  POP    rax
  MOV    byte [v_single],al
However something I don't understand are the push rbp, pop rbp and push rax, pop rax instructions. Can anyone explain to me why those are pushed and popped immediately?

Re: ASM Output Question regarding push/pop

Posted: Tue Apr 10, 2018 4:53 pm
by Fred
Is this code in debug mode ? The peephole optimiser should catch these cases and remove them in release. It's useless code, only here because the code generator works step by step and output the asm immediately.

Re: ASM Output Question regarding push/pop

Posted: Tue Apr 10, 2018 5:08 pm
by Crusiatus Black
Hi Fred! This was compiled with 'pbcompiler -c -l -e outname'. So I'm assuming the debugger is not enabled then, right? I simply enabled the OnError lines support

Re: ASM Output Question regarding push/pop

Posted: Tue Apr 10, 2018 6:09 pm
by CELTIC88
because "pb" translates your code word by word to asm and is very difficult to generate it without junk code.

if you want optimize your code , learn asm!

Re: ASM Output Question regarding push/pop

Posted: Tue Apr 10, 2018 6:29 pm
by Crusiatus Black
CELTIC88 wrote:because "pb" translates your code word by word to asm and is very difficult to generate it without junk code.

if you want optimize your code , learn asm!
That's a weird response, I know ASM - I merely was asking the reasoning behind this code generation and as you can see from Fred's response, there is a reasoning behind it.

Re: ASM Output Question regarding push/pop

Posted: Wed Apr 11, 2018 11:11 am
by Fred
I will take a closer look to see why it's not removed in release mode.

Re: ASM Output Question regarding push/pop

Posted: Thu Apr 12, 2018 10:10 am
by Crusiatus Black
Fred wrote:I will take a closer look to see why it's not removed in release mode.
Thank you, however is this not because I used the commented asm option? Even though I don't consider this to be a debug mode, it still is an intermediate step isn't it?

Re: ASM Output Question regarding push/pop

Posted: Thu Apr 12, 2018 10:28 am
by Fred
It should be the final file which is sent to the assembler, so no further pass are done on it (it's done like this so you can tweak the output file without wondering if it will be parsed again by the compiler).

Re: ASM Output Question regarding push/pop

Posted: Thu Apr 12, 2018 1:47 pm
by Crusiatus Black
Yes that is what I always used it for, good to know. Would you like me to test multiple PureBasic versions with this same piece of code? This was produced using the latest PB on Linux x64

Re: ASM Output Question regarding push/pop

Posted: Thu Apr 12, 2018 1:51 pm
by Fred
I don't bother about it, I will take a look.

Re: ASM Output Question regarding push/pop

Posted: Fri Mar 05, 2021 11:39 am
by Rinzwind
Any update?

Re: ASM Output Question regarding push/pop

Posted: Fri Mar 05, 2021 3:54 pm
by chi
Rinzwind wrote:Any update?
Would be interested in that as well

Re: ASM Output Question regarding push/pop

Posted: Sat Mar 06, 2021 6:57 pm
by mk-soft
In ASM, when accessing an element constantly, using push/pop with rbp and rax makes no sense.
However, as basic code when implementing access to an element in ASM, it makes sense because you can simply add code after push rbp and before pop rbp. This way the register rbp can be used again. This can be repeated at will until the stack is full.

Nearly optimal
; single = derp\chars[100]
LEA rbp,[v_derp]
PUSH rbp
POP rbp
MOVZX rax,word [rbp+200]
PUSH rax
POP qword [v_single]

Code: Select all

Structure udtDerp
  chars.c[1000]
EndStructure

Global derp.udtDerp
Global Dim single(10)

Define a = 100
single(1) = derp\chars[a]
Simplifies inserting code between push/pop
; single(1) = derp\chars[a]
LEA rbp,[v_derp]
PUSH rbp
MOV rax,qword [v_a]
ADD rax,rax
POP rbp
ADD rbp,rax
MOVZX rax,word [rbp]
PUSH rax
MOV rbp,qword [a_single]
POP qword [rbp+8]