Author Topic: Read the data from a file and print the content of it  (Read 183 times)

ToanBao122

  • Regular Member
  • *
  • Posts: 8
Read the data from a file and print the content of it
« on: October 17, 2020, 05:01:33 PM »
Hello everyone, say if I'm to have a "text.txt" file that contains a simple string "Hello,world". Can you show me a program to read this file and then print out its contents? This is what I was trying to do albeit unsuccessfully. Thank you!
Code: [Select]
INCLUDE Irvine32.inc
BUFMAX = 128
.data
buffer   BYTE   BUFMAX+1 DUP(0)
bufSize  DWORD  ?
fileName BYTE "textt.txt",0
fileHandle HANDLE ?
bytesRead dword ?
.code
main PROC
mov  EDX,OFFSET fileName
call OpenInputFile
mov  fileHandle, EAX
mov  eax,fileHandle
mov  edx,OFFSET buffer
mov  ecx,BUFMAX
call ReadFromFile
jc   show_error_message
mov  bytesRead,eax
mov  edx, offset bytesRead
call WriteString
exit
main ENDP

« Last Edit: October 17, 2020, 06:44:02 PM by ToanBao122 »

jj2007

  • Member
  • *****
  • Posts: 10678
  • Assembler is fun ;-)
    • MasmBasic
Re: Read the data from a file and print the content of it
« Reply #1 on: October 17, 2020, 06:54:57 PM »
I don't know much about Irvine's strange library (nobody uses it here), but if you hand over OFFSET buffer to ReadFile, could it be that WriteString might also want to be informed about this buffer?

Check also what Irvine offers for debugging registers. It might happen that your ReadFile fails miserably because it doesn't find textt.txt on your disk.

ToanBao122

  • Regular Member
  • *
  • Posts: 8
Re: Read the data from a file and print the content of it
« Reply #2 on: October 17, 2020, 07:16:30 PM »
The "textt.txt" was a minor spelling mistake on my part, however, it wasn't exactly the case. Can you elaborate on this part :" if you hand over OFFSET buffer to ReadFile, could it be that WriteString might also want to be informed about this buffer?"  Sorry, I'm new to MASM in general.

jj2007

  • Member
  • *****
  • Posts: 10678
  • Assembler is fun ;-)
    • MasmBasic
Re: Read the data from a file and print the content of it
« Reply #3 on: October 17, 2020, 10:14:24 PM »
What does the irvine manual say about WriteString?

Vortex

  • Member
  • *****
  • Posts: 2385
Re: Read the data from a file and print the content of it
« Reply #4 on: October 17, 2020, 11:17:56 PM »
Hi ToanBao122,

This is not an example based on Irvine's include files but it can give you some ideas :

Code: [Select]
include     ReadFile.inc

.data

FileName    db 'test.txt',0     ; file to read

.data?

hFile       dd ?
FileSize    dd ?
hMem        dd ?
BytesRead   dd ?

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC   uses ebx

    xor     ebx,ebx
   
    invoke  CreateFile,ADDR FileName,GENERIC_READ,ebx,ebx,\
            OPEN_EXISTING,ebx,ebx           

    mov     hFile,eax

    invoke  GetFileSize,eax,ebx

    mov     FileSize,eax
    inc     eax

    invoke  GlobalAlloc,GMEM_FIXED,eax
    mov     hMem,eax

    add     eax,FileSize

    mov     BYTE PTR [eax],bl  ; Set the last byte to NULL so that StdOut
                               ; can safely display the text in memory.

    invoke  ReadFile,hFile,hMem,FileSize,\
            ADDR BytesRead,ebx

    invoke  CloseHandle,hFile

    invoke  StdOut,hMem

    invoke  GlobalFree,hMem
    ret

main ENDP


END start

ToanBao122

  • Regular Member
  • *
  • Posts: 8
Re: Read the data from a file and print the content of it
« Reply #5 on: October 18, 2020, 06:07:37 AM »
@jj2007 It says the WriteString function takes in EDX as a string pointer as argument.

ToanBao122

  • Regular Member
  • *
  • Posts: 8
Re: Read the data from a file and print the content of it
« Reply #6 on: October 18, 2020, 06:51:17 AM »
Anyway thanks everyone for the inpute, I've figureed out the problem is that the content of the string is not in the bytesRead but in the buffer, and in the line before call WriteString, I should set "mov edx, offset buffer" instead of "mov edx, offset bytesRead". Have a good day everyone!

jj2007

  • Member
  • *****
  • Posts: 10678
  • Assembler is fun ;-)
    • MasmBasic
Re: Read the data from a file and print the content of it
« Reply #7 on: October 18, 2020, 08:24:05 AM »
Yep, that was indeed the solution, congrats :thumbsup:

ToanBao122

  • Regular Member
  • *
  • Posts: 8
Re: Read the data from a file and print the content of it
« Reply #8 on: October 20, 2020, 07:18:50 AM »
Anyone know of a way that I could get the length of the string in the buffer?

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7642
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Read the data from a file and print the content of it
« Reply #9 on: October 20, 2020, 08:56:34 AM »
Pass the ADDRESS of the string to the following procedure. Return value is in the EAX register.

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

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

slen proc ptxt:DWORD

    mov eax, [esp+4]
    sub eax, 1

  lbl0:
    add eax, 1
    cmp BYTE PTR [eax], 0
    jne lbl0

    sub eax, [esp+4]

    ret 4

slen endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Vortex

  • Member
  • *****
  • Posts: 2385
Re: Read the data from a file and print the content of it
« Reply #10 on: October 20, 2020, 09:29:23 AM »
Hi ToanBao122,

You also have the API function lstrlen

Quote
Determines the length of the specified string (not including the terminating null character).

https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-lstrlena