Author Topic: Wrong return from crt_scanf  (Read 90 times)

Elnee

  • Regular Member
  • *
  • Posts: 2
Wrong return from crt_scanf
« on: December 10, 2017, 01:17:41 AM »
Hello everyone! Please, I need help.
I use crt_scanf in loop but result always is incorrect.
What is going wrong?

Code:
Code: [Select]
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    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:

Code: [Select]
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.

jj2007

  • Member
  • *****
  • Posts: 7884
  • Assembler is fun ;-)
    • MasmBasic
Re: Wrong return from crt_scanf
« Reply #1 on: December 10, 2017, 02:52:16 AM »
Have a look at the manual and check how arrays are defined in assembler:
Code: [Select]
    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:

Elnee

  • Regular Member
  • *
  • Posts: 2
Re: Wrong return from crt_scanf
« Reply #2 on: December 10, 2017, 03:04:14 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.

jj2007

  • Member
  • *****
  • Posts: 7884
  • Assembler is fun ;-)
    • MasmBasic
Re: Wrong return from crt_scanf
« Reply #3 on: December 10, 2017, 03:11:09 AM »
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.