What does this assembly language code mean?

后端 未结 2 708
长情又很酷
长情又很酷 2021-02-04 21:39

I am a student and just started studying assembly language. To understand it better I just wrote a short in C and converted it to assembly language. Surprisingly I didn\'t under

2条回答
  •  难免孤独
    2021-02-04 22:02

    For me, intels syntax is easier to read, learning how to generate intels syntax is handy for understanding C programs better;

    gcc -S -masm=intel file.c
    

    In windows your C program becomes;

        .file   "file.c"
        .intel_syntax noprefix
        .def    ___main;    .scl    2;  .type   32; .endef
        .section .rdata,"dr"
    LC0:
        .ascii "%d\0"
        .text
        .globl  _main
        .def    _main;  .scl    2;  .type   32; .endef
    _main:
    LFB13:
        .cfi_startproc
        push    ebp
        .cfi_def_cfa_offset 8
        .cfi_offset 5, -8
        mov ebp, esp
        .cfi_def_cfa_register 5
        and esp, -16
        sub esp, 32
        call    ___main
        mov DWORD PTR [esp+28], 4
        mov eax, DWORD PTR [esp+28]
        mov DWORD PTR [esp+4], eax
        mov DWORD PTR [esp], OFFSET FLAT:LC0
        call    _printf
        mov eax, 0
        leave
        .cfi_restore 5
        .cfi_def_cfa 4, 4
        ret
        .cfi_endproc
    LFE13:
        .ident  "GCC: (rev2, Built by MinGW-builds project) 4.8.1"
        .def    _printf;    .scl    2;  .type   32; .endef
    

    (the compiler options should be the same on ubuntu as in windows)

    Apart from the psychotic labels, this is more like the assembly i read about in text books..

    Here is a way of looking at it;

        call    ___main
    
        mov DWORD PTR [esp+28], 4  
        mov eax, DWORD PTR [esp+28]              ; int n = 4;
    
        mov DWORD PTR [esp+4], eax 
        mov DWORD PTR [esp], OFFSET FLAT:LC0
        call    _printf                          ; printf("%d",n);
    
        mov eax, 0
        leave                                    ; return 0;
    

提交回复
热议问题