ok, i'm an asm newbee just starting out and trying to get familiar with the basics.
i'm having a problem with what should be a very simple piece of code.

i did a simple app which reads a file into a block of memory, then writes it back to another file.
i got this part to work fine.

next i tried to 'encrypt' the block of data before writing it back to the file. code goes something like this:
;============================
;'encrypt' code here
push ebx
mov ebx, 0
mov ecx, pMemory
.while ebx != SizeReadWrite

inc
inc pMemory
inc ebx
.endw
mov pMemory, ecx
pop ebx
;============================
this goes between the read/write blocks. it's supposed to increment whatever it reads by one but it doesn't. the output is exactly identical to input. either it's too late at night or i'm having a serious logic problem with my pointers.

the full code is listed below:
;===========START=============
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\comdlg32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\comdlg32.lib

.const
MAXSIZE equ 260
MEMSIZE equ 65535
.data

ofn OPENFILENAME <>
FilterString db "All Files",0,"*.*",0
db "Text Files",0,"*.txt",0,0
buffer db MAXSIZE dup(0) ;filename string buffer

.data?
hFile HANDLE ? ;file handle
hMemory HANDLE ? ;memory handle
pMemory DWORD ? ;memory pointer
SizeReadWrite DWORD ?

.code
start:

;initialize common parametres of OPENFILE struct
mov ofn.lStructSize,SIZEOF ofn
mov ofn.hWndOwner, NULL
mov ofn.hInstance, NULL
mov ofn.lpstrFilter, OFFSET FilterString
mov ofn.lpstrFile, OFFSET buffer
mov ofn.nMaxFile,MAXSIZE

mov ofn.Flags, OFN_FILEMUSTEXIST or \
OFN_PATHMUSTEXIST or OFN_LONGNAMES or\
OFN_EXPLORER or OFN_HIDEREADONLY
invoke GetOpenFileName, ADDR ofn
.if eax==TRUE
invoke CreateFile,ADDR buffer,\
GENERIC_READ or GENERIC_WRITE ,\
FILE_SHARE_READ or FILE_SHARE_WRITE,\
NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,\
NULL
mov hFile,eax ;open file and return handle in hFile
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,MEMSIZE
mov hMemory,eax
invoke GlobalLock,hMemory
mov pMemory,eax ;reserve 65KB of memory and get a pointer to it
invoke ReadFile,hFile,pMemory,MEMSIZE-1,ADDR SizeReadWrite,NULL
invoke CloseHandle,hFile
.endif

;============================
;'encrypt' code here
push ebx
mov ebx, 0
mov ecx, pMemory
.while ebx != SizeReadWrite

inc
inc pMemory
inc ebx
.endw
mov pMemory, ecx
pop ebx
;============================

mov ofn.Flags,OFN_LONGNAMES or\
OFN_EXPLORER or OFN_HIDEREADONLY
invoke GetSaveFileName, ADDR ofn
.if eax==TRUE
invoke CreateFile,ADDR buffer,\
GENERIC_READ or GENERIC_WRITE ,\
FILE_SHARE_READ or FILE_SHARE_WRITE,\
NULL,CREATE_NEW,FILE_ATTRIBUTE_ARCHIVE,\
NULL
mov hFile,eax
mov eax, SizeReadWrite
invoke WriteFile,hFile,pMemory,eax,ADDR SizeReadWrite,NULL
invoke CloseHandle,hFile
invoke GlobalUnlock,pMemory
invoke GlobalFree,hMemory
.endif

invoke ExitProcess,NULL

end start

;=============END===========
Posted on 2003-11-23 06:18:26 by watahhh
Where is your encryption code? How come I see none?
Posted on 2003-11-23 06:38:52 by roticv
it's supposed to increment whatever it reads by one but it doesn't


something like this would do thiat
;============================
;'encrypt' code here
push ebx
xor ecx, ecx
mov ebx, pMemory
.while ecx != SizeReadWrite
inc BYTE PTR
inc ebx
inc ecx
.endw
pop ebx
Posted on 2003-11-23 06:47:43 by ENF
inc


Double-dereferencing memory is not possible in ASM.
Posted on 2003-11-23 10:51:09 by gliptic
Masm will just ignore the brackets and increase the pointer.
Posted on 2003-11-23 11:30:18 by roticv

Masm will just ignore the brackets and increase the pointer.


Without any warning ???

