user-0 提问: C语言 汇编混编 数组传参问题  帮忙看看

回复:
user: user-1
content: 可以学学这些排序算法 sort函数排序 https://blog.csdn.net/weixin_43956598/article/details/90241551 冒泡排序 https://blog.csdn.net/weixin_43956598/article/details/90176251 选择排序 https://blog.csdn.net/weixin_43956598/article/details/90178197 插入排序 https://blog.csdn.net/weixin_43956598/article/details/90181567 快速排序 https://blog.csdn.net/weixin_43956598/article/details/90215135 希尔排序 https://blog.csdn.net/weixin_43956598/articledetails/90234480 堆排序https://blog.csdn.net/weixin_43956598/article/details/90343547

user: user-0
content: 引用 6 楼 Intel0011 的回复:a没有解释为地址，所以用lea ebx, a p解释为地址了，所以用mov ebx, p一语道破天机  非常感谢！！！

user: user-2
content: a没有解释为地址，所以用lea ebx, a p解释为地址了，所以用mov ebx, p

user: user-2
content: 这个结果是对的 #include <stdio.h>  int main(void) {    int i;    int a[]={5,-8,9,4,1};    int *sort(int *p);        sort(a);        for (i = 0; i <= 4; i++)    {       printf("\t%d", a[i]);    }    printf("\n");    getchar();    return 0; }  int *sort(int *p) {    int n=5;    _asm    {       //pushad       mov ebx,p       mov ecx,n       dec ecx    loop1:       mov edx,ecx       mov esi,0    loop2:       mov eax,[ebx+esi]       cmp eax,[ebx+esi+4]       jge next       xchg eax,[ebx+esi+4]       mov [ebx+esi],eax    next:       add esi,4       loop loop2       mov ecx,edx       loop loop1       //popad    }    return p; }

user: user-2
content: #include <stdio.h>  int main(void) {    int i;    int a[]={5,-8,9,4,1};    int *sort(int *p);        sort(a);        for (i = 0; i <= 4; i++)    {       printf("\t%d", a[i]);    }    printf("\n");    getchar();    return 0; }  int *sort(int *p) {    _asm    {       //pushad       mov ebx,p // <-- lea ebx, p       mov ecx,5       dec ecx    loop1:       mov edx,ecx       mov esi,0    loop2:       mov eax,[ebx+esi]       cmp eax,[ebx+esi+4]       jge next       xchg eax,[ebx+esi+4]       mov [ebx+esi],eax    next:       add esi,4       loop loop2       mov ecx,edx       loop loop1       //popad    }    return p; }

user: user-3
content: 你应该看一下return 这句在C函数里编译做了什么，C函数调用返回结果在eax中,你可以返回之前打印eax和返回之后的p出来和返回的结果对比一下,你这个可以定义成void sort(*p)最后不用返回

user: user-0
content: 引用 1 楼 Intel0011的回复:1. 保存和恢复寄存器，因为系统会改写eax等寄存器 2. 循环中不能简单使用sort(a)，返回的是一个指针，并未随循环而变化 int main(void) {    int i;    int a[]={5,-8,9,4,1};    int *sort(int *p);        sort(a);        for (i = 0; i <= 4; i++)    {       printf("\t%d", a[i]);    }    printf("\n");    return 0; }  int *sort(int *p) {    int n=5;    _asm    {       pushad       lea ebx,p       mov ecx,n       dec ecx    loop1:       mov edx,ecx       mov esi,0    loop2:       mov eax,[ebx+esi]       cmp eax,[ebx+esi+4]       jge next       xchg eax,[ebx+esi+4]       mov [ebx+esi],eax    next:       add esi,4       loop loop2       mov ecx,edx       loop loop1       popad    }    return p; } #else int main(void) {    int i;    int a[]={5,-8,9,4,1};    int n=5;        _asm    {       lea ebx,a       mov ecx,n       dec ecx    loop1:       mov edx,ecx       mov esi,0    loop2:       mov eax,[ebx+esi]       cmp eax,[ebx+esi+4]       jge next       xchg eax,[ebx+esi+4]       mov [ebx+esi],eax    next:       add esi,4       loop loop2       mov ecx,edx       loop loop1    }        for (i = 0; i <= 4; i++)    {       printf("\t%d", a[i]);    }    printf("\n");    return 0; }  修改之后还是不能实现功能。。。。能帮我再改改么

user: user-2
content: 1. 保存和恢复寄存器，因为系统会改写eax等寄存器 2. 循环中不能简单使用sort(a)，返回的是一个指针，并未随循环而变化 int main(void) {    int i;    int a[]={5,-8,9,4,1};    int *sort(int *p);        sort(a);        for (i = 0; i <= 4; i++)    {       printf("\t%d", a[i]);    }    printf("\n");    return 0; }  int *sort(int *p) {    int n=5;    _asm    {       pushad       lea ebx,p       mov ecx,n       dec ecx    loop1:       mov edx,ecx       mov esi,0    loop2:       mov eax,[ebx+esi]       cmp eax,[ebx+esi+4]       jge next       xchg eax,[ebx+esi+4]       mov [ebx+esi],eax    next:       add esi,4       loop loop2       mov ecx,edx       loop loop1       popad    }    return p; } #else int main(void) {    int i;    int a[]={5,-8,9,4,1};    int n=5;        _asm    {       lea ebx,a       mov ecx,n       dec ecx    loop1:       mov edx,ecx       mov esi,0    loop2:       mov eax,[ebx+esi]       cmp eax,[ebx+esi+4]       jge next       xchg eax,[ebx+esi+4]       mov [ebx+esi],eax    next:       add esi,4       loop loop2       mov ecx,edx       loop loop1    }        for (i = 0; i <= 4; i++)    {       printf("\t%d", a[i]);    }    printf("\n");    return 0; }