kohlrak

Untitled

Jul 17th, 2017
71
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. int main(int argc, char **argv){
  6.         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.
  7.         be=le;
  8.         asm("bswap %1":"=r"(be):"r"(be):); //Thank you gcc, for making this task more difficult than necessary. If i used compiler output, i'd just "bswap [be]"
  9.         printf("LE: 0x%08x\nBE: 0x%08x\n", le, be);
  10.         return 0;
  11. }
  12. [kohlrak@kohlrak-server temp]$ cat main.s
  13.         .file   "main.cpp"
  14.         .intel_syntax noprefix
  15.         .section        .rodata.str1.1,"aMS",@progbits,1
  16. .LC0:
  17.         .string "LE: 0x%08x\nBE: 0x%08x\n"
  18.         .section        .text.startup,"ax",@progbits
  19.         .globl  main
  20. main:
  21.         lea     ecx, [esp+4]
  22.         and     esp, -16
  23.         push    DWORD PTR [ecx-4]
  24.         push    ebp
  25.         mov     ebp, esp
  26.         push    ecx         #begin atoi(argv[1])
  27.         sub     esp, 8
  28.         mov     eax, DWORD PTR [ecx+4]
  29.         push    10
  30.         push    0
  31.         push    DWORD PTR [eax+4]
  32.         call    strtol
  33.         add     esp, 12         #end atoi(argv[1])
  34.  
  35.         bswap eax           #convert result of atoi(argv[1]) from Little-endian to big-endian. Surprise, the CPU has an instruction to do this that the compiler was *not* using!
  36.  
  37.         push    edx
  38.         push    eax
  39.         push    OFFSET FLAT:.LC0
  40.         call    printf
  41.         mov     ecx, DWORD PTR [ebp-4]
  42.         add     esp, 16         #delete variables
  43.         xor     eax, eax        #set 0 for returning
  44.         leave               #delete other variables
  45.         lea     esp, [ecx-4]
  46.         ret             #return
Advertisement
Add Comment
Please, Sign In to add comment