• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

0 terminated?

The following code outputs the first two letters of a file called test.txt.
test.txt has the letters abcd and nothing else in it.
When I display the ab with StdOut there is no error message but I have cause to wonder... shouldn't that string be null terminated?
Should I be manually placing a 0 at the end of it?

  .model flat, stdcall
  option casemap :none

  include \masm32\include\windows.inc
  include \masm32\macros\macros.asm

  include \masm32\include\masm32.inc
  include \masm32\include\gdi32.inc
  include \masm32\include\user32.inc
  include \masm32\include\kernel32.inc.

  includelib \masm32\lib\masm32.lib
  includelib \masm32\lib\gdi32.lib
  includelib \masm32\lib\user32.lib
  includelib \masm32\lib\kernel32.lib

    filename db "c:\masm32\assembly projects\fopen\test.txt",0    
    file_handle HANDLE ?
    file_buffer DWORD 256 dup (?)
    readC DW ?

  invoke CreateFile,ADDR filename,GENERIC_READ,FILE_SHARE_READ,
  mov file_handle,eax
  invoke ReadFile,file_handle,ADDR file_buffer,sizeof(BYTE)*2,ADDR readC,NULL
  invoke StdOut,ADDR file_buffer
  invoke CloseHandle,file_handle

end start

Open in new window

1 Solution
You read two bytes from the file, and place them in the buffer.

Nothing else is placed in the buffer, so it will contain those two bytes, and whatever happened to be in the buffer before the ReadFile call.

If you want it to be null terminated, then you have to add the '\0' byte yourself, as you correctly assume.
aiakonaiAuthor Commented:
Thanks. I'm still new to 32 bit assembly ... just making sure I know is what I know.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now