我將字符串用戶輸入轉換爲整數並將所述整數存儲在數組中。我可以在添加之前打印整數,並從我的ReadVal過程中的數組中打印整數,並且數字與鍵盤輸入匹配。MASM訪問並添加到數組的第一個元素
但是,當我嘗試在main中打印數組的第一個元素時,我得到了一個非常不同的數字。第二,第三和第四次打印準確。當我從第二個元素開始存儲整數時,我沒有任何問題,並且所有內容都被正確存儲。在程序完成之後,什麼會改變第一個元素?
我如何打印的第一個元素在主:
mov esi, OFFSET array
mov eax, [esi]
call WriteDec
Printing the second:
mov esi, OFFSET array
mov eax, [esi + 4]
call WriteDec
萬一有幫助,這是我在串讀整數,並將其轉換爲數字代碼:
readVal PROC
push ebp
mov ebp, esp
mov edi, [ebp + 24] ; @array
getString:
xor edx, edx
mov eax, edi ; Address of current element in array
sub eax, [ebp + 24] ; subtract the starting address from the current
cmp eax, 40 ; If 10 DWORDS have been added to the array, jump to finish
je readValFinish
mGetString [ebp + 8] ; store value in @dString
; Convert the string to numeric value
mov esi, [ebp + 8] ; @dString
mov ecx, eax ; length of the string
;Set X to 0
mov ebx, 0
cld ; Set direction to forward
convertNext:
lodsb ; Gets the char in AL
cmp al, 48
jl outOfRange
cmp al, 57
jg outOfRange
jmp goodChar
outOfRange:
mov edx, [ebp + 16]
call WriteString
call Crlf
;mov eax, 0 ;Signals they guessed an invalid number
jmp getString
; If it's a valid character, add it to the total
goodChar:
; subtract 48
sub al, 48
push eax
; multiply x by 10
mov eax, ebx
;call WriteDec
mov ebx, 10
mul ebx
mov ebx, eax ; x back in ebx
pop eax
add ebx, eax
loop convertNext
call Crlf
; If valid, add numeric value (EBX) to an array
mov eax, ebx
call WriteDec
mov [edi], eax
mov eax, [edi]
call WriteDec
; xor eax, eax
; mov eax, [edi]
; call WriteDec
add edi, 4 ;Move to the next position
jmp getString
readValFinish:
pop ebp
ret 20 ; 5 pushed on the stack
readVal ENDP
你能指定數組的內容嗎? – rcd
當然。該數組用無符號整數填充。 –