flat assembler
Message board for the users of flat assembler.

Index > Windows > Files.!

Author
Thread Post new topic Reply to topic
Betheny



Joined: 28 Dec 2006
Posts: 4
Location: Canberra
Betheny 01 Jan 2007, 12:45
Hey guys ....
This thg i have written in here opens a file and converts all the capital letters into low and the vice versa. it works but i want this program in MASM ...and i have not worked with that, anyone here does have the equivalent of this program in MASM please send it to my Email or post it in here. ThanX alot.... Wink

Sorry for not attaching the file but welll........ Confused

Code:
**************************************

;// EXE Format
;// PE_SIG: OBTTFS-36454
format PE GUI 4.0
include 'C:\FASM\Include\Win32ax.inc'

;// Data section
section '.data' data readable writable

       ;// Initializations for OPENFILENAME Structure
       ofn_szOutFile db 256 dup(?)
       ofn_szFilter  db 'All Files (*.*)',0,'*.*',0,0

       s_OFN OPENFILENAME 0,0,0,ofn_szFilter,0,0,0,ofn_szOutFile,0100h,0,0,0,0,OFN_FILEMUSTEXIST + OFN_PATHMUSTEXIST, \
                          0,0,0,0,0,0

       ;// s_ErrorMessage used by GET_LAST_ERROR_PROC
       s_ErrorMessage db 256 dup(?)

       ;// Allocate a dwWritten
       dwWritten dd 0


;// Code section
section '.code' code readable executable

    ;// MAIN CODE
    proc main

        ;// File Handle Holder
        local hFileHandle:DWORD

        ;// Initialize OFN Struct
        mov  [s_OFN.lStructSize],sizeof.OPENFILENAME
        invoke  GetOpenFileName,s_OFN
        cmp  eax,0 ;// Zero is returned when user closes the dialog of presses cancel
        je   .ExitProcess_JUMP

        ;// We have the file, open it and convert
        invoke  CreateFile,ofn_szOutFile,GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ, \
                           0,OPEN_EXISTING,0,0

        cmp     eax, INVALID_HANDLE_VALUE
        jne     @F ;// Jump to the next @@ label

        ;// We have a wrong handle, Tell the user, then exit
        call    GET_LAST_ERROR_PROC
        invoke  ExitProcess,0

        ;//------------------------------- Handle valid, continue
        @@:
        mov     [hFileHandle],eax

        ;// Get File Size
        local iFileSize:DWORD
        invoke  GetFileSize,[hFileHandle],0
        mov     [iFileSize],eax
        cmp     [iFileSize],0
        je      .InvalidFileSize
        cmp     [iFileSize],40*1024*1024 ;// Set our limit to 40MB, since usuall RAM's these day are alteast 128MB
                                         ;// And, our paging is also active
        jg      .FileTooBig
        jmp     .StartConversion

        ;// Invalid File Size, File too big,
        .InvalidFileSize:
        invoke  MessageBox,0,'File is empty, nothing to convert.','Abort',16
        invoke  CloseHandle,[hFileHandle]
        invoke  ExitProcess,0

        .FileTooBig:
        invoke  MessageBox,0,'File is too big, please specify a smaller file.','Abort',16
        invoke  CloseHandle,[hFileHandle]
        invoke  ExitProcess,0

        ;// FileSize valid, start conversion
        .StartConversion:

               local   i:DWORD, hHeap: DWORD, dLastError: DWORD, pData: DWORD ;// I is out counter

               ;// Allocate pData equal to file size
               invoke  GetProcessHeap
               mov     [hHeap],eax
               cmp     eax,0
               jne     @f

                        ;// Error
                        invoke  MessageBox,0,'Unknow error occured.','Abort',0
                        invoke  CloseHandle,[hFileHandle]
                        invoke  ExitProcess,0

               ;// We have the HEAP
               @@:
               invoke  HeapAlloc,[hHeap],0,[iFileSize]
               cmp     eax,0
               jne     @f

                        ;// Memory allocation failed
                        invoke  MessageBox,0,'Unable to allocate memory.','Low Resource',16
                        invoke  CloseHandle,[hFileHandle]
                        invoke  ExitProcess,0

               ;// We have the memory
               @@:
               mov      [pData],eax

               ;// Read the file
               invoke   ReadFile,[hFileHandle],[pData],[iFileSize],dwWritten,0 ;// Note that dwWritten is not used in this function
               cmp      eax,0
               jne      @f

                        call     GET_LAST_ERROR_PROC
                        invoke   HeapFree,[hHeap],0,[pData]
                        invoke   CloseHandle,[hFileHandle]
                        invoke   ExitProcess,0

               ;Continue, file read successful
               @@:
               mov      [i],0
               mov      ebx,[pData]
               ;// Start of Loop
               .ConversionLoop:
                        ;// Check i
                        mov eax,[i]
                        cmp eax,[iFileSize]
                        jge .ExitLoop

                        ;// Check for case
                        mov al,byte [ebx]
                        mov ah,0

                        .if (al <= 'Z') & (al >= 'A')
                            add  al,'a'-'A'
                            mov  [ebx],al
                        .elseif (al <= 'z') & (al >= 'a')
                            sub  al,'a'-'A'
                            mov  [ebx],al
                        .endif

                        ;// Increase i and repeat
                        inc ebx
                        inc [i]
                        jmp .ConversionLoop

                ;// End of Loop
                .ExitLoop:
                ;// Write to file
                invoke  SetFilePointer,[hFileHandle],0,0,FILE_BEGIN
                invoke  WriteFile,[hFileHandle],[pData],[iFileSize],dwWritten,0 ;// Note that dwWritten is not used in this function
                cmp     eax,0
                jne     @f

                        ;// File Write Failed
                        call     GET_LAST_ERROR_PROC
                        invoke   HeapFree,[hHeap],0,[pData]
                        invoke   CloseHandle,[hFileHandle]
                        invoke   ExitProcess,0

                ;// End of our process
                @@:
                invoke  MessageBox,0,'File conversion completed successfuly','Complete',64

                ;// Release Memory
                invoke   HeapFree,[hHeap],0,[pData]
        ;//------------------------------- Close the file
        invoke  CloseHandle,[hFileHandle]


        ;// Terminate APP
        .ExitProcess_JUMP:

        invoke ExitProcess,0

    endp

    ;// GetLastError Procedures finds the failure reason in system-level
    proc GET_LAST_ERROR_PROC
       invoke   GetLastError
       invoke   FormatMessage,FORMAT_MESSAGE_FROM_SYSTEM,0,eax,0,s_ErrorMessage,256,0
       invoke   MessageBox,0,s_ErrorMessage,'Error',16
    endp


