汇编:数组写入问题
Assembly: Array writing Issue
我创建了以下程序来读取 5 个数字,然后 dumpreg
查看输入了哪些数字...
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
这是我第一次使用堆栈参数,输入第一个数字后收到错误 Exception thrown at 0x0040365A in Project.exe: 0xC0000005: Access violation writing location 0x0040361C.
。
我认为这是因为我在数组中的索引存在问题,但我不确定问题出在哪里。非常感谢任何帮助!
当您使用带参数的 PROC
时 (..., wArray:PTR WORD, arraySize : WORD
) MASM 会自动插入序言和结尾并根据序言计算参数的地址。
当您添加第二个序言时:
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 个数字,然后 dumpreg
查看输入了哪些数字...
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
这是我第一次使用堆栈参数,输入第一个数字后收到错误 Exception thrown at 0x0040365A in Project.exe: 0xC0000005: Access violation writing location 0x0040361C.
。
我认为这是因为我在数组中的索引存在问题,但我不确定问题出在哪里。非常感谢任何帮助!
当您使用带参数的 PROC
时 (..., wArray:PTR WORD, arraySize : WORD
) MASM 会自动插入序言和结尾并根据序言计算参数的地址。
当您添加第二个序言时:
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