News:

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

Having trouble converting my code to assembly.

Started by David, November 23, 2009, 05:00:12 PM

Previous topic - Next topic

David

Hi guys, I am new to assembly and I love mASM! it is so much fun to program in lower level languages than it is to program in high level languages (like C++).

Anyway, back on topic, I am having trouble converting a file IO operation for my program.  It basically stores it's settings at the end of the file, then it gets those settings and adjusts values.  (Instead of using a txt file to save settings)

C++ version

#include <Windows.h>
void Read_EOF_Data();
char EOF_String[12040];

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{

    Read_EOF_Data();

    int Loop_C = 0;
    static char Option [700][700] = {0};
    char *token;

   token = strtok( EOF_String, "(%)" );
   while( token != NULL )
   {
  strcpy (Option[Loop_C],token);
      token = strtok( NULL, "(%)" );
  Loop_C++;
   }

    MessageBoxA(NULL,Option[0] , "Title" , MB_OK) ;
MessageBoxA(NULL,Option[1] , "Title" , MB_OK) ;
MessageBoxA(NULL,Option[2] , "Title" , MB_OK) ;
MessageBoxA(NULL,Option[3] , "Title" , MB_OK) ;

}

void Read_EOF_Data()
{
HANDLE hFile;
DWORD dwFileSize,dwNumRead;
char* Stub_Data = {0};
TCHAR App_Path[MAX_PATH];
GetModuleFileName(NULL,App_Path,MAX_PATH);

hFile = CreateFile(App_Path, GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

dwFileSize = GetFileSize(hFile, NULL);
    Stub_Data = new char[dwFileSize];

    ReadFile(hFile, Stub_Data, dwFileSize, &dwNumRead,NULL);
    CloseHandle(hFile);


    char Search_String[] = "{&Main_Spliter&}";
    long loCh;
    long loSe = 0;
    long EOF_No = 0;


for (loCh = 0; loCh < dwFileSize; loCh++)
{
while (Stub_Data[loCh] == Search_String[loSe])
{
loSe++;
loCh++;
if (loSe == strlen(Search_String))
{

EOF_No = (dwFileSize - loCh) - 1;
for (int i = 0; i <= EOF_No ; i++)
{
EOF_String[i] = Stub_Data[(loCh + 22) - (22 - i)];
}
}
}
    loSe = 0;
}

delete [] Stub_Data;
}


My conversion

;.368 and starting stuff here. option casemap etc

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

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

.data ?
hFile dd ?
dwFileSize dd ?
dwNumread dd ??

.data
token db 260 dUP(0)
option db 700 700 dUP(0)
StubData db 999999 dUP(0)
AppData db 260 dUP(0)
App_Path db 260 dUP(0)
splitter db "(%)", 0
Search_String db "{&Main_Splitter&}", 0
long loCh
long loSe
long EOF_No
title db "Title", 0

int Loop ;?? is this how you do an integer in assembly?

.code

start:

invoke GetModuleFileName, NULL, App_Path, 260

invoke CreateFile, addr App_Path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL

mov hFile, eax

invoke GetFileSize, hFile, NULL

mov dwFileSize, eax

invoke ReadFile, hFile, Stub_Data, dwFileSize, offset dwNumRead, NULL

CloseHandle, hFile




end start


;Not sure how to do the for loop

.WHILE Stub_Data[loCh] == Search_String[loCh] ;not sure how array is done

mov loSe, +1
mov loCh, +1

invoke strlen, Search_String

.IF loSe == eax

mov EOF_No, dwFileSize - loCh -1

;not sure how to do the for loop

;no idea how to do this EOF_String[i] = Stub_Data[(loCh + 22) - (22 - i)];




.ENDIF

mov loSe, 0


.ENDW


invoke strtok, NULL, addr splitter


mov token, eax

;Not sure how to do this?
invoke MessageBoxA, NULL, Option[1], addr title, MB_OK ;??
;Not sure how to do this

;Not sure how to do this?
invoke MessageBoxA, NULL, Option[2], addr title, MB_OK ;??
;Not sure how to do this

;Not sure how to do this?
invoke MessageBoxA, NULL, Option[3], addr title, MB_OK ;??
;Not sure how to do this

;Not sure how to do this?
invoke MessageBoxA, NULL, Option[4], addr title, MB_OK ;??
;Not sure how to do this




So, I am confused with arrays,  advanced math functions and for loops.  But I still gave it a try, how well did I convert it?  Were can I find info on the things that I am having trouble with?

hutch--

David,

In most instances you cannot successfull open a running applications file as it is blocked by the operating system. You could do it in the DOS days but not in 32 bit Windows.

The file IO is the simple part, load the file, set the offset from the beginning to where the data starts and write what you like there but I doubt there is a way to do this on 32 bit Windows unless you have some trick around OS level protection of a running process.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007

Quote from: David on November 23, 2009, 05:00:12 PM

So, I am confused with arrays,  advanced math functions and for loops.  But I still gave it a try, how well did I convert it?  Were can I find info on the things that I am having trouble with?

Your conversion is very well advanced, compliments :thumbu

.While.... : You cannot do a direct mem to mem comparison. One of the operators must go to a register, e.g. ebx or esi or edi, but study the rules for preserving these registers

There are no FOR loops in Masm32, but the MasmBasic library has a For_ ... Next macro.

invoke MessageBoxA, NULL, Option[1], addr title, MB_OK

You can set the options like this:
Option dd O1, O2, O3
O1 db "This is option 1", 0
O2 db "This is option 2", 0
O3 db "Ok, I got it", 0
BUT:
invoke MessageBoxA, NULL, Option[0], addr title, MB_OK
invoke MessageBoxA, NULL, Option[4], addr title, MB_OK
invoke MessageBoxA, NULL, Option[8], addr title, MB_OK

cheers, jj

sinsi

>Option dd O1, O2, O3
That's FASM syntax - shouldn't there be a few OFFSETs there?
Light travels faster than sound, that's why some people seem bright until you hear them.

jj2007

Hi Sinsi,
It's Masm syntax, too, but I had overlooked that option and title are reserved words. Here is an example that works.
Apologies to David :thumbu

include \masm32\include\masm32rt.inc

.data

MyOption dd O1, O2, O3
O1 db "This is option 1", 0
O2 db "This is option 2", 0
O3 db "Ok, I got it", 0

.code
MyTitle db "My options:", 0

start:

invoke MessageBox, NULL, MyOption[0], addr MyTitle, MB_OK
invoke MessageBox, NULL, MyOption[4], addr MyTitle, MB_OK
invoke MessageBox, NULL, MyOption[8], addr MyTitle, MB_OK

exit ; short form of invoke ExitProcess, 0

end start