循环中的宏工作一次,然后不工作
Macro in loop works once, then doesn't
过程 ReadVal 应该循环并要求用户输入 10 个整数 (AS STRINGS),最终将每个整数存储在一个数组中。 ReadVal 使用宏 getString 并且它第一次运行。然后另外 9 次,它打印 "Please enter an unsigned integer:" 但不会停下来让用户输入一个值。所以,肯定是在调用宏,但为什么它不停止等待接下来的9次输入。
INCLUDE Irvine32.inc
LEN = 10 ; Number of elements in array.
getString MACRO buffer, size
push eax
push ecx
push edx
mov edx, OFFSET userMsg_2
call WriteString
mov edx, buffer ; buffer passed by reference, so no OFFSET req'd.
mov eax, size
mov ecx, [eax] ; Dereference sizeElem to ecx counter.
call ReadString
pop edx
pop ecx
pop eax
ENDM
.data
userMsg_2 BYTE "Please enter an unsigned number: ",0
array DWORD LEN dup(?)
arLength DWORD LEN
sizeElem BYTE 11 ; Number of digits allowed in. reading ints as strings.
holder DWORD ? ; Temp memory location for storing string input/output.
.code
main PROC
push OFFSET array
push OFFSET arLength
push OFFSET sizeElem
push OFFSET holder
call ReadVal
exit
main ENDP
ReadVal PROC
push ebp
mov ebp, esp
push ecx
mov esi, [ebp+20] ; esi has starting address of array.
mov eax, [ebp+16] ; @arLength
mov ecx, [eax] ; ecx gets arLength.
cmp ecx, 0
je L2
L1:
getString [ebp+8], [ebp+12] ; [ebp+8] gets the user inputted string.
mov esi, [ebp+8] ; mov string in holder into array[esi].
add esi, TYPE DWORD
loop L1
L2:
pop ecx
pop ebp
ret 16
ReadVal ENDP
END main
我认为问题在于 ReadString 期望 ECX 中的值是 DWORD。给它一个数字作为 BYTE 会混淆它。将 sizeElem 声明为 DWORD 可以解决问题。
您可能也希望将 holder 声明为使用 BYTE 而不是 DWORD,我认为这是更典型的字符串存储。
过程 ReadVal 应该循环并要求用户输入 10 个整数 (AS STRINGS),最终将每个整数存储在一个数组中。 ReadVal 使用宏 getString 并且它第一次运行。然后另外 9 次,它打印 "Please enter an unsigned integer:" 但不会停下来让用户输入一个值。所以,肯定是在调用宏,但为什么它不停止等待接下来的9次输入。
INCLUDE Irvine32.inc
LEN = 10 ; Number of elements in array.
getString MACRO buffer, size
push eax
push ecx
push edx
mov edx, OFFSET userMsg_2
call WriteString
mov edx, buffer ; buffer passed by reference, so no OFFSET req'd.
mov eax, size
mov ecx, [eax] ; Dereference sizeElem to ecx counter.
call ReadString
pop edx
pop ecx
pop eax
ENDM
.data
userMsg_2 BYTE "Please enter an unsigned number: ",0
array DWORD LEN dup(?)
arLength DWORD LEN
sizeElem BYTE 11 ; Number of digits allowed in. reading ints as strings.
holder DWORD ? ; Temp memory location for storing string input/output.
.code
main PROC
push OFFSET array
push OFFSET arLength
push OFFSET sizeElem
push OFFSET holder
call ReadVal
exit
main ENDP
ReadVal PROC
push ebp
mov ebp, esp
push ecx
mov esi, [ebp+20] ; esi has starting address of array.
mov eax, [ebp+16] ; @arLength
mov ecx, [eax] ; ecx gets arLength.
cmp ecx, 0
je L2
L1:
getString [ebp+8], [ebp+12] ; [ebp+8] gets the user inputted string.
mov esi, [ebp+8] ; mov string in holder into array[esi].
add esi, TYPE DWORD
loop L1
L2:
pop ecx
pop ebp
ret 16
ReadVal ENDP
END main
我认为问题在于 ReadString 期望 ECX 中的值是 DWORD。给它一个数字作为 BYTE 会混淆它。将 sizeElem 声明为 DWORD 可以解决问题。
您可能也希望将 holder 声明为使用 BYTE 而不是 DWORD,我认为这是更典型的字符串存储。