代码之家  ›  专栏  ›  技术社区  ›  builder-7000

为部件中的数组元素赋值(GAS语法)

  •  0
  • builder-7000  · 技术社区  · 7 年前

    #include <stdio.h>
    int main()
    {
        int array[4]={0};
        array[2]=9;
        return array[2];
    }
    

    为了在汇编语言中实现同样的功能,我尝试了:

    .section .data
    array: .zero 4    
    .section .text
        .globl _start
    _start:
        mov $2,%esi
        mov array(,%esi,4),%ecx   # copy address of array[2] to %ecx
        mov $9,%ecx
        mov $1,%eax
        mov array(,%esi,4),%ebx   # return the value of array[2]
        int $0x80
    

    组装并连接到:

    gcc -g -c array.s && ld array.o && ./a.out
    

    0 而不是 9 :

    >./a.out
    >echo $?
    0
    

    我怀疑问题出在评论中。在多次失败的尝试后,我决定问一个问题:如何更改数组元素的值(在本例中 array[2] )装配中?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Nathan Xabedi    7 年前
    .section .data
    array: .zero 16               # allocate 16 bytes instead of 4
    .section .text
        .globl _start
    _start:
        mov $2,%esi
        lea array(,%esi,4),%ecx   # copy address of array[2] to %ecx, not the value
        mov $9, %ebx
        mov %ebx,(%ecx)             # assign value into the memory pointed by %ecx
        mov $1,%eax
        mov array(,%esi,4),%ebx
        int $0x80
    
    推荐文章