试图在堆栈上创建两个局部变量但只能创建一个

trying to create two local variables on the stack but can only create one

我正在尝试在堆栈上创建两个作为计数器的局部变量 两个循环,但我只能创建一个。我需要推送我不能推送的变量吗 在堆栈上创建一个作为局部变量,还是我做错了?这是我目前的代码:

.386
.model flat,stdcall
.stack 100h

printf proto c arg1:ptr byte, printlist:vararg

.data

array dword 180,8,9,10,40,80,0
fmtmsg2 db 0dh,0ah,0
fmtmsg1 db "%d ",0
counter dword 6,0
.code

bubbleSort proc
  push ebp
  mov ebp,esp
  sub esp,4
  mov dword ptr[ebp - 4],0
  mov esi,[ebp + 12]
  mov ebx,[ebp + 8]

  ;trying to create two local variables but only can create one
  mov edx,0
  ;mov ecx,0


innerloop:
        inc dword ptr[ebp - 4]       ;========>   ;inc ecx           
        cmp dword ptr[ebp - 4],ebx   ;=========> ;cmp ecx,ebx         
      je outerloop       
      mov eax,[esi]      
      cmp eax,[esi + 4]  
      Jge noexchange 
        ;exchange values   
      xchg eax,[esi+4]   
      mov [esi],eax      
  noexchange:
      add esi,4           
      jmp innerloop          

outerloop:
     mov esi,offset array

     ;inner loop counter
     mov dword ptr[ebp - 4],0           ;========>;mov ecx,0 

     ;outer loop counter
     inc edx     

     cmp edx,ebx
     jne innerloop

     ;loop 3 counter
     mov edx,0

     ;load array offset
     mov esi,[ebp + 12]

     mov esp,ebp
     pop ebp
     ret 8
bubbleSort endp

displayArray proc
     push ebp
     mov ebp,esp
     mov esi,[ebp + 12]
     mov ebx,[ebp + 8]
     mov edx,0

     loop3:
     mov eax,[esi]
     push edx
     invoke printf,addr fmtmsg1,eax
     pop edx

     add esi,4
     inc edx
     cmp edx,ebx
     jne loop3

     invoke printf,addr fmtmsg2

     pop ebp
     ret 8
displayArray endp

public main

main proc

      push offset array
      push counter
      call bubbleSort

      push offset array
      push counter
      call displayArray

     ret
main endp

end main

当我尝试添加另一个局部变量时,程序崩溃了,我不知道是什么问题。

请记住,堆栈向下增长 - 因此在堆栈上传递的参数是正数(相对于堆栈指针或其在 ebp 中的副本),局部变量(存储在您插入的 "hole" 中)是负数并且 return 地址(通常未被代码使用)位于 0.

sub     esp, 4          ; allocates 4 (extra) bytes on the stack.
    ....
mov dword ptr[ebp - 4],0   ; References those 4 bytes
    ....
mov esp,ebp                ; Puts the stack pointer back where it was

要分配2个4字节的变量,需要从栈中分配8个字节,相对于ebp引用为-4和-8。