我无法弄清楚如何在循环中进行索引编制。我知道esi
用于编制索引,所以我试图使用它...
scores DWORD MAXIMUMSCORES DUP(0)
optionPromptMsg byte "Type 1 to continue or -1 to exit: ", 0
scorePromptMsg byte "Enter your numeric score (0-100): ", 0
scoreErrorMsg byte "Score out of range (0-100)!", 0
optionErrorMsg byte "Only 0 or 1 allowed in option specification!", 0
resultMsg byte " scores have been entered.", 0
.code
main proc
mov esi, 0
L1:
mov edx, offset optionPromptMsg
call WriteString
call ReadInt
mov ebx, 1
cmp eax, ebx
je L2
mov ebx, -1 //99% sure my main is okay
cmp eax, ebx
je L3
mov ebx, -2
mov ecx, 2
cmp ebx, eax
ja L4
cmp eax, ecx
ja L4
L2: call NextScore
jmp L5
L4: mov edx, offset optionErrorMsg
call WriteString
call Crlf
jmp L5
L5:
loop L1
L3 : call WriteScore
exit
main ENDP
WriteScore PROC USES esi //Thought was somehow make esi global?
mov eax, lengthof scores //total number of items added to array
call writeInt
mov edx, offset resultMsg
call WriteString
mov esi,0
L1:
mov eax, scores[esi *4]
call writeInt //writes the numbers in the array
inc esi
loop L1
mov eax, 5000
call Delay
ret
WriteScore ENDP
NextScore PROC USES esi
mov edx, offset scorePromptMsg
call WriteString
call ReadInt
mov ebx, 0
mov ecx, 100
cmp ebx, eax
ja L1
cmp eax,ecx
ja L1
jmp L2
L1:
mov edx, offset scoreErrorMsg
call WriteString
call Crlf
L2:
mov scores[esi*4], eax //gets the next number and puts it in the array
inc esi
ret
NextScore ENDP
当我运行它并向数组中添加3个项目时,无论出于何种原因,它都表示scores
的长度为20,然后当它打印出数组时,数字甚至不接近我期待的东西,通常只有数百万或0
。
非常感谢任何建议!
答案 0 :(得分:1)
你有几个问题。一个是你似乎不明白程序/功能的USES
指令是什么。如果使用USES
并列出一个寄存器,则告诉汇编器将这些寄存器的值保存在堆栈中,并在函数退出之前恢复它们。这意味着调用它的函数不会看到您对该函数中的该寄存器所做的任何更改。
MASM手册说明了 USES :
语法:USES reglist
说明
An optional keyword used with PROC. Generates code to push the value of registers that should be preserved (and that will be altered by your procedure) on the stack and pop them off when the procedure returns. The <reglist> parameter is a list of one or more registers. Separate multiple registers with spaces.
由于您似乎希望对函数 NextScore 中的 ESI 所做的更改被调用函数看到,您将要删除 USES 该程序的陈述。变化:
NextScore PROC USES esi
为:
NextScore PROC
现在,当您在下一个分数中增加 ESI 时,当函数退出时,它将无法撤消。
另一个问题是lengthof伪操作码:
lengthof:返回数组变量中的项数。
可能不太清楚但是这个伪操作码是组装代码时数组中元素的数量。您可以像这样定义分数数组:
scores DWORD MAXIMUMSCORES DUP(0)
得分数组的长度值 MAXIMUMSCORES 。而不是使用 lengthof 您应该做的只是使用 ESI 寄存器。您已使用 ESI 来保留已添加到阵列的元素数。所以这段代码:
WriteScore PROC USES esi ; Thought was somehow make esi global?
mov eax, lengthof scores ; total number of items added to array
call WriteInt
可以更改为:
WriteScore PROC USES esi ; Thought was somehow make esi global?
mov eax, esi ; esi = number of items added to array
call WriteInt
另一个问题是,您似乎不知道loop
指令的工作原理。从[x86指令集] loop
指令执行:
使用ECX或CX寄存器作为计数器执行循环操作。 每次执行LOOP指令时,计数寄存器都是 递减,然后检查0.如果计数为0,则循环为 终止,程序继续执行指令 遵循LOOP指令。如果计数不为零,则接近跳跃 执行到目的地(目标)操作数,这可能是 循环开始时的指令。
在您的代码中,您从未将 ECX 设置为您希望循环的次数,因此它将使用 ECX 中出现的任何值。这就是你打印出大量额外数字的原因。 ECX 需要初始化。由于您想要遍历输入的所有分数,只需将 ESI 移动到 ECX 即可。您的WriteScore功能确实:
mov esi,0
L1:
mov eax, scores[esi *4]
call WriteInt ; writes the numbers in the array
inc esi
loop L1
我们可以将其修改为:
mov ecx,esi ; Initialize ECX loop counter to number of scores added
; to the array.
mov esi,0
L1:
mov eax, scores[esi *4]
call WriteInt ; writes the numbers in the array
inc esi
loop L1
现在我们只循环用户实际输入的分数( ESI )。
考虑到这些变化,您的程序可能如下所示:
INCLUDE Irvine32.inc
INCLUDELIB Irvine32.lib
INCLUDELIB user32.lib
INCLUDELIB kernel32.lib
MAXIMUMSCORES equ 20
.data
scores DWORD MAXIMUMSCORES DUP(0)
optionPromptMsg byte "Type 1 to continue or -1 to exit: ", 0
scorePromptMsg byte "Enter your numeric score (0-100): ", 0
scoreErrorMsg byte "Score out of range (0-100)!", 0
optionErrorMsg byte "Only 0 or 1 allowed in option specification!", 0
resultMsg byte " scores have been entered.", 0
.code
main proc
mov esi, 0
L1:
mov edx, offset optionPromptMsg
call WriteString
call ReadInt
mov ebx, 1
cmp eax, ebx
je L2
mov ebx, -1 ; 99% sure my main is okay
cmp eax, ebx
je L3
mov ebx, -2
mov ecx, 2
cmp ebx, eax
ja L4
cmp eax, ecx
ja L4
L2: call NextScore
jmp L5
L4: mov edx, offset optionErrorMsg
call WriteString
call Crlf
jmp L5
L5:
loop L1
L3: call WriteScore
exit
main ENDP
WriteScore PROC USES esi ; We make changes to ESI not visible to caller
; since we don't intend to change the number of scores
; with this function. Any change to ESI in this function
; will not appear to the caller of this function
mov eax, esi ; total number of items added to array in ESI
call WriteInt
mov edx, offset resultMsg
call WriteString
mov ecx,esi
mov esi,0
L1:
mov eax, scores[esi *4]
call WriteInt ; writes the numbers in the array
inc esi
loop L1
mov eax, 5000
call Delay
ret
WriteScore ENDP
NextScore PROC ; We want changes to ESI to exist after we exit this function. ESI
; will effectively act as a global register.
mov edx, offset scorePromptMsg
call WriteString
call ReadInt
mov ebx, 0
mov ecx, 100
cmp ebx, eax
ja L1
cmp eax,ecx
ja L1
jmp L2
L1:
mov edx, offset scoreErrorMsg
call WriteString
call Crlf
L2:
mov scores[esi*4], eax ; gets the next number and puts it in the array
inc esi
ret
NextScore ENDP
END
此示例输出:
Type 1 to continue or -1 to exit: 1
Enter your numeric score (0-100): 10
Type 1 to continue or -1 to exit: 1
Enter your numeric score (0-100): 20
Type 1 to continue or -1 to exit: 1
Enter your numeric score (0-100): 40
Type 1 to continue or -1 to exit: 1
Enter your numeric score (0-100): 650
Score out of range (0-100)!
Type 1 to continue or -1 to exit: 99
Only 0 or 1 allowed in option specification!
Type 1 to continue or -1 to exit: 1
Enter your numeric score (0-100): 100
Type 1 to continue or -1 to exit: -1
+5 scores have been entered.+10+20+40+650+100