Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re^2: Help me to understand increment / decrement operator behavior in perl by BrowserUk
in thread Help me to understand increment / decrement operator behavior in perl by sam_bakki

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-19 07:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found