具有连续编号的程序集填充数组

Assembly fill array with Consecutive numbers

我在 Assembly 中遇到简单数组的问题。这是正确的做法吗?我需要用连续的数字填充数组,在这种情况下它是10。代码如下:

section .data
array TIMES 10 db 0

section .text
global _start
_start:
mov eax,10  ;counter
mov ebx,0   ;start from value 0
mov ecx,array;

loop:   
mov [ecx],ebx
inc ecx     ;increase pointer to next array element
inc ebx     ;increase inputted value
dec eax     ;decrease iterator
jnz loop    ;if iterator not 0 loop

mov eax,1   ;eit
int 0x80

这是正确的代码吗?

使用 gdb 调试时。第 11 行(5 个循环)上的断点显示以下输出:

: $eax = 4
: $ebx = 6
: $ecx = 134516902

x/d $ecx-1   0x80490a5:   5

如何从整个数组中查找值?只有我的这个地方 $ecx-1 显示输入数字的正确值。我几乎可以肯定问题出在我的代码上。

亲切的问候

有几种方法可以解决这个问题。在下面的示例中,我们使用 lodsb(加载字节大小的字符串)指令从字节大小元素的数组中检索值。我并不是说这是最有效的方法。

  1 section .data
  2         array  db       3,1,4,1,5,9,2,6,5
  3         len.array equ   $-array
  4
  5 section .text
  6         global _start
  7 _start:
  8         lea esi, [array]  ; load array pointer
  9         mov ecx, len.array  ; use length of array as counter
 10 loop:
 11         lodsb        ; load string byte - will place loads in al;
 12         dec ecx      ; decrements ecx 
 13         cmp ecx, 0   ; 
 14         jnz loop
 15
 16 _exit:
 17
 18         mov eax,1   ;exit
 19         int 0x80

(gdb) 显示 $ax ;当您逐步执行代码时,您可以看到 ax 正在填充数组元素。

12              dec ecx
1: $ax = 3
(gdb)
13              cmp ecx, 0
1: $ax = 3
(gdb)
14              jnz loop
1: $ax = 3
(gdb)
11              lodsb
1: $ax = 3
(gdb)
12              dec ecx
1: $ax = 1
(gdb)
13              cmp ecx, 0
1: $ax = 1
(gdb)
14              jnz loop
1: $ax = 1
(gdb)
11              lodsb
1: $ax = 1
(gdb)
12              dec ecx
1: $ax = 4
(gdb)
13              cmp ecx, 0
1: $ax = 4
(gdb)
14              jnz loop
1: $ax = 4
(gdb)
11              lodsb
1: $ax = 4

抱歉,直到现在我才意识到我最后的回答不是你问的。假设字节大小的元素。

section .bss                                                                                                                                                            
        array:   resb 10                                                                                                                                                
section .data                                                                                                                                                           

section .text                                                                                                                                                           
        global _start                                                                                                                                                   
_start:                                                                                                                                                                 
        lea edi, [array]    ; pointer                                                                                                                                             
        mov ecx, 0   ; element counter                                                                                                                                                   
        mov eax, 1   ; starting number                                                                                                                                                   
loop:                                                                                                                                                                   
        mov [edi+ecx], eax   ; mov n to array element x                                                                                                                                           

        add ecx, 1                                                                                                                                                      
        add eax, 1                                                                                                                                                      
        cmp eax, 10                                                                                                                                                     
        jl loop                                                                                                                                                         

_exit:                                                                                                                                                                  
        mov eax,1   

输出:(gdb) x/8b &array

0x80490a8 <array>:      1       2       3       4       5       6       0       0
21              cmp eax, 10
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       0       0
22              jl loop
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       0       0
17              mov [esi+ecx], eax
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       7       0
19              add ecx, 1
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       7       0
20              add eax, 1
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       7       0
21              cmp eax, 10
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       7       0
22              jl loop
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       7       0
17              mov [esi+ecx], eax
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       7       8