Assembly: write to an array

I created the following program to read in 5 numbers, and then dumpregto find out what numbers were entered ...

        INCLUDE Irvine32.inc

    .data
    count = 5
    scores WORD count DUP(? )
    prompt BYTE "Please type an integer score: ", 0

    .code
    GetScores PROTO, wArray:PTR WORD, arraySize : WORD

    main proc


    INVOKE GetScores,OFFSET scores, count

    mov esi, OFFSET scores
    mov ecx, count
    mov ebx, 2
    call DumpMem
    mov eax, 50000
    call Delay

    exit
    main endp

    GetScores PROC, wArray:PTR WORD, arraySize : WORD
    push ebp
    mov ebp, esp
    pushad

    mov esi, wArray
    movzx ecx, arraySize
    cmp ecx, 0; ECX < 0 ?
    jle L2; yes: skip over loop
    L1 :
call ReadInt
mov[esi], eax
add esi, TYPE WORD
loop L1

L2 : popad
    pop ebp
    ret 8
    GetScores ENDP

    END main

This is the first time I use stack parameters and I get an error Exception thrown at 0x0040365A in Project.exe: 0xC0000005: Access violation writing location 0x0040361C.after entering the first number.

I believe this is due to the problem of my indexing in my array, but I'm not sure where the problem is. Any help is appreciated!

+4
source share
1 answer

When you use PROC( ..., wArray:PTR WORD, arraySize : WORD) with parameters , MASM automatically inserts the prolog and epilogue and calculates the parameter addresses according to this prolog.

When you added the second prologue:

push ebp
mov ebp, esp

EBP , . ECX .

:

GetScores PROC STDCALL, wArray:PTR WORD, arraySize : WORD
;    push ebp               ; superfluous and harmful prolog
;    mov ebp, esp
    pushad

    mov esi, wArray
    movzx ecx, arraySize
    cmp ecx, 0              ; ECX < 0 ?
    jle L2                  ; yes: skip over loop

    L1 :
    call ReadInt
    mov[esi], eax
    add esi, TYPE WORD
    loop L1

    L2 :
    popad
;    pop ebp                ; superfluous epilog
    ret                     ; becomes `ret 8` due to "STDCALL" in the PROC header
GetScores ENDP
+5

Source: https://habr.com/ru/post/1619287/


All Articles