为什么char数组中的linux 0x80中断程序编号4函数(sys_write)不起作用?

时间:2018-06-03 14:24:03

标签: c linux gcc assembly interrupt

我想通过调用linux 0x80中断将字符串输出到stdout,但是当我使用char数组作为参数时,它不能产生输出。

当我使用char指针变量作为参数时,没有问题。

这是我的代码(文件:c.c)。输出是print" cd"不是" abcd"。

void prints(char *str, int size){
    asm(
        "movl $4, %%eax \n\t"
        "movl $1, %%ebx \n\t"
        "movl %0, %%ecx \n\t"
        "movl %1, %%edx \n\t"
        "int $0x80 \n\t"
        ::"m"(str), "m"(size)
       );

    return;
}

int main(){
    char str[] = {'a', 'b', '\0'};
    char *str1 = "cd";
    prints(str, 3);
    prints(str1, 3);
}

我使用gcc -S c.c,输出文件c.s就像这样。 我仍然无法理解为什么" ab"没有输出。

    .file   "c.c"
    .text
.globl prints
    .type   prints, @function
prints:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movq    %rdi, -8(%rbp)
    movl    %esi, -12(%rbp)
#APP
# 2 "c.c" 1
    movl $4, %eax 
    movl $1, %ebx 
    movl -8(%rbp), %ecx 
    movl -12(%rbp), %edx 
    int $0x80 

# 0 "" 2
#NO_APP
    nop
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   prints, .-prints
    .section    .rodata
.LC0:
    .string "cd"
    .text
.globl main
    .type   main, @function
main:
.LFB1:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    movb    $97, -16(%rbp)
    movb    $98, -15(%rbp)
    movb    $0, -14(%rbp)
    movq    $.LC0, -8(%rbp)
    leaq    -16(%rbp), %rax
    movl    $3, %esi
    movq    %rax, %rdi
    call    prints
    movq    -8(%rbp), %rax
    movl    $3, %esi
    movq    %rax, %rdi
    call    prints
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE1:
    .size   main, .-main
    .ident  "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-18)"
    .section    .note.GNU-stack,"",@progbits

0 个答案:

没有答案
相关问题