arrfile$ macro causing my program to crash with files with odd numbers of lines

Started by bhy56, August 02, 2011, 06:06:32 PM

Previous topic - Next topic

bhy56

Hi I am a little confused about what I am doing wrong.

I have some code like this . . .

include \masm32\include\masm32rt.inc
.data
    m_inFile BYTE "test.txt",0
    m_inArray      DWORD   0       
.code
start:
    print chr$("Test output one.",13,10)
    mov m_inArray, arrfile$(addr m_inFile)
    print chr$("Test output two.",13,10)
    exit
end start


. . . and a file (test.txt) like this . . .


one b
two w
three w
four w
five b
six w
seven w
eight w
nine b
ten w
elven b
twelve w
thirteen b
fourteen w
fifteen b
sixteen w
seventeen b
eighteen w


. . . if I end my file on one of the lines that ends with a w it works fine. If I end it one of the lines that ends with a b it does not work at all. It seems like the line number is what affects if it works or not. Any idea how I could get it to work for files with any number of lines.

I am using masm32 on windows 7. I thought it said I had downloaded version 10 but when I look at the editor it says  "quick editor 4".

Thanks for your help.


hutch--

Try this, you must allocate the array first. Check the reference material in the HLHELP help file.



IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                      Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL array :DWORD
    LOCAL rval  :DWORD
    LOCAL ptxt  :DWORD

    mov array, arralloc$(64)            ; allocate the array

    test arrset$(array,1,"item 1"), 0   ; load text into array
    test arrset$(array,2,"item 2"), 0
    test arrset$(array,3,"item 3"), 0
    test arrset$(array,4,"item 4"), 0
    test arrset$(array,5,"item 5"), 0
    test arrset$(array,6,"item 6"), 0
    test arrset$(array,7,"item 7"), 0
    test arrset$(array,8,"item 8"), 0


    mov ptxt, arrget$(array,8)          ; display results in reverse order
    print ptxt,13,10
    mov ptxt, arrget$(array,7)
    print ptxt,13,10
    mov ptxt, arrget$(array,6)
    print ptxt,13,10
    mov ptxt, arrget$(array,5)
    print ptxt,13,10
    mov ptxt, arrget$(array,4)
    print ptxt,13,10
    mov ptxt, arrget$(array,3)
    print ptxt,13,10
    mov ptxt, arrget$(array,2)
    print ptxt,13,10
    mov ptxt, arrget$(array,1)
    print ptxt,13,10

    mov rval, arrfree$(array)           ; free the array memory

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

ToutEnMasm


jj2007


bhy56

Quote from: hutch-- on August 03, 2011, 08:36:50 AM
Try this, you must allocate the array first. Check the reference material in the HLHELP help file.


The help file says "The array is created by this function and the return value is the handle of the array." So it seems like I do not need to allocate it first, but I will try it out. The code sample you showed is not using the arrfile macro. Thanks for your help.

dedndave

there is an example that uses arrfile$ in \masm32\examples\exampl10\dynarray\loadfile

the macro is defined in \masm32\macros\macros.asm
    arrfile$ MACRO file_name            ;; load multiline text file into array
      EXITM <rv(arrfile,reparg(file_name))>
    ENDM


the arrfile function is defined in \masm32\m32lib\arrfile.asm
arrfile proc file_name:DWORD

    LOCAL arr   :DWORD
    LOCAL hMem  :DWORD
    LOCAL flen  :DWORD
    LOCAL lcnt  :DWORD
    LOCAL pbuf  :DWORD
    LOCAL spos  :DWORD
    LOCAL void  :DWORD

    push ebx
    mov hMem, InputFile(file_name)
    mov flen, ecx
    mov lcnt, rv(get_line_count,hMem,flen)
    mov arr, arralloc$(lcnt)
    mov pbuf, alloc(flen)

    mov spos, 0
    mov ebx, 1
  @@:
    mov spos, rv(readline,hMem,pbuf,spos)
    mov void, arrset$(arr,ebx,pbuf)
    add ebx, 1
    cmp ebx, lcnt
    jle @B

    free pbuf
    free hMem

    mov eax, arr
    pop ebx

    ret

arrfile endp


as you can see, the arrfile function calls arrset$
but it looks like you still need to use arrget$   :P

hutch--

bhy56,

I did write you the example for a reason, trying to re-interpret the help file yielded the results you already had that did not work. There is an overview in the help file that tells you how it works, one array of pointers in fixed memory which holds pointers to either separate allocated memory for each member or a place holder for an empty member.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

the example in \masm32\examples\exampl10\dynarray\loadfile makes it look pretty easy   :P

bhy56

Quote from: hutch-- on August 04, 2011, 01:30:48 AM
bhy56,

I did write you the example for a reason, trying to re-interpret the help file yielded the results you already had that did not work. There is an overview in the help file that tells you how it works, one array of pointers in fixed memory which holds pointers to either separate allocated memory for each member or a place holder for an empty member.

I still do not understand what I need to do to make my code open all files. It opens files with a even number of lines and I can work with them and change the strings and print them out. But it crashes when opening a file with a odd number of lines, and does not even get out of the arrfile macro. I tried to allocate the array first with a line like this: "mov hArray, arralloc$(4096)", but it did not help. Sorry I am really new at this. Thanks for your help.

bhy56

Quote from: dedndave on August 04, 2011, 01:36:34 AM
the example in \masm32\examples\exampl10\dynarray\loadfile makes it look pretty easy   :P

It does make it look easy, but I do not see what they are doing that I need to do in order to make it work. I have not yet figured out what GetTickCount does. Do you know where it is defined? Thanks for pointing out this example.


dedndave

the main points of interest in that demo....
arrfile$("\masm32\include\windows.inc")  ;returns a handle
arrcnt$(hArr)                            ;returns element count
arrget$(hArr,ebx)                        ;returns an element address (ebx = number)
arrfree$(hArr)                           ;frees the allocation

bhy56

Quote from: dedndave on August 04, 2011, 04:25:52 AM
GetTickCount
http://msdn.microsoft.com/en-us/library/ms724408%28v=vs.85%29.aspx

they are using it for demo purposes to show how long it takes   :bg

If I use the example (\masm32\examples\exampl10\dynarray\loadfile) to load my file it brakes with a odd number of lines also, just like my code above does. I loaded masm32 on another computer and with both the sample code and the code I posted above, it does not work with a odd number lines in the file.

dedndave

it may be you have found a real bug   :U
i'll play with it tomorrow

it looks as though it searches for carriage return/line feed pairs
is the last line terminated with CR/LF ?

bhy56

Quote from: dedndave on August 04, 2011, 04:52:36 AM
it may be you have found a real bug   :U
i'll play with it tomorrow

it looks as though it searches for carriage return/line feed pairs
is the last line terminated with CR/LF ?

I think I tested for that, and if I remember right it does not seem to make any difference if there is a CR/LF at the end of the file.