码迷,mamicode.com
首页 > 编程语言 > 详细

C语言与汇编的嵌入式编程:汇编调用函数(两数交换)

时间:2020-02-06 01:19:03      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:处理   style   call   寄存器   汇编代码   转换   c语言   嵌入式   两数交换   

 编写一个两数交换函数swap,具体代码如下:

#include<stdio.h>

void swap(int *a,int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b= temp;
    //printf("a=%d,b=%d,temp=%d\n",a,b,temp);
}

void main(){
    int a=0;
    int b=0;
    char *str1="a=%d,b=%d\n";
    printf("++++++\n");

    a=2;
    b=3;

    printf(str1,a,b);
    
    swap(&a,&b);

    printf(str1,a,b);
} 

技术图片

首先对main函数进行汇编转换:

#include<stdio.h>

void swap(int *a,int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b= temp;
    //printf("a=%d,b=%d,temp=%d\n",a,b,temp);
}

main(){
    int a=0;
    int b=0;
    char *str1="a=%d,b=%d\n";
    printf("++++++\n");
    _asm{
        //a=2
        mov        a,2

        //b=3
        mov        b,3

        //printf(str1,a,b);
        mov        ecx,b
        push    ecx
        mov        eax,a
        push    eax
        mov        edx,str1
        push    edx
        call    printf
        add        esp,12

        //swap(&a,&b);
        lea         eax,b
        push        eax
        lea         ecx,a
        push        ecx
        call        swap
        add         esp,8


        //printf(str1,a,b);
        mov        ecx,b
        push    ecx
        mov        eax,a
        push    eax
        mov        edx,str1
        push    edx
        call    printf
        add        esp,12
    }
}

 

现在需要将swap也转化为汇编,并放入main函数中,具体思路如下:

1、先对swap函数反汇编,并删除ret指令,

注明:swap函数大致处理过程为:把下个地址压入堆栈,然后参数入栈,然后把所有寄存器压入堆栈,分配空间,空间清C然后变量赋值开始程序然后做堆栈平衡清理堆栈

void swap(int *a,int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b= temp;
}

swap:
    push        ebp
    mov         ebp,esp
    sub         esp,44h
    push        ebx
    push        esi
    push        edi
    lea         edi,[ebp-44h]
    mov         ecx,11h
    mov         eax,0CCCCCCCCh
    rep stos    dword ptr [edi]
    mov         eax,dword ptr [ebp+8]
    mov         ecx,dword ptr [eax]
    mov         dword ptr [ebp-4],ecx
    mov         edx,dword ptr [ebp+8]
    mov         eax,dword ptr [ebp+0Ch]
    mov         ecx,dword ptr [eax]
    mov         dword ptr [edx],ecx
    mov         edx,dword ptr [ebp+0Ch]
    mov         eax,dword ptr [ebp-4]
    mov         dword ptr [edx],eax
    pop         edi
    pop         esi
    pop         ebx
    mov         esp,ebp
    pop         ebp
    ret

2、将1中的汇编代码替换掉call swap,

#include<stdio.h>

main(){
    int a=0;
    int b=0;
    char *str1="a=%d,b=%d\n";
    printf("++++++\n");
    _asm{
        //a=2
        mov        a,2

        //b=3
        mov        b,3

        //printf(str1,a,b);
        mov        ecx,b
        push    ecx
        mov        eax,a
        push    eax
        mov        edx,str1
        push    edx
        call    printf
        add        esp,12

        //swap(&a,&b);
        lea         eax,b
        push        eax
        lea         ecx,a
        push        ecx
        //call        swap
        push        ebp
        mov         ebp,esp
        sub         esp,44h
        push        ebx
        push        esi
        push        edi
        lea         edi,[ebp-44h]
        mov         ecx,11h
        mov         eax,0CCCCCCCCh
        rep stos    dword ptr [edi]
        mov         eax,dword ptr [ebp+8]
        mov         ecx,dword ptr [eax]
        mov         dword ptr [ebp-4],ecx
        mov         edx,dword ptr [ebp+8]
        mov         eax,dword ptr [ebp+0Ch]
        mov         ecx,dword ptr [eax]
        mov         dword ptr [edx],ecx
        mov         edx,dword ptr [ebp+0Ch]
        mov         eax,dword ptr [ebp-4]
        mov         dword ptr [edx],eax
        pop         edi
        pop         esi
        pop         ebx
        mov         esp,ebp
        pop         ebp
        add         esp,8


        //printf(str1,a,b);
        mov        ecx,b
        push    ecx
        mov        eax,a
        push    eax
        mov        edx,str1
        push    edx
        call    printf
        add        esp,12
    }
}

3、调整替换的swap汇编

#include<stdio.h>

main(){
    int a=0;
    int b=0;
    char *str1="a=%d,b=%d\n";
    printf("++++++\n");
    _asm{
        //a=2
        mov        a,2

        //b=3
        mov        b,3

        //printf(str1,a,b);
        mov        ecx,b
        push    ecx
        mov        eax,a
        push    eax
        mov        edx,str1
        push    edx
        call    printf
        add        esp,12

        //swap(&a,&b);
        lea         eax,b
        push        eax
        lea         ecx,a
        push        ecx
        //call        swap
        push        ebp
        mov         ebp,esp
        sub         esp,44h
        push        ebx
        push        esi
        push        edi
        lea         edi,[ebp-44h]
        mov         ecx,11h
        mov         eax,0CCCCCCCCh
        rep stos    dword ptr [edi]
        mov         eax,a                    //取a地址
        mov         ecx,dword ptr [eax]        //将a的值赋给ecx
        mov         dword ptr [ebp-4],ecx    //temp=*a=2
        mov         edx,a                    //取a地址
        mov         eax,b                    //取b地址
        mov         ecx,dword ptr [eax]        //取b的值3
        mov         dword ptr [edx],ecx        //a=b=3
        mov         edx,b                    //取b地址
        mov         eax,dword ptr [ebp-4]    //取temp的值2
        mov         dword ptr [edx],eax        //b=temp=2
        pop         edi
        pop         esi
        pop         ebx
        mov         esp,ebp
        pop         ebp
        //add         esp,8


        //printf(str1,a,b);
        mov        ecx,b
        push    ecx
        mov        eax,a
        push    eax
        mov        edx,str1
        push    edx
        call    printf
        add        esp,12
    }
}

 

 

技术图片

 

C语言与汇编的嵌入式编程:汇编调用函数(两数交换)

标签:处理   style   call   寄存器   汇编代码   转换   c语言   嵌入式   两数交换   

原文地址:https://www.cnblogs.com/little-kwy/p/12267261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!