News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Print one byte.

Started by AiaKonai, June 30, 2011, 06:10:05 PM

Previous topic - Next topic

AiaKonai

Hello,
I am trying to understand how to print out just the first byte of a file.
The file looks like this
test<carriage return><line feed><end of file>
I have managed to read in the contents of that file into a variable, but am at a loss as to how to just print off that first byte?
Eventually I would like to be able to print off that first byte as hex.

Here is the code I have so far.
  include \masm32\include\masm32rt.inc

  .data
    filename  BYTE "test.txt",0
    file_sz   DWORD ?
    error_msg BYTE "Failed to open file.",0dh,0ah,0
    msg1      BYTE "Before opening a file.",0dh,0ah,0
    msg2      BYTE "After opening a file.",0dh,0ah,0
    handle    DWORD ?
    count     DWORD ?
    Mem       DWORD ?
  .code

start:
  call main
  INVOKE ExitProcess,0

main proc
  INVOKE StdOut,ADDR msg1
  INVOKE CreateFile,                     ;Open test.txt.
         ADDR filename,
         GENERIC_READ or GENERIC_WRITE,
         NULL,
         NULL,
         OPEN_EXISTING,
         FILE_ATTRIBUTE_NORMAL,
         NULL
  mov handle,eax                          ;Store file handle.
  cmp eax,INVALID_HANDLE_VALUE
  je bad_file
  INVOKE StdOut,ADDR msg2
  INVOKE GetFileSize,handle,NULL
  mov file_sz,eax
  INVOKE SysAllocStringByteLen,0,file_sz
  mov Mem, eax
  INVOKE ReadFile,
         handle,
         Mem,                             ;This is where the file contents go.
         file_sz,
         ADDR count,
         NULL
  print str$(count)," bytes were read.",0dh,0ah
  INVOKE CloseHandle,handle              ;Close test.txt.
main_end:
  ret
;ERROR MESSAGES
bad_file:
  INVOKE StdOut,ADDR error_msg
  jmp main_end
main endp

end start


I have code to print out a byte as hex, but I wrote it in 16 bit assembly.
If someone could help me understand how to print off just one byte to the console, I think I can figure out how to do the rest.

jj2007

If you have no aversion against macros, try this:

  INVOKE CloseHandle,handle              ;Close test.txt.
  mov edx, Mem
  movzx eax, byte ptr [edx]
  print hex$(eax), " is byte #1", 13, 10
main_end:



AiaKonai

That's it! ...thanks. I'm still pretty new to 32 bit assembly. That helps a ton.