;// End of program
.end main
****************************************    

edited by moderator(scientica): please use [ code ] -tags or better yet attach a file when posting big pices of code.

_________________
SAPA . YAYA
Post 01 Jan 2007, 12:45
View user's profile Send private message Yahoo Messenger MSN Messenger Reply with quote
AsmER



Joined: 25 Mar 2006
Posts: 64
Location: England
AsmER 01 Jan 2007, 21:20
Here's the converted source file:

Code:
.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\comdlg32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\comdlg32.lib

;// Data section
.data

       ;// Initialized data    
       ofn_szFilter             db      'All Files (*.*)',0,'*.*',0,0
       FILE_EMPTY_MSG   db      'File is empty, nothing to convert.',0
       FILE_TOO_BIG_MSG db      'File is too big, please specify a smaller file.',0
       UNKNOWN_ERR_MSG  db      'Unknow error occured.',0
       MEM_ERR_MSG              db      'Unable to allocate memory.',0
       RES_ERR_MSG              db      'Low Resource',0
       SUCCESS_MSG              db      'File conversion completed successfuly',0
       COMPLETED_MSG    db      'Complete',0
       ABORT_MSG                db      'Abort',0
       ERROR_MSG                db      'Error',0
       dwWritten                dd      0

       s_OFN OPENFILENAME <0,0,0,ofn_szFilter,0,0,0,ofn_szOutFile,0100h,0,0,0,0,OFN_FILEMUSTEXIST + OFN_PATHMUSTEXIST, 0,0,0,0,0,0>

       ;// Uninitialized memory after initialized data (makes output smaller)
       s_ErrorMessage   db 256 dup(?)
       ofn_szOutFile    db 256 dup(?)
       


