ASM Output Question regarding push/pop

Bare metal programming in PureBasic, for experienced users
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 376
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

ASM Output Question regarding push/pop

Post 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?
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
Fred
Administrator
Administrator
Posts: 14296
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: ASM Output Question regarding push/pop

Post 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.
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 376
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: ASM Output Question regarding push/pop

Post 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
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
User avatar
CELTIC88
Enthusiast
Enthusiast
Posts: 147
Joined: Thu Sep 17, 2015 3:39 pm

Re: ASM Output Question regarding push/pop

Post 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!
interested in Cybersecurity..
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 376
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: ASM Output Question regarding push/pop

Post 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.
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
Fred
Administrator
Administrator
Posts: 14296
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: ASM Output Question regarding push/pop

Post by Fred »

I will take a closer look to see why it's not removed in release mode.
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 376
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: ASM Output Question regarding push/pop

Post 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?
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
Fred
Administrator
Administrator
Posts: 14296
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: ASM Output Question regarding push/pop

Post 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).
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 376
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: ASM Output Question regarding push/pop

Post 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
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
Fred
Administrator
Administrator
Posts: 14296
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: ASM Output Question regarding push/pop

Post by Fred »

I don't bother about it, I will take a look.
Rinzwind
Enthusiast
Enthusiast
Posts: 384
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: ASM Output Question regarding push/pop

Post by Rinzwind »

Any update?
User avatar
chi
Addict
Addict
Posts: 825
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: ASM Output Question regarding push/pop

Post by chi »

Rinzwind wrote:Any update?
Would be interested in that as well
Et cetera is my worst enemy
User avatar
mk-soft
Addict
Addict
Posts: 3071
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ASM Output Question regarding push/pop

Post 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]
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
Post Reply