通过在INVOKE之前推送参数来循环遍历ASM中的数组

时间:2016-04-18 02:20:12

标签: arrays assembly x86 masm irvine32

我在调用程序之前将信息推送到堆栈,我不确定我在做什么,或者我做错了什么。我创建了PrintArray proc,它应该只是打印出一个数组。

我这样称呼程序:

push OFFSET array
push count ; length of array
push TYPE array
INVOKE PrintArray

然后是实际程序:

 PrintArray proc
    push ebp 
    mov ebp, esp
    pushad  
    mov esi, OFFSET array;[ebp+16]
    mov ecx, 12;[ebp+12] 
    L3: 
         mov eax, [esi]
         call WriteInt
         mov edx, OFFSET endl 
         call WriteString
         add esi, TYPE array; [ebp+8]
    loop L3
    popad               ; restores registers
    pop ebp             ; restores ebp
    INVOKE ShowParams 
PrintArray endp

当我尝试用我注释掉的内容(即ebp + 12)替换硬编码数字(OFFSET数组,12和TYPE数组)时,程序崩溃了。我不确定我是否正确使用ebp,但我在使用用户输入填充数组的过程中执行相同的操作,并且构建并运行正常。

我不知道这是否相关,但即使我对参数进行硬编码,输出也不正确。例如,如果我输入1,2,3,4,5,6,7,8,9,10,11,12,则输出:

131073,196610,262147 {...} 786443,12。数组中的最后一个数字始终是正确的。

我的完整计划:

 INCLUDE c:\Irvine\Irvine32.inc

 ExitProcess proto,dwExitCode:dword

 .data      ;// write your data in this section
input BYTE "Enter 12 numbers", 0dh, 0ah, 0
firstdisplay BYTE "Stack after InputArray and ShowParams calls", 0dh, 0ah, 0
seconddisplay BYTE "Stack after CalculateElements and ShowParams calls", 0dh, 0ah, 0
thirddisplay BYTE "Stack after PrintArray and ShowParams calls", 0dh, 0ah, 0
stackparam BYTE "Stack Parameters: ", 0dh, 0ah, "---------------------------", 0dh, 0ah, 0
     address BYTE "Address ", 0
     equals BYTE " = ", 0
    endl BYTE 0dh, 0ah, 0
    count = 12
    array WORD count DUP(?)

 .code          ;// write your program here
  main proc

InputArray PROTO            ; prototypes InputArray procedure
ShowParams PROTO            ; prototypes ShowParams procedure
CalculateElements PROTO     ; prototypes CalculateElements procedure
PrintArray PROTO            ; prototypes PrintArray Procedure 


mov edx, OFFSET input       ; sets the edx to the address of input
call WriteString            ; prints the edx to the screen

push OFFSET array           ; pushes the address of the array to the stack for invoking InputArray
push count                  ; pushes count to the stack for invoking InputArray
push TYPE array             ; pushes the type of the array to the stack for invoking InputArray
INVOKE InputArray           ; pushes parameters to the stack and calls InputArray

mov edx, OFFSET endl 
call WriteString
mov edx, OFFSET seconddisplay
call WriteString
push count
push SIZEOF array
push TYPE array 
push OFFSET array
INVOKE CalculateElements

push OFFSET array
push count
push TYPE array
INVOKE PrintArray

InputArray proc             ; start of InputArray procedure
    push ebp                ; preserves ebp
    mov ebp, esp
    pushad                  ; saves all registers
    mov esi, [ebp+16]    ;offset of array
    mov ecx, [ebp+12]    ;array length 
    L1: 
        call ReadInt
        mov [esi], eax
        add esi, TYPE array;[ebp+8]
    loop L1

    popad               ; restores registers
    pop ebp             ; restores ebp

    mov edx, OFFSET endl 
    call WriteString
    mov edx, OFFSET firstdisplay 
    call WriteString
    INVOKE ShowParams
    ret
InputArray endp

ShowParams proc 
    mov edx, OFFSET stackparam 
    call WriteString 
    mov ecx, 8
    L2:
        mov edx, OFFSET address 
        call WriteString 
        mov eax, ebp
        add eax, 4
        call WriteHex
        mov edx, OFFSET equals 
        call WriteString
        mov eax, ebp
        add eax, 8
        call WriteHex 
        add ebp, 4
        mov edx, OFFSET endl 
        call WriteString
    loop L2
    ret

ShowParams endp

CalculateElements proc 
    INVOKE ShowParams
CalculateElements endp 

PrintArray proc
    push ebp 
    pushad
    mov ebp, esp
    mov edx, OFFSET endl 
    call WriteString
    mov esi, OFFSET array;[ebp+16]    offset of array
    mov ecx, count ;[ebp+12]     array length 
    L3: 
         mov eax, [esi]
         call WriteInt
         mov edx, OFFSET endl 
         call WriteString
         add esi, TYPE array; [ebp+8]
    loop L3
    popad               ; restores registers
    pop ebp             ; restores ebp
    mov edx, OFFSET endl 
    call WriteString
    mov edx, OFFSET endl 
    call WriteString
    mov edx, OFFSET thirddisplay
    call WriteString 
    INVOKE ShowParams 
PrintArray endp
invoke ExitProcess,0

main endp
end main

0 个答案:

没有答案