汇编:索引的问题

问题描述 投票:0回答:1

我无法弄清楚如何在循环中进行索引编制。我知道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

任何建议都非常感谢!

arrays assembly indexing x86
1个回答
1
投票

你有几个问题。一个是你似乎不明白过程/函数的USES指令是什么。如果你使用USES并列出一个寄存器,那么告诉汇编器将这些寄存器的值保存在堆栈中,并在函数退出之前恢复它们。这意味着调用它的函数不会看到您对该函数中的该寄存器所做的任何更改。

MASM手册说明了这个用法:

语法:USES已登记

描述:

 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。而不是使用你应该做的事情的长度只是使用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
© www.soinside.com 2019 - 2024. All rights reserved.