Compare two data size Assembly Analysis

Source: Internet
Author: User

Compare the size of two numbers, it is easy to write in C Language

int compare1(int x, int y){    if (x > y)    {        return 1;    }     else if (x == y)     {        return 0;    }    else    {        return -1;    }}

Use GCC to generate PowerPC PPU Assembly commands and use the default-O Option

The generated assembly code:

.compare1:.LFB39:stdu 1,-80(1).LCFI3:std 31,72(1).LCFI4:mr 31,1.LCFI5:mr 0,3mr 9,4stw 0,128(31)stw 9,136(31)lwz 9,128(31)lwz 0,136(31)cmpw 7,9,0ble 7,.L4li 0,1stw 0,48(31)b .L6.L4:lwz 0,128(31)lwz 9,136(31)cmpw 7,0,9bne 7,.L7li 9,0stw 9,48(31)b .L6.L7:li 0,-1stw 0,48(31).L6:lwz 9,48(31)extsw 0,9mr 3,0ld 11,0(1)ld 31,-8(11)mr 1,11blr
Of course, many of these commands are not clear, but some write operations have some pressure on the stack. Below we will use the GCC-O3 option for optimization.
.compare1:.LFB39:cmpw 7,3,4li 3,1ble 7,.L11.L6:extsw 3,3blr.L11:li 3,0beq 7,.L6li 3,-1b .L6
The code is much less, but I use the if statement to redirect a lot. We all know that the jump is time-consuming.

So I wrote another method:

int compare2(int x, int y){    return x < y ? -1 : x == y ? 0 : 1;}
The above code is very concise. Let's take a look at the Code Compiled by GCC-O3. The default-O Option Code will not be listed. You can try the following by yourself:

. Compare2 :. lfb40: XOR, 4 // R3 = x, r4 = ycmpw, 3, 4 srawi 11, 0, 31 // R0 arithmetic right shift 31-bit Li 9,-1xor, 0 subf 3, 3, 11 // This command means R3 = R11-r3blt 7 ,. l15srwi 9, 3, 31 // R9 = R3 logic shifts 31 bits to the right. l15: extsw 3, 9blr
I probably know how to compile, so I wrote one myself based on the compilation practice:

<Span style = "white-space: pre"> </span> Li R9,-1 // R9 =-1 <span style = "white-space: pre "> </span> subf R6, R3, R4 <span style =" white-space: pre "> </span> cmpwi cr7, R6, 0 // Cal the diff <span style = "white-space: pre"> </span> BLT cr7 ,. out // jump to store-1 <span style = "white-space: pre"> </span> Li R7, 0 <span style = "white-space: pre "> </span> subf R6, R6, R7 // Cal 0-diff, if the diff = 0, then R6 = 0, diff> 0, r6 <0 <span style = "white-space: pre"> </span> srwi R9, R6, 31 // mov the sign bit to R9 ,. out: <span style = "white-space: pre"> </span> Mr R3, R9 // R3 is the register that stores the returned value <span style = "white-space: pre "> </span> BLR


The above is the analysis of C and Assembly comparing the two numbers, but there are many jumps when using the if statement.

 
Any errors. Please indicate the source of the post.
Thank you for your cooperation!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.