为程序集中的数组元素赋值(GAS 语法)
Assigning value to array element in assembly (GAS syntax)
我有以下 C 代码 returns 9:
#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 ,%esi
mov array(,%esi,4),%ecx # copy address of array[2] to %ecx
mov ,%ecx
mov ,%eax
mov array(,%esi,4),%ebx # return the value of array[2]
int [=11=]x80
组装并链接到:
gcc -g -c array.s && ld array.o && ./a.out
但是这个程序 returns 0
而不是 9
:
>./a.out
>echo $?
0
我怀疑问题出在注释行中。在尝试寻找问题的各种失败之后,我决定问这个问题:How to change of value an array element (in this case array[2]
) in assembly?
.section .data
array: .zero 16 # allocate 16 bytes instead of 4
.section .text
.globl _start
_start:
mov ,%esi
lea array(,%esi,4),%ecx # copy address of array[2] to %ecx, not the value
mov , %ebx
mov %ebx,(%ecx) # assign value into the memory pointed by %ecx
mov ,%eax
mov array(,%esi,4),%ebx
int [=10=]x80
我有以下 C 代码 returns 9:
#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 ,%esi
mov array(,%esi,4),%ecx # copy address of array[2] to %ecx
mov ,%ecx
mov ,%eax
mov array(,%esi,4),%ebx # return the value of array[2]
int [=11=]x80
组装并链接到:
gcc -g -c array.s && ld array.o && ./a.out
但是这个程序 returns 0
而不是 9
:
>./a.out
>echo $?
0
我怀疑问题出在注释行中。在尝试寻找问题的各种失败之后,我决定问这个问题:How to change of value an array element (in this case array[2]
) in assembly?
.section .data
array: .zero 16 # allocate 16 bytes instead of 4
.section .text
.globl _start
_start:
mov ,%esi
lea array(,%esi,4),%ecx # copy address of array[2] to %ecx, not the value
mov , %ebx
mov %ebx,(%ecx) # assign value into the memory pointed by %ecx
mov ,%eax
mov array(,%esi,4),%ebx
int [=10=]x80