Hello everyone!
Please, I need help.
I use crt_scanf in loop but result always is incorrect.
What is going wrong?
Code:
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
.const
arrlength = 5
.data?
arr sword arrlength dup(?)
itr db ?
temp sword ?
.data
f1 db "%i"
.code
start:
cls
printf("Input %d numbers size word:\n", arrlength)
mov itr, 0
lea esi, arr
.while itr < arrlength
invoke crt_scanf, ADDR f1, esi
inc esi
inc itr
.endw
printf("MASS:\n%d\n", arr[0])
printf("%d\n", arr[1])
printf("%d\n", arr[2])
printf("%d\n", arr[3])
printf("%d\n", arr[4])
inkey
exit
end start
Result in CMD:
Input 5 numbers size word:
23
-23
234
-54
43
MASS:
-5865
-5399
-13590
11210
43
Press any key to continue ...
Only last input is correct.
Have a look at the manual and check how arrays are defined in assembler:
lea esi, arr
.while itr < arrlength
invoke crt_scanf, ADDR f1, esi
add esi, WORD ; move on to the next array element
inc itr
.endw
printf("MASS:\n%d\n", arr[0])
printf("%d\n", arr[1*WORD])
printf("%d\n", arr[2*WORD])
printf("%d\n", arr[3*WORD])
printf("%d\n", arr[4*WORD])
Welcome to the forum :icon14:
Quote from: jj2007 on December 10, 2017, 02:52:16 AM
Have a look at the manual and check how arrays are defined in assembler:
Wow! Now everything is working! Thank you very much :greenclp:
I do not know the theory so well. Sure, I'll look at the manual.
It's a simple point: arr[n] means element n in many languages, but not in assembler. You have to count the bytes manually, and if you have an array of WORDs, then your elements are in arr[0], arr[2], arr[4] etc.