The MASM Forum Archive 2004 to 2012
Welcome, Guest. Please login or register.
January 22, 2022, 09:31:32 AM

Login with username, password and session length
Search:     Advanced search
128553 Posts in 15254 Topics by 684 Members
Latest Member: mottt
* Home Help Search Login Register
+  The MASM Forum Archive 2004 to 2012
|-+  General Forums
| |-+  The Workshop
| | |-+  Having trouble converting my code to assembly.
« previous next »
Pages: [1] Print
Author Topic: Having trouble converting my code to assembly.  (Read 4857 times)
David
Guest


Email
Having trouble converting my code to assembly.
« on: November 23, 2009, 05:00:12 PM »

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
Code:

#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
Code:
;.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?
Logged
hutch--
Administrator
Member
*****
Posts: 12013


Mnemonic Driven API Grinder


Re: Having trouble converting my code to assembly.
« Reply #1 on: November 24, 2009, 01:14:37 AM »

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.
Logged

Regards,



Download site for MASM32
http://www.masm32.com
jj2007
Member
*****
Gender: Male
Posts: 6011



Re: Having trouble converting my code to assembly.
« Reply #2 on: November 24, 2009, 08:06:33 AM »


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 ThumbsUp

.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
Logged

sinsi
Member
*****
Gender: Male
Posts: 1758


RIP Bodie 1999-2011


Re: Having trouble converting my code to assembly.
« Reply #3 on: November 24, 2009, 08:26:33 AM »

>Option dd O1, O2, O3
That's FASM syntax - shouldn't there be a few OFFSETs there?
Logged

Light travels faster than sound, that's why some people seem bright until you hear them.
jj2007
Member
*****
Gender: Male
Posts: 6011



Re: Having trouble converting my code to assembly.
« Reply #4 on: November 24, 2009, 10:39:26 AM »

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 ThumbsUp

Code:
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
Logged

David
Guest


Email
Re: Having trouble converting my code to assembly.
« Reply #5 on: November 24, 2009, 09:36:18 PM »

Thank you very much jj2007
Logged
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP The MASM Forum Archive 2004 to 2012 | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.
Valid XHTML 1.0! Valid CSS!