c - 在汇编中声明一个字符串

标签 c assembly inline-assembly

我有这个计算一些素数的汇编代码:

#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;
}

它工作正常,但我还想在 asm 中声明“格式”字符串,而不是在 C 代码中。我曾尝试添加类似 format db "%d\t", 0 的内容,但没有成功。

最佳答案

如果一切都失败了,总会有丑陋的方式:

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

关于c - 在汇编中声明一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14488269/

相关文章:

c - 为什么我要添加 rax 和 rdx?

c++ - 调用 Windows API 内联汇编的问题

c - 打印消息内联汇编

c - 在文件流中后退一位

java - 在 Java 中将 C 的 uint32 转换为 int 的正确方法

c++ - C++ 中 malloc 的自定义实现

assembly - 16 位 asm 指令集

assembly - 我怎样才能擅长组装?

c - 在平面二进制文件中包含 char 数组的内容

c - 如何处理替换字符串文字以及为什么在堆上分配很重要?