回 帖 发 新 帖 刷新版面

主题:求助,谁能帮我看看那里出错了

一个排序的算法,想用汇编写

for(i=0;i<9;i++)
{
    for(int j=i+1;j<9;j++)
    {
        if (a[i]<a[j])
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }
}

把只些代码改用汇编写...

我写的代码...    

.model small
.stack
.data

a db 11h,15h,45h,34h,7h,65h,16h,33h
temp db ?
i equ $-a
j db ?

.code
.startup

mov cx,0 ; cx相当与外循环的i
.while cx<i
push cx


mov ax,[si+cx]
.while cx<i ;相当于内循环的j
.if ax<[si+cx+1]
mov temp,ax
mov ax,[si+cx+1]
mov [si+cx+1],temp
.endif
inc cx
.endw

pop cx;i
inc cx

.endw


.exit 0
end

但怎么也调不通...

还有一个问题是,如何实现C++混合汇编?
用刚才那个为例

#include<iostream>
using namespace std;

int main()
{
    int a[]={11,5,34,67,34,69,32,2,23};

    for(int i=0;i<9;i++)
    {
        cout<<a[i]<<" ";
    }

    cout<<endl;
    //上面的for用的显示排序前的输出
    int temp;

  [color=FF0000]     for(i=0;i<9;i++)
      {
    for(int j=i+1;j<9;j++)
    {
        if (a[i]<a[j])
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }
      }[/color]            



    for( i=0;i<9;i++)
    {
        cout<<a[i]<<" ";
    }

//排序后的输出




    return 0;
}


红色的排序代码应如何修改,用汇编实现?如何写入口参数和出口参数?
        

回复列表 (共2个回复)

沙发

程序最新修改...但都是报错...希望高手指点...

.model small
.stack
.data

a db 11h,15h,45h,34h,7h,65h,16h,33h
;temp dw 0
i equ $-a
j db ?

.code
.startup

mov si,offset a
mov cx,0 ; cx相当与外循环的i
.while cx<i
push cx

;内循环

mov ax,[si+cx]   

.while cx<i

.if ax<[si+cx+1]
xchg ax,[si+cx+1]


        
.endif
inc cx
.endw
;内循环结束

pop cx;i
inc cx

.endw




板凳

简化段定义不怎样用,爱莫能助。。。。
不过在相对基址加变址的寻址方式中
EA=(BX)/(BP)+(SI)/(DI)+16/8位偏移
是不是mov ax,[si+cx]要改成
mov bx,cx
mov ax,[bx+si+1]
?(没有调试过,下面还要改)

另,在C++中调用汇编就是在一个工程中,将汇编写成一个独立的块,直接当函数一样调用即可(可参考VS2003。NET的SAMPLE)

我来回复

您尚未登录,请登录后再回复。点此登录或注册