Assembly 在汇编中填充数字数组

Assembly 在汇编中填充数字数组,assembly,nasm,Assembly,Nasm,为了用数字字符串填充数组,我编写了以下代码9876..1234: segment .data counter dd 9877 count dd 8642 array times 9876 dd '0000' segment .bss buffer resb 4 segment .text global _start _start: mov ecx, [count] mov edx, arra

为了用数字字符串填充数组,我编写了以下代码
9876..1234

segment     .data
  counter   dd 9877
  count     dd 8642
  array     times 9876 dd '0000' 

segment     .bss
  buffer    resb  4 

segment     .text
  global    _start

  _start: 

  mov ecx, [count]

  mov edx, array
  mov dword [pointer], array 

  l1:
    push ecx
    push edx

    dec dword [counter]                      ; decrease the counter
    lea esi, [buffer]
    mov eax, [counter]
    call int_to_string

    mov ecx, eax ; "begin print" position
    xor edx, edx
getlen:
    cmp byte [ecx + edx], 10
    jz gotlen
    inc edx
    jmp getlen
gotlen:
    inc edx

    push eax                             ; printing the string converted
    mov eax, 4
    mov ebx, 1
    int 0x80
    pop eax                              ; printing the string converted


    pop edx                              ; copying the string to array
    mov [edx], eax
    add edx, 4

    pop ecx
  loop l1


  mov ecx, [count]                  ;printing the array
  mov eax, array
  l2:
    push ecx

    mov [pointer], eax
    push eax

    mov eax, 4
    mov ebx, 1
    mov ecx, pointer
    mov edx, 4
    int 0x80

    pop eax
    add eax, 4

    pop ecx
  loop l2

  mov   eax, 1
  mov   ebx, 0
  int   0x80

int_to_string:
  add   esi, 9 ; counter + 9 ?
  mov   byte [esi], 10
  mov   ebx, 10         

.next_digit:
  xor   edx, edx        
  div   ebx             
  add   dl, '0'          
  dec   esi             
  mov   [esi],dl
  test  eax, eax            
  jnz   .next_digit
  mov   eax, esi
  ret
打印输出导致在屏幕上打印随机字符。是什么导致我的代码出现这种情况?

三个中心错误:

1) “缓冲区”太小&缺少指针

改变

segment     .bss
  buffer    resb  4
pop eax                              ; printing the string converted


pop edx                              ; copying the string to array
mov [edx], eax
add edx, 4
mov eax, 4
mov ebx, 1
mov ecx, pointer
mov edx, 4
int 0x80


2“复制”是错误的

改变

segment     .bss
  buffer    resb  4
pop eax                              ; printing the string converted


pop edx                              ; copying the string to array
mov [edx], eax
add edx, 4
mov eax, 4
mov ebx, 1
mov ecx, pointer
mov edx, 4
int 0x80


3“打印”是错误的

改变

segment     .bss
  buffer    resb  4
pop eax                              ; printing the string converted


pop edx                              ; copying the string to array
mov [edx], eax
add edx, 4
mov eax, 4
mov ebx, 1
mov ecx, pointer
mov edx, 4
int 0x80


您有两个循环,它们似乎都在向stdout写入。哪一个是导致打印不正确的原因?什么是
指针
?我看不到它在代码中的任何地方被声明。另外,一个明显的问题是,您只为
缓冲区
保留了4个字节的空间。正如我在之前的一些回答中发布
int\u to_string
函数时所写的那样,
esi
指向的缓冲区预计至少有10个字节的空间。