X86 Assembly Newpie write file problem

Discussion in 'Programmer's Corner' started by hab2008, Dec 4, 2012.

  1. hab2008

    Thread Starter New Member

    Dec 4, 2012
    2
    0
    Hi,

    I am learning x86 Assembly language and found it interesting by following Youtube videos and some other tutorials. Of course I am going to by the most popular book of this language called: Art of Assembly

    But I tried first to follow tutorials then by a book, that is my habit in learning languages :)

    I tried to modifi a BMP file but each try results in an empty or incorrect formatted BMP file.

    I am looking for a clue over and over but for 3 days I could not find what I am doing wrong :(

    I am using MASM 6 for Windows 32bit.

    Here is my ASM file:
    Code ( (Unknown Language)):
    1.  
    2.         .586
    3.         .model      flat, stdcall
    4.         option      casemap :none
    5.         include     ..\masm32\include\windows.inc
    6.         include     ..\masm32\include\user32.inc
    7.         include     ..\masm32\include\kernel32.inc
    8.         include     ..\masm32\macros\macros.asm
    9.         include     ..\masm32\include\masm32.inc
    10.  
    11.         includelib  ..\masm32\lib\user32.lib
    12.         includelib  ..\masm32\lib\kernel32.lib
    13.         includelib  ..\masm32\lib\masm32.lib
    14.  
    15.         .data
    16. FileName    db  "source.bmp", 0
    17. filename    db  "result.bmp",0
    18. errMsg      BYTE    "Cannot create file",0dh,0ah,0
    19.    
    20. hFile       HANDLE  ?
    21. hwFile      HANDLE  ?
    22.  
    23. hMemory     HANDLE  ?       ;incoming data
    24. pMemory     DWORD   ?
    25.  
    26. hMemory_o   HANDLE  ?       ;outgoing data
    27. pMemory_o   DWORD   ?
    28.  
    29. ReadSize    DWORD   ?
    30.  
    31. bytesWritten    DWORD   ?
    32. firstLine   DWORD   ?
    33. FileSize    DWORD   ?
    34. BDoff       DWORD   ?
    35. BHSize      DWORD   ?
    36. szTemp      byte    16 dup (0)  ;buffer for messages
    37. szPrime     byte    "%08i", 0   ;message format string
    38. szPrimeH    byte    "%08lx",0   ;message hexa format string
    39. signature   DD  0
    40. MEMORYSIZE      equ 75535   ;This is how much memory I'll allocate,
    41.                 ;so I will have a lot of space to store my file.
    42.                 ; I know this may seam stubid but I am learning Assemlby and do not
    43.                 ; want to face unneccessry bugs
    44. im_offset   dd  ?
    45. im_width    dd  ?
    46. im_height   dd  ?
    47. bits_pix    dd  ?
    48.  
    49.  
    50.  
    51.  
    52.     .code
    53. ;................................
    54. show    MACRO   caption, value
    55.     print   SADD(caption)
    56.     mov eax, value
    57.     invoke  wsprintf, offset szTemp, offset szPrime, eax    ;converts eax into string
    58.     print   offset szTemp                   ;print string
    59.     print   SADD(13,10)
    60.     ENDM
    61. ;..................................
    62.  
    63. start: 
    64.  
    65.     invoke      CreateFile, addr FileName,GENERIC_READ,FILE_SHARE_READ,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
    66.     mov      hFile, eax
    67.  
    68. ;Allocate and lock the memory for incoming file.
    69.     invoke      GlobalAlloc, GMEM_MOVEABLE or GMEM_ZEROINIT, MEMORYSIZE
    70.     mov         hMemory, eax
    71.     invoke      GlobalLock, hMemory
    72.     mov         pMemory, eax
    73.  
    74. ;Allocate and lock the memory for outgoing file.
    75.     invoke      GlobalAlloc, GMEM_MOVEABLE or GMEM_ZEROINIT, MEMORYSIZE
    76.     mov         hMemory_o, eax
    77.     invoke      GlobalLock, hMemory_o
    78.     mov         pMemory_o, eax
    79.  
    80.  
    81. ;Read file and save image parameters
    82.     invoke      ReadFile, hFile, pMemory, MEMORYSIZE-1, addr ReadSize, NULL
    83.     mov         esi, pMemory
    84.  
    85.     add         esi, 02     ;get filesize
    86.     mov         edi, [esi]
    87.     invoke      wsprintf, offset szTemp, offset szPrime, edi
    88.     print       offset szTemp
    89.     print       SADD(10,13)
    90.    
    91.     add         esi, 8      ; jump 8 bytes to get image offset
    92.     mov         edi, [esi]  ; get image offset
    93.     mov         im_offset,edi
    94.     invoke      wsprintf, offset szTemp, offset szPrimeH, edi
    95.     print       offset szTemp
    96.     print       SADD(10,13)
    97.  
    98.     add         esi,8       ;jump 8 bytes to get image width
    99.     mov         edi, [esi]  ; get image width      
    100.     mov         im_width, edi  
    101.     invoke      wsprintf, offset szTemp, offset szPrime, edi
    102.     print       offset szTemp
    103.     print       SADD(10,13)
    104.  
    105.     add         esi,4       ;jump 4 bytes to get image height
    106.     mov         edi, [esi]  ; get image height
    107.     mov         im_height, edi 
    108.     invoke      wsprintf, offset szTemp, offset szPrime, edi
    109.     print       offset szTemp
    110.     print       SADD(10,13)
    111.  
    112.     add         esi,4       ;jump 4 bytes to get color plane
    113.     mov         ebx, [esi] 
    114.     shr         ebx,16      ; get color plane and bit-pix
    115.     print       SADD("bit-per-pix ")
    116.     invoke      wsprintf, offset szTemp, offset szPrime, ebx
    117.     print       offset szTemp
    118.     print       SADD(10,13)
    119.  
    120.     mov         ebp, pMemory    ; get ready to start processing the image
    121.     add         ebp, im_offset  ; esi now points to the first pix
    122.  
    123. ;filtering process
    124. ;leave first row and first column and last row and last column untouched.
    125.  
    126.     mov         esi,1       ; esi is the row counter
    127.     mov         edi,1       ; edi is the column counter
    128.  
    129.  
    130. proc_pix:
    131.     xor         ebx,ebx     ; ebx = 0 ebx will accumulate intermediate values for averaging
    132.     mov         eax, im_width   ; eax is the pointer to the pixel
    133.     mul         esi
    134.     add         eax,edi    
    135.     add         ebx, [ebp+eax]  ;get the pixel(I,J)
    136.  
    137.     mov         eax, im_width
    138.     add         esi,1       ;next row (I+1)
    139.     mul         esi
    140.     add         eax,edi    
    141.     add         ebx, [ebp+eax]  ;get the pixel(I+1,J)
    142.  
    143.  
    144.     mov         eax, im_width
    145.     sub         esi,2       ; prev row (I-1)
    146.     mul         esi
    147.     add         eax,edi    
    148.     add         ebx, [ebp+eax]  ;get the pixel(I-1,J)
    149.  
    150.     add         esi,1       ;back to the current row
    151.  
    152.     add         edi,1       ;get next column (J+1)
    153.     mov         eax, im_width
    154.     mul         esi
    155.     add         eax,edi    
    156.     add         ebx, [ebp+eax]  ;get the pixel(I,J+1)
    157.  
    158.  
    159.     sub         edi,2       ; get prev column (J-1)
    160.     mov         eax, im_width
    161.     mul         esi
    162.     add         eax,edi    
    163.     add         ebx, [ebp+eax]  ;get the pixel(I,J-1)
    164.  
    165.     add         edi,1       ;back current column
    166.     xor         edx, edx    ;clear upper part dividend
    167.     mov         eax, ebx    ;move data to eax to divide
    168.     mov         ecx, 5 
    169.     div         ecx         ;do the average (div 5)>>> result in eax
    170.    
    171.  
    172.     inc     edi         ; do the next column
    173.     cmp     edi, im_width  
    174.     jl      proc_pix
    175.  
    176.     inc         esi     ; do the next row
    177.     mov     edi,1       ; skip the first column
    178.     cmp     esi, im_height 
    179.     jl      proc_pix
    180.  
    181. ;....................................................................................
    182.  
    183.  
    184. new_file:
    185.     invoke      CreateFile,ADDR filename, GENERIC_WRITE, NULL, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
    186.     mov         hwFile,eax      ; save file handle
    187.  
    188. ; handling of error if invalid file handle
    189.     cmp         eax,INVALID_HANDLE_VALUE
    190.     jne     writef
    191.     invoke      StdOut, addr errMsg ; Display error message
    192.     jmp         QuitNow
    193.    
    194. writef: invoke      WriteFile, hwFile, pMemory_o, FileSize, ADDR bytesWritten, 0       
    195.  
    196.  
    197. QuitNow:
    198.     invoke      GlobalUnlock, pMemory
    199.     invoke      GlobalUnlock, pMemory_o
    200.     invoke      GlobalFree, hMemory
    201.     invoke      CloseHandle, hFile
    202.     invoke      CloseHandle, hwFile
    203.     invoke      ExitProcess, NULL
    204.     end         start
    205. ;finish
    206.  
     
  2. hab2008

    Thread Starter New Member

    Dec 4, 2012
    2
    0
    By the way I am using the popular "Lena" bitmap :)
     
Loading...