1

いくつかの素数を計算する次のアセンブリ コードがあります。

#include <stdio.h>

int main() {
    char format[] = "%d\t";

    _asm{
        mov ebx, 1000
        mov ecx, 1
        jmp start_while1
incrementare1:
        add ecx, 1
start_while1:
        cmp ecx, ebx
        jge end_while1
        mov edi, 2
        mov esi, 0
        jmp start_while2
incrementare2:
        add edi, 1
start_while2:
        cmp edi, ecx
        jge end_while2
        mov eax, ecx
        xor edx, edx
        div edi
        test edx, edx
        jnz incrementare2
        mov esi, 1
end_while2:
        test esi, esi
        jnz incrementare1
        push ecx
        lea ecx, format
        push ecx
        call printf
        pop ecx
        pop ecx
        jmp incrementare1
end_while1:
        nop
    }
    return 0;
}

正常に動作しますが、C コードではなく asm で 'format' 文字列も宣言したいと思います。のようなものを追加しようとしましformat db "%d\t", 0たが、うまくいきませんでした。

4

2 に答える 2

2

他のすべてが失敗した場合、常に醜い方法があります。

format_minus_1:
mov ecx,0x00096425  ; '%', 'd', '\t', '\0' in little-endian format
lea ecx,format_minus_1 + 1  ; skip past the "mov ecx" opcode
push ecx
call printf
于 2013-01-23T20:17:52.880 に答える
1

_asmこれらのディレクティブを使用してブロック内のオブジェクトを定義することはできません。C 宣言はスタックにスペースを割り当てているため、ブロック内でそのようなことをしたい場合は_asm、スタック ポインターを操作してメモリを自分で初期化する必要があります。

sub esp, 4
mov [esp], '%'
mov [esp + 1], 'd'
mov [esp + 2], '\t'
mov [esp + 3], '\0'
...
push ecx
push esp + 4
call printf

これは 1 つの方法であることに注意してください。必ずしも最良の方法ではありません。最善の方法は、C にメモリ管理を任せることです。

于 2013-01-23T20:26:20.207 に答える