Using assembly and C to Implement Bubble sorting and a bit of thinking

Source: Internet
Author: User

Assembly implementation (AT&T syntax ):

.section .datavalues:.int 33, 25, 67, 10, 1.section .text.global _start_start:nopmovl $values, %esimovl $4, %ecxmovl $4, %ebxloop:movl (%esi), %eaxcmp %eax, 4(%esi)jge skipxchg %eax, 4(%esi)movl %eax, (%esi)skip:add $4, %esidec %ebxjnz loopdec %ecxjz endmovl $values, %esimovl %ecx, %ebxjmp loopend:movl $1, %eaxmovl $0, %ebxint $0x80

C implementation:

#include 
 
  #include 
  
   void PrintValues(int *values, int count){if (NULL == values || 0 >= count)return;for (int i = 0; i  *(values+inner+1)){int temp = *(values+inner);*(values+inner) = *(values+inner+1);*(values+inner+1) = temp; }else {continue;}}}PrintValues(values, count);getchar();return 0;}
  
 
C-implemented disassembly:
int main(int argc, char** argv){ push        ebp   mov         ebp,esp   sub         esp,110h   push        ebx   push        esi   push        edi   lea         edi,[ebp-110h]   mov         ecx,44h   mov         eax,0CCCCCCCCh   rep stos    dword ptr es:[edi]   mov         eax,dword ptr [___security_cookie (257000h)]   xor         eax,ebp   mov         dword ptr [ebp-4],eax  int values[] = {33, 25, 67, 10, 1}; mov         dword ptr [ebp-1Ch],21h   mov         dword ptr [ebp-18h],19h   mov         dword ptr [ebp-14h],43h   mov         dword ptr [ebp-10h],0Ah   mov         dword ptr [ebp-0Ch],1  int count = sizeof(values) / sizeof(*values); mov         dword ptr [ebp-28h],5  PrintValues(values, count); mov         eax,dword ptr [ebp-28h]   push        eax   lea         ecx,[ebp-1Ch]   push        ecx   call        PrintValues (2511BDh)   add         esp,8  for (int outer = 0; outer  *(values+inner+1)) mov         eax,dword ptr [inner]   mov         ecx,dword ptr [inner]   mov         edx,dword ptr [ebp+eax*4-1Ch]   cmp         edx,dword ptr [ebp+ecx*4-18h]   jle         main+0D3h (253663h)  {int temp = *(values+inner); mov         eax,dword ptr [inner]   mov         ecx,dword ptr [ebp+eax*4-1Ch]   mov         dword ptr [temp],ecx  *(values+inner) = *(values+inner+1); mov         eax,dword ptr [inner]   mov         ecx,dword ptr [inner]   mov         edx,dword ptr [ebp+ecx*4-18h]   mov         dword ptr [ebp+eax*4-1Ch],edx  *(values+inner+1) = temp;  mov         eax,dword ptr [inner]   mov         ecx,dword ptr [temp]   mov         dword ptr [ebp+eax*4-18h],ecx  }else  jmp         main+0D5h (253665h)  {continue; jmp         main+88h (253618h)  }} jmp         main+88h (253618h)  } jmp         main+6Bh (2535FBh)  PrintValues(values, count); mov         eax,dword ptr [ebp-28h]   push        eax   lea         ecx,[ebp-1Ch]   push        ecx   call        PrintValues (2511BDh)   add         esp,8  getchar(); mov         esi,esp   call        dword ptr [__imp__getchar (2582B0h)]   cmp         esi,esp   call        @ILT+295(__RTC_CheckEsp) (25112Ch)  return 0; xor         eax,eax  }

When using C, the core of thinking is how to control the loop and exchange data in the right way.

When using assembly, the core of thinking is how to allocate registers and control loops (loop and skip ).

When using assembly, I first thought about the process of the program: loop and skip, then set up the corresponding structure, then enter the code in the structure, and then control the execution process of the program.

Compilation requires more consideration than C, and has higher requirements on programmers. C does not need to be considered. compilation requires consideration. When C # and Java are used to Implement Bubble sorting, you no longer need to think about pointer operations. When using dynamic languages such as Python to implement this algorithm, compared with Java and C #, the temporary variables for data exchange are no longer required, saving the code.

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.