1

我如何从堆栈中将字符串(例如“Hello”)写入标准输出?没有数据段,也就是说。

void main() {
    __asm__(
                "movl  $0x4, %eax   \n\t"
                "movl  $0x1, %ebx   \n\t"
            //   put "Hello" on the stack and load its address into %ecx
                "movl  $0x5, %edx   \n\t"
                "int   $0x80        \n\t"

                "movl  $0x1, %eax   \n\t"
                "movl  $0x0, %ebx   \n\t"
                "int   $0x80        \n\t"
             );
}

提前致谢

4

3 回答 3

3

答案1:

int main()
{
    const char* string = "hello";  // string is not in a data segment, it's in the text segment
    fputs(string, stdout);
    return 0;
}

答案 2:

int main()
{
    char[6] string = "hello";  // Space for string allocated on stack
    fputs(string, stdout);
    return 0;
}

使用 gcc,第二个答案似乎产生以下内容:

main:      
    leal    4(%esp), %ecx
    andl    $-16, %esp
    pushl   -4(%ecx)
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %ecx
    subl    $36, %esp
    movl    $1819043176, -10(%ebp) ;<< hell
    movw    $111, -6(%ebp)         ;<< o\0
    movl    stdout, %eax
    movl    %eax, 4(%esp)
    leal    -10(%ebp), %eax
    movl    %eax, (%esp)
    call    fputs
    movl    $0, %eax
    addl    $36, %esp
    popl    %ecx
    popl    %ebp
    leal    -4(%ecx), %esp

这显然只使用堆栈。

于 2010-07-16T11:00:08.823 回答
1
int main() {
        char *hello = "Hello world!\n";
        __asm__("\
                movl $4, %%eax\n\
                movl $0, %%ebx\n\
                push %0\n\
                pop %%ecx\n\
                movl $13,%%edx\n\
                int $0x80" : :"g"(hello));
        return 0;
}

我不明白堆栈部分。为什么不使用 `movl %0,%%ecx'?

于 2010-07-16T10:05:52.173 回答
0

我如何从堆栈中将字符串(例如“Hello”)写入标准输出?没有数据段,也就是说。

类似的东西:

  1. 在堆栈上分配缓冲区,例如 8 个字节。(检查GCC alloca()是如何做到的。)
  2. 在缓冲区中创建字符串。“你好”字节为 48 65 6c 6c 6f 00 00 00 (对齐,用零填充)。两个 32 位常量 - 0x6c6c6548 和 0x0000006f - 应该足以表示字符串。
  3. 将缓冲区的地址传递给 write 系统调用。
  4. 将堆栈指针恢复到之前的位置(如果需要)。

不能更精确,因为我对 GCC 内联 asm 和 x86 ABI 不是很了解。

于 2010-07-16T10:12:52.037 回答