kohlrak icon

Untitled

kohlrak | PRO | 07/17/17 08:56:12 PM UTC | 0 ⭐ | 271 👁️ | Never ⏰ | []
ASM (NASM) |

0 B

|

None

|

0 👍

/

0 👎

[kohlrak@kohlrak-server temp]$ cat main.cpp
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char **argv){
        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.
        be=le;
        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]"
        printf("LE: 0x%08x\nBE: 0x%08x\n", le, be);
        return 0;
}
[kohlrak@kohlrak-server temp]$ cat main.s
        .file   "main.cpp"
        .intel_syntax noprefix
        .section        .rodata.str1.1,"aMS",@progbits,1
.LC0:
        .string "LE: 0x%08x\nBE: 0x%08x\n"
        .section        .text.startup,"ax",@progbits
        .globl  main
main:
        lea     ecx, [esp+4]
        and     esp, -16
        push    DWORD PTR [ecx-4]
        push    ebp
        mov     ebp, esp
        push    ecx         #begin atoi(argv[1])
        sub     esp, 8
        mov     eax, DWORD PTR [ecx+4]
        push    10
        push    0
        push    DWORD PTR [eax+4]
        call    strtol
        add     esp, 12         #end atoi(argv[1])
 
        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!
 
        push    edx
        push    eax
        push    OFFSET FLAT:.LC0
        call    printf
        mov     ecx, DWORD PTR [ebp-4]
        add     esp, 16         #delete variables
        xor     eax, eax        #set 0 for returning
        leave               #delete other variables
        lea     esp, [ecx-4]
        ret             #return

Comments