Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Help me to understand increment / decrement operator behavior in perl

by BrowserUk (Patriarch)
on Mar 04, 2015 at 17:06 UTC ( [id://1118763]=note: print w/replies, xml ) Need Help??


in reply to Re: Help me to understand increment / decrement operator behavior in perl
in thread Help me to understand increment / decrement operator behavior in perl

C behaves differently since it places values on the stack.

Um. Actually, C doesn't place anything on the stack. It loads the values into registers.

This is the MS compiler:

; Listing generated by Microsoft (R) Optimizing Compiler Version 15.00 +.21022.08 include listing.inc INCLUDELIB LIBCMT INCLUDELIB OLDNAMES _DATA SEGMENT $SG2413 DB 'T1', 0aH, 00H $SG2415 DB 'T2', 0aH, 00H _DATA ENDS PUBLIC main EXTRN printf:PROC pdata SEGMENT $pdata$main DD imagerel $LN5 DD imagerel $LN5+87 DD imagerel $unwind$main pdata ENDS xdata SEGMENT $unwind$main DD 010401H DD 06204H ; Function compile flags: /Odtp ; File c:\test\c\junk.c xdata ENDS _TEXT SEGMENT a$ = 32 main PROC ; 3 : void main( void ) { $LN5: sub rsp, 56 ; 00000038H ; 4 : int a = 10; mov DWORD PTR a$[rsp], 10 ; 5 : if( a == a-- ) printf( "T1\n" ); mov edx, DWORD PTR a$[rsp] mov ecx, DWORD PTR a$[rsp] mov eax, DWORD PTR a$[rsp] sub eax, 1 mov DWORD PTR a$[rsp], eax cmp edx, ecx jne SHORT $LN2@main lea rcx, OFFSET FLAT:$SG2413 call printf $LN2@main: ; 6 : if( a == --a ) printf( "T2\n" ); mov eax, DWORD PTR a$[rsp] sub eax, 1 mov DWORD PTR a$[rsp], eax mov eax, DWORD PTR a$[rsp] cmp DWORD PTR a$[rsp], eax jne SHORT $LN1@main lea rcx, OFFSET FLAT:$SG2415 call printf $LN1@main: ; 7 : } xor eax, eax add rsp, 56 ; 00000038H ret 0 main ENDP _TEXT ENDS END

The interesting thing about that is that for the post decrement, it loads the value of a into two registers (edx & ecx), then loads it a third time in to eax, where it decrements it and then writes it back. Only then does it compare hte first two values and branch accordingly.

And for the predecrement, it loads one register, decrements it and then saves it back before loading the modified value back into the same register and the comparing it with the stored value and branching accordingly.

That's before the optimiser takes a look. After the optimiser, all those shenanigins are optimised away and all that remains is the printfs:

; Listing generated by Microsoft (R) Optimizing Compiler Version 15.00 +.21022.08 include listing.inc INCLUDELIB LIBCMT INCLUDELIB OLDNAMES _DATA SEGMENT $SG2478 DB 'T1', 0aH, 00H $SG2480 DB 'T2', 0aH, 00H _DATA ENDS PUBLIC main EXTRN printf:PROC pdata SEGMENT $pdata$main DD imagerel $LN5 DD imagerel $LN5+35 DD imagerel $unwind$main pdata ENDS xdata SEGMENT $unwind$main DD 010401H DD 04204H ; Function compile flags: /Ogtpy ; File c:\test\c\junk.c xdata ENDS _TEXT SEGMENT main PROC ; 3 : void main( void ) { $LN5: sub rsp, 40 ; 00000028H ; 4 : int a = 10; ; 5 : if( a == a-- ) printf( "T1\n" ); lea rcx, OFFSET FLAT:$SG2478 call printf ; 6 : if( a == --a ) printf( "T2\n" ); lea rcx, OFFSET FLAT:$SG2480 call printf ; 7 : } xor eax, eax add rsp, 40 ; 00000028H ret 0 main ENDP _TEXT ENDS END

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re^3: Help me to understand increment / decrement operator behavior in perl
by ikegami (Patriarch) on Mar 04, 2015 at 17:11 UTC
    Whether the stack or a register is used depends on a number of factors. Whether the stack or a register is used is not important here. Fixed.
      Whether the stack or a register is used depends on a number of factors.

      Can you show me one example of C pushing a integer any numeric argument onto the stack in order to compare it to another integer numeric argument?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
        How about machine without registers? How about values retrieved early in the sub and compared late in the sub?

        Try compiling with -O0, that usually forces auto variables to stack (may be useful when debugging).

        Intels icc with -Os (size optimized) may load small constants like so: pushq $18; popq %rdi.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1118763]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2024-04-18 06:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found