kohlrak

Untitled

Jul 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [kohlrak@kohlrak-server temp]$ cat main.cpp
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. template <typename T>
  6. T swap_endian(T u)
  7. {
  8.     //static_assert (CHAR_BIT == 8, "CHAR_BIT != 8"); //Totally unnecessary.
  9.  
  10.     union
  11.     {
  12.         T u;
  13.         unsigned char u8[sizeof(T)];
  14.     } source, dest;
  15.  
  16.     source.u = u;
  17.  
  18.     for (T k = 0; k < sizeof(T); k++)
  19.         dest.u8[k] = source.u8[sizeof(T) - k - 1];
  20.  
  21.     return dest.u;
  22. }
  23.  
  24. int main(int argc, char **argv){
  25.         int le = atoi(argv[1]), be; //For the sake of example, we'll just assume the user will send us the correct number of params.
  26.         be=swap_endian<int>(le);
  27.         printf("LE: 0x%08x\nBE: 0x%08x\n", le, be);
  28.         return 0;
  29. }
  30. [kohlrak@kohlrak-server temp]$ ./a.out 328328
  31. LE: 0x00050288
  32. BE: 0x88020500
  33. [kohlrak@kohlrak-server temp]$ g++ main.cpp -S -masm=intel -O9
  34. [kohlrak@kohlrak-server temp]$ cat main.s
  35.         .file   "main.cpp"
  36.         .intel_syntax noprefix
  37.         .section        .rodata.str1.1,"aMS",@progbits,1
  38. .LC0:
  39.         .string "LE: 0x%08x\nBE: 0x%08x\n"
  40.         .section        .text.startup,"ax",@progbits
  41.         .globl  main
  42. main:
  43. .LFB27:                 #begin variable preserving
  44.         lea     ecx, [esp+4]
  45.         and     esp, -16        #create 2 int variables (taking advantage of it being aligned to use the faster and instruction)
  46.         push    DWORD PTR [ecx-4]
  47.         push    ebp
  48.         mov     ebp, esp        #end variable preserving
  49.         push    ebx         #begin atoi(argv[1]). note: the function atoi is actually here, rather than calling atoi.
  50.         push    ecx
  51.         mov     eax, DWORD PTR [ecx+4]
  52.         sub     esp, 4
  53.         push    10
  54.         push    0
  55.         push    DWORD PTR [eax+4]
  56.         call    strtol          #end atoi(argv[1]), note return value is in eax register AKA beginning of swap_endian(le)
  57.         mov     ecx, eax        #put result of atoi(argv[1]) into ecx register.
  58.         xor     edx, edx        #empty edx register (RAM efficient)
  59.         mov     ebx, eax        #put result of atoi(argv[1]) into ebx register.
  60.         shr     ecx, 24         #performing C++'s >>24 on ecx
  61.         sal     ebx, 24         #performing C++ <<24 on ebx.
  62.         add     esp, 12         #deleting the variables used by atoi(argv[1]).
  63.         mov     dl, cl          #moving the first byte of ecx to edx (aka edx=ecx&0xFF)
  64.         mov     ecx, eax        #moving that return from atoi(argv[1]) into ecx
  65.         shr     ecx, 16         #ecx>>16
  66.         mov     dh, cl          #moving the result from that into the second byte of edx
  67.         movzx   ecx, ah         #moving the second byte of the return of atoi(argv[1]) to ecx
  68.         sal     ecx, 16         #ecx<<16
  69.         movzx   edx, dx         #edx=edx&0xFFFF;
  70.         or      edx, ecx        #edx|=ecx
  71.         or      edx, ebx        #edx|=ebx AKA end of swap_endian(le), result in edx
  72.         push    edx         #begin printf("LE: 0x%08x\nBE: 0x%08x\n", le, be);
  73.        push    eax
  74.        push    OFFSET FLAT:.LC0
  75.        call    printf
  76.        add     esp, 16
  77.        lea     esp, [ebp-8]
  78.         xor     eax, eax        #set eax to 0
  79.         pop     ecx         #end printf("LE: 0x%08x\nBE: 0x%08x\n", le, be);
  80.        pop     ebx
  81.        pop     ebp
  82.         lea     esp, [ecx-4]        #restore preserved registers and variables
  83.         ret             #return; NOTE: eax is the register that holds return values, which is 0.
Advertisement
Add Comment
Please, Sign In to add comment