ページ 11

インラインアセンブリの基本 配列編

Posted: 2010年11月08日(月) 22:11
by みけCAT
開発環境はDev-C++4.9.9.2、コンパイラはデフォルトです。
インラインアセンブリでいろいろやってみたいと思っています。
今度は配列から値を読みだしたり、代入したりする方法について質問させていただきます。
配列をソートするプログラムを作りたいです。
以下のコードだと、
(ファイル名) In function `test':
21 (ファイル名) syntax error before ',' token
というエラーが出ます。
お願いします。
#include <stdio.h>

void test(int*);

int main(void) {
    int in[2];
    in[0]=123;
    in[1]=456;
    test(in);
    printf("%d %d\n",in[0],in[1]);
    return 0;
}

void test(int* in) {
    asm volatile (
    "mov %2(,$0,4),%%eax \n\t"
    "mov %2(,$1,4),%%ebx \n\t"
    "mov %%eax,%2(,$1,4)\n\t"
    "mov %%ebx,%2(,$0,4)\n\t"
    : "m" (in)
    : "%eax" , "%ebx"
    );

}

Re:インラインアセンブリの基本 配列編

Posted: 2010年11月08日(月) 22:22
by みけCAT
このコードは、
とりあえず配列の二つの値を交換するプログラムです。

Re:インラインアセンブリの基本 配列編

Posted: 2010年11月10日(水) 15:26
by みけCAT
コードを修正してみました。
同じエラーが出ます。
どなたか解決法がわかる方がいましたら回答をお願いします。
#include <stdio.h>

void test(int*);

int main(void) {
    int in[2];
    in[0]=123;
    in[1]=456;
    test(in);
    printf("%d %d\n",in[0],in[1]);
    return 0;
}

void test(int* in) {
    asm volatile (
    "movl %0(,$0,4),%%eax\n\t"
    "movl %0(,$1,4),%%ebx\n\t"
    "movl %%eax,%0(,$1,4)\n\t"
    "movl %%ebx,%0(,$0,4)\n\t"
    : "m" (in)
    : "%eax" , "%ebx"
    );

}

Re:インラインアセンブリの基本 配列編

Posted: 2010年11月10日(水) 16:17
by ISLe
#include <stdio.h>
void test(int*);
int main(void) {
    int in[2];
    in[0]=123;
    in[1]=456;
    test(in);
    printf("%d %d\n",in[0],in[1]);
    return 0;
}
void test(int* in) {
    asm volatile (
    "movl 0(%0),%%eax\n\t"
    "movl 4(%0),%%ebx\n\t"
    "movl %%eax,4(%0)\n\t"
    "movl %%ebx,0(%0)\n\t"
    :
    : "r"(in)
    : "%eax","%ebx"
    );
}
m指定のパラメータはそれ自体がオフセット付きのアドレッシングに展開されるのでオフセットとして使用できません。
(a,b,c)のアドレッシング構文でbの位置にはレジスタが入ります。即値は書けません。

Re:インラインアセンブリの基本 配列編

Posted: 2010年11月10日(水) 17:26
by みけCAT
ありがとうございます。
実際のプログラムで使用した方法をイメージしたコードを提示して解決...
と行きたいところなのですが、エラーが消えません。
なぜでしょうか?
実際のプログラムはうまく動きました。
#include <stdio.h>

void test(int*);

int main(void) {
    int in[2];
    in[0]=123;
    in[1]=456;
    test(in);
    printf("%d %d\n",in[0],in[1]);
    return 0;
}

void test(int* in) {
    asm volatile (
    "mov %0,%%ecx\n\t"
    "mov $0,%%edx\n\t"
    "mov (%%ecx,%%edx,4),%%eax\n\t"
    "inc %%edx\n\t"
    "mov (%%ecx,%%edx,4),%%ebx\n\t"
    "mov %%eax,(%%ecx,%%edx,4)\n\t"
    "dec %%edx\n\t"
    "mov %%ebx,(%%ecx,%%edx,4)\n\t"
    : "m" (in)
    : "%eax" , "%ebx" , "%ecx" , "%edx"
    );
}
エラーメッセージ
D:\(中略)\test2.c In function `test':
25 D:\(中略)\test2.c syntax error before ',' token

Re:インラインアセンブリの基本 配列編

Posted: 2010年11月10日(水) 18:04
by みけCAT
「実際のプログラム」とは、
http://www.play21.jp/board/formz.cgi?ac ... e=&id=dixq
に投稿した、インラインアセンブリでのクイックソートのプログラムです。

Re:インラインアセンブリの基本 配列編

Posted: 2010年11月10日(水) 18:36
by ISLe
とりあえず:(コロン)が足りない気がします。

Re:インラインアセンブリの基本 配列編

Posted: 2010年11月10日(水) 18:39
by みけCAT
なるほど。
また後でやってみます。