c->asm汇编

本文详细介绍了如何将C语言代码转换为汇编语言,包括预处理、汇编、编译和链接四个步骤,并提供了一个简单的C程序作为示例,展示了对应的汇编代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Compile:

1. redirect, preprocess

gcc -E test_asm.c -o test_asm.i

2. to asm

gcc -S test_asm.i -o tset_asm.s

3. to obj

gcc -c test_asm.s -o test_asm.o

4. link

gcc test_asm.o -o test_asm


=====

fred@fred-Vostro-2420:~/myspace/sets/test$ gcc -E test_asm.c -o test_asm.i
fred@fred-Vostro-2420:~/myspace/sets/test$ gcc -S test_asm.i -o test_asm.s
fred@fred-Vostro-2420:~/myspace/sets/test$ gcc -c test_asm.s -o test_asm.o
fred@fred-Vostro-2420:~/myspace/sets/test$ gcc test_asm.o -o test_asm
fred@fred-Vostro-2420:~/myspace/sets/test$ ./test_asm
i=6, j=5
fred@fred-Vostro-2420:~/myspace/sets/test$

1) .c

fred@fred-Vostro-2420:~/myspace/sets/test$ more test_asm.c
#include<stdlib.h>
#include<stdio.h>
int swap(int *a, int *b)
{
    int middle = *b;
    *b = *a;
    *a = middle;
    return 0;   
}

int main(int argc, char *argv[])
{
    int i = 5;
    int j;
    
    j = 6;
    swap(&i, &j);
   
    printf("i=%d, j=%d\n", i, j);

    return 0;
}
fred@fred-Vostro-2420:~/myspace/sets/test$

2) .asm

fred@fred-Vostro-2420:~/myspace/sets/test$ more test_asm.s
    .file    "test_asm.c"
    .text
    .globl    swap
    .type    swap, @function
swap:
.LFB2:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $16, %esp
    movl    12(%ebp), %eax
    movl    (%eax), %eax
    movl    %eax, -4(%ebp)
    movl    8(%ebp), %eax
    movl    (%eax), %edx
    movl    12(%ebp), %eax
    movl    %edx, (%eax)
    movl    8(%ebp), %eax
    movl    -4(%ebp), %edx
    movl    %edx, (%eax)
    movl    $0, %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE2:
    .size    swap, .-swap
    .section    .rodata
.LC0:
    .string    "i=%d, j=%d\n"
    .text
    .globl    main
    .type    main, @function
main:
.LFB3:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    andl    $-16, %esp
    subl    $32, %esp
    movl    $5, 24(%esp)
    movl    $6, 28(%esp)
    leal    28(%esp), %eax
    movl    %eax, 4(%esp)
    leal    24(%esp), %eax
    movl    %eax, (%esp)
    call    swap
    movl    28(%esp), %edx
    movl    24(%esp), %eax
    movl    %edx, 8(%esp)
    movl    %eax, 4(%esp)
    movl    $.LC0, (%esp)
    call    printf
    movl    $0, %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE3:
    .size    main, .-main
    .ident    "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
    .section    .note.GNU-stack,"",@progbits

==

b   12(%ebp)

a   8(%ebp)

call swap--------------------------------------like push EIP

(

ebp

local

)

...swap(a,b)

{

push ebp

movl esp, ebp

...

leave(ebp,esp)

ret(->EIP)-------------------------------------like pop EIP, coordinate with CALL swap.

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值