Btw i never use [ ] with variables
Posted on 2003-11-23 11:54:46 by AceEmbler
nope, no warrning from masm.
thanks guys, just what i needed
Posted on 2003-11-23 15:04:33 by watahhh
ok, more problems.
what i'm trying to do now is read the file in segments, so read one chunk in memory, encrypt, and overwrite back to same file on top of original data, then read next segment and so on. (the code below only does one segment at the moment)

i tried using the OVERLAPPED structure but i can't get it to work and I don't know y. for one thing, the code never finishes and it does some pretty weird things. probably best not to run it.

if anyone could point out some of the mistakes i would appreciate it.

(i really should start using a good debugger)

;@@@@@@@ START @@@@@@@@

.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\comdlg32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\comdlg32.lib
includelib \masm32\lib\user32.lib

.const
MAXSIZE equ 260
MEMSIZE equ 5 ;start off with small segments for easy testing

.data
MsgCaption db "Tester",0
movePointer LONG MEMSIZE

ofn OPENFILENAME <>
FilterString db "All Files",0,"*.*",0
db "Text Files",0,"*.txt",0,0
buffer db MAXSIZE dup(0) ;filename string buffer

over OVERLAPPED <>

.data?
hFile HANDLE ? ;file handle
hMemory HANDLE ? ;memory handle
pMemory DWORD ? ;memory pointer
SizeReadWrite DWORD ?

.code
start:
;initialize OVERLAPPED structure
mov over.loffset, 0
mov over.OffsetHigh, 0


;initialize OPENFILE struct
mov ofn.lStructSize,SIZEOF ofn
mov ofn.hWndOwner, NULL
mov ofn.hInstance, NULL
mov ofn.lpstrFilter, OFFSET FilterString
mov ofn.lpstrFile, OFFSET buffer
mov ofn.nMaxFile,MAXSIZE
mov ofn.Flags, OFN_FILEMUSTEXIST or \
OFN_PATHMUSTEXIST or OFN_LONGNAMES or\
OFN_EXPLORER or OFN_HIDEREADONLY

invoke GetOpenFileName, ADDR ofn

.if eax==TRUE
invoke CreateFile,ADDR buffer,\
GENERIC_READ or GENERIC_WRITE ,\
FILE_SHARE_READ or FILE_SHARE_WRITE,\
NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE or FILE_FLAG_OVERLAPPED,\
ADDR over
mov hFile,eax ;open file and return handle in hFile
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,MEMSIZE
mov hMemory,eax
invoke GlobalLock,hMemory
mov pMemory,eax ;reserve MEMSIZE bytes of memory and get a pointer to it


invoke ReadFile,hFile,pMemory,MEMSIZE-1,ADDR SizeReadWrite,ADDR over
;wait for read to finish?
invoke GetOverlappedResult,hFile,ADDR over,ADDR SizeReadWrite,TRUE
invoke MessageBox,NULL, ADDR MsgCaption, ADDR MsgCaption,MB_OK


;============================
;'encrypt' code here
push ebx ;save ebx
xor ecx, ecx ;set ecx = 0
mov ebx, pMemory ;save original pointer
.while ecx != SizeReadWrite
invoke MessageBeep,MB_ICONASTERISK
xor , ecx
inc ebx
inc ecx
.endw
pop ebx
;============================

invoke WriteFile,hFile,pMemory,eax,ADDR SizeReadWrite,ADDR over
;wait for write to finish?
invoke GetOverlappedResult,hFile,ADDR over,ADDR SizeReadWrite,TRUE
;move file pointer along
invoke SetFilePointer,hFile, movePointer, NULL, FILE_CURRENT
add over.loffset, MEMSIZE

invoke GlobalUnlock,pMemory
invoke GlobalFree,hMemory
invoke CloseHandle,hFile


.endif

invoke MessageBox,NULL, ADDR MsgCaption, ADDR MsgCaption,MB_OK
invoke ExitProcess,NULL
end start


;@@@@@@@ END @@@@@@@@@
Posted on 2003-11-23 21:35:58 by watahhh
nm, i figured it out. just a careless mistake on my part, and using beeps for debugging was really dumb. RadASM is awsome. Nice debugging features.
Posted on 2003-11-24 00:29:47 by watahhh
Try debugging with int 3 (with a debuggger to trap the exception) or maybe vkim's debug (Though I never used it before). Also do not *assume* that windows api will not corrupt ecx.
Posted on 2003-11-24 04:40:32 by roticv