;// Code section
.code
Start:
    ;// MAIN CODE
    main proc

        ;// File Handle Holder
        LOCAL hFileHandle:DWORD
        ;// Get File Size
        LOCAL iFileSize:DWORD
        LOCAL   i:DWORD
        LOCAL   hHeap:DWORD
        LOCAL   dLastError:DWORD
        LOCAL   pData: DWORD ;// I is out counter

        ;// Initialize OFN Struct
        mov  s_OFN.lStructSize, sizeof OPENFILENAME
        invoke  GetOpenFileName, ADDR s_OFN
        cmp  eax,0 ;// Zero is returned when user closes the dialog of presses cancel
        je   ExitProcess_JUMP

        ;// We have the file, open it and convert
        invoke  CreateFile, ADDR ofn_szOutFile,GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ, \
                           0,OPEN_EXISTING,0,0

        cmp     eax, INVALID_HANDLE_VALUE
        jne     @F ;// Jump to the next @@ label

        ;// We have a wrong handle, Tell the user, then exit
        call    GET_LAST_ERROR_PROC
        invoke  ExitProcess,0

        ;//------------------------------- Handle valid, continue
        @@:
        mov     hFileHandle,eax


        invoke  GetFileSize,hFileHandle,0
        mov     iFileSize,eax
        cmp     iFileSize,0
        je      InvalidFileSize
        cmp     iFileSize,40*1024*1024 ;// Set our limit to 40MB, since usuall RAM's these day are alteast 128MB
                                         ;// And, our paging is also active
        jg      FileTooBig
        jmp     StartConversion

        ;// Invalid File Size, File too big,
        InvalidFileSize:
        invoke  MessageBox,0, ADDR FILE_EMPTY_MSG,ADDR ABORT_MSG,16
        invoke  CloseHandle, hFileHandle
        invoke  ExitProcess,0

        FileTooBig:
        invoke  MessageBox,0,ADDR FILE_TOO_BIG_MSG,ADDR ABORT_MSG,16
        invoke  CloseHandle, hFileHandle
        invoke  ExitProcess,0

        ;// FileSize valid, start conversion
        StartConversion:

               ;// Allocate pData equal to file size
               invoke  GetProcessHeap
               mov     hHeap ,eax
               cmp     eax,0
               jne     @f

                        ;// Error
                        invoke  MessageBox,0,ADDR UNKNOWN_ERR_MSG,ADDR ABORT_MSG,0
                        invoke  CloseHandle, hFileHandle
                        invoke  ExitProcess,0

               ;// We have the HEAP
               @@:
               invoke  HeapAlloc,hHeap,0,iFileSize
               cmp     eax,0
               jne     @f

                        ;// Memory allocation failed
                        invoke  MessageBox,0,ADDR MEM_ERR_MSG,ADDR RES_ERR_MSG,16
                        invoke  CloseHandle,hFileHandle
                        invoke  ExitProcess,0

               ;// We have the memory
               @@:
               mov      pData,eax

               ;// Read the file
               invoke   ReadFile,hFileHandle,pData,iFileSize, ADDR dwWritten,0 ;// Note that dwWritten is not used in this function
               cmp      eax,0
               jne      @f

                        call     GET_LAST_ERROR_PROC
                        invoke   HeapFree, hHeap,0,pData
                        invoke   CloseHandle,hFileHandle
                        invoke   ExitProcess,0

               ;Continue, file read successful
               @@:
               mov      i,0
               mov      ebx,pData
               ;// Start of Loop
               ConversionLoop:
                        ;// Check i
                        mov eax,i
                        cmp eax,iFileSize
                        jge ExitLoop

                        ;// Check for case
                        mov al,byte ptr [ebx]
                        mov ah,0

                        cmp     al, 'Z'
                        ja      @f
                        cmp     al, 'A'
                        jb      @f
                            add  al,0x20
                            mov  byte [ebx],al
                            jmp  @END
                        @@:    
                        cmp     al, 'z'
                        ja      .@END
                        cmp     al, 'a'
                        jb      @END
                            sub  al,0x20
                            mov  byte  [ebx],al
                        @END:

                        ;// Increase i and repeat
                        inc ebx
                        inc i
                        jmp ConversionLoop

                ;// End of Loop
                ExitLoop:
                ;// Write to file
                invoke  SetFilePointer,hFileHandle,0,0,FILE_BEGIN
                invoke  WriteFile,hFileHandle,pData,iFileSize, ADDR dwWritten,0 ;// Note that dwWritten is not used in this function
                cmp     eax,0
                jne     @f

                        ;// File Write Failed
                        call     GET_LAST_ERROR_PROC
                        invoke   HeapFree,hHeap,0,pData
                        invoke   CloseHandle,hFileHandle
                        invoke   ExitProcess,0

                ;// End of our process
                @@:
                invoke  MessageBox,0,ADDR SUCCESS_MSG,ADDR COMPLETED_MSG,64

                ;// Release Memory
                invoke   HeapFree,hHeap,0,pData
        ;//------------------------------- Close the file
        invoke  CloseHandle,hFileHandle


        ;// Terminate APP
        ExitProcess_JUMP:

        invoke ExitProcess,0

    main endp

    ;// GetLastError Procedures finds the failure reason in system-level
    GET_LAST_ERROR_PROC proc 
       invoke   GetLastError
       invoke   FormatMessage,FORMAT_MESSAGE_FROM_SYSTEM,0,eax,0,ADDR s_ErrorMessage,256,0
       invoke   MessageBox,0,s_ErrorMessage,ERROR_MSG,16
    GET_LAST_ERROR_PROC endp

end Start
    


Regards

_________________
;\\ http://theasmer.spaces.live.com \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Post 01 Jan 2007, 21:20
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.