News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Translating C ++ code to masm

Started by sergei_lost_mail, January 24, 2021, 10:57:33 PM

Previous topic - Next topic

sergei_lost_mail

Hi guys, have a question.
Translation error or other error?
For Dev c++;

#include <windows.h>
#include <iostream>

using namespace std;

int main()
{
    PROCESS_INFORMATION pInfo;
    STARTUPINFO sInfo;

    ZeroMemory(&sInfo, sizeof(sInfo));
    sInfo.cb = sizeof(sInfo);
    ZeroMemory(&pInfo, sizeof(pInfo));

    CreateProcess("test.exe", NULL, NULL, NULL, false, DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &sInfo, &pInfo);
    DWORD bpAdress = 0x004012A5;
    HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, false, pInfo.dwThreadId);
    CONTEXT ctx = { 0 };
    ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;

    if (GetThreadContext(hThread, &ctx)) {
        ctx.Dr0 = bpAdress;
        ctx.Dr7 = 0x00000001;
        SetThreadContext(hThread, &ctx);
    }

    BOOL dbgDvm = false;
    while (true) {
        DEBUG_EVENT dbg = { 0 };

        if (!WaitForDebugEvent(&dbg, INFINITE)) {
            break;
        }
        if (dbg.dwDebugEventCode == EXCEPTION_DEBUG_EVENT) {
            if (dbg.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_SINGLE_STEP) {
                if (dbg.u.Exception.ExceptionRecord.ExceptionAddress == (LPVOID)bpAdress) {
                    ctx.ContextFlags = CONTEXT_FULL;
                    SuspendThread(hThread);
                    GetThreadContext(hThread, &ctx);
                    HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, false, pInfo.dwProcessId);
                    ReadProcessMemory(hProc, (LPVOID)ctx.Eip, 0, 0, 0);
                    cout << hex << "adress :" << ctx.Eip << " breakpoint is here" << endl;
                    ctx.Eip += 1;
                    SetThreadContext(hThread, &ctx);
                    ResumeThread(hThread);
                    dbgDvm = true;
                }
            }
        }
        if (dbgDvm) {
            ContinueDebugEvent(pInfo.dwProcessId, pInfo.dwThreadId, DBG_CONTINUE);
            dbgDvm = false;
        }
        else {
            ContinueDebugEvent(pInfo.dwProcessId, pInfo.dwThreadId, DBG_EXCEPTION_NOT_HANDLED);
        }

    }
    system("pause");
    return 0;
}


The part I'm trying to translate;


HelloWorld db "Hello World!",0

pInfo PROCESS_INFORMATION  <>
sInfo STARTUPINFO <>
ctx   CONTEXT <>
dbg   DEBUG_EVENT <>

debugsample db "test.exe",0
bpAdress equ 004012A5h
dbgdvm dd ?

start:
    call main
main proc   
invoke CreateProcess,addr debugsample,NULL,NULL,NULL,FALSE,DEBUG_ONLY_THIS_PROCESS,NULL,NULL,addr sInfo,addr pInfo
invoke OpenThread,THREAD_ALL_ACCESS,FALSE,pInfo.dwThreadId
MOV ctx.ContextFlags,CONTEXT_DEBUG_REGISTERS;

invoke GetThreadContext,pInfo.hThread,addr ctx
TEST EAX,EAX ;;??????????????         
JE @L_004B1D80
mov ctx.iDr0,bpAdress
mov ctx.iDr7,00000001h
invoke SetThreadContext,pInfo.hThread,addr ctx

@L_004B1D80:
mov dbgdvm,FALSE
JMP @L_004B1EE1

@L_004B1D9B:
CMP dbg.dwDebugEventCode,EXCEPTION_DEBUG_EVENT;1h
JNZ @L_004B1EA3
CMP dbg.u.Exception.pExceptionRecord.ExceptionCode,EXCEPTION_SINGLE_STEP;80000004h
JNZ @L_004B1EA3
CMP dbg.u.Exception.pExceptionRecord.ExceptionAddress,bpAdress
JNZ @L_004B1EA3

MOV ctx.ContextFlags,CONTEXT_FULL
invoke SuspendThread,pInfo.hThread
invoke GetThreadContext,pInfo.hThread,addr ctx
invoke OpenProcess,PROCESS_ALL_ACCESS,FALSE, pInfo.dwProcessId
inc ctx.regEip
invoke SetThreadContext,pInfo.hThread,addr ctx;
invoke ResumeThread,pInfo.hThread
mov dbgdvm,TRUE

@L_004B1EA3:
CMP dbgdvm,TRUE
JNZ @L_004B1ECB
invoke ContinueDebugEvent,pInfo.dwProcessId, pInfo.dwThreadId, DBG_CONTINUE
mov dbgdvm,FALSE
JMP @L_004B1EE1

@L_004B1ECB:
invoke ContinueDebugEvent,pInfo.dwProcessId, pInfo.dwThreadId, DBG_EXCEPTION_NOT_HANDLED

@L_004B1EE1:
invoke WaitForDebugEvent,addr dbg,INFINITE 
JNZ @L_004B1D9B ;;??????????????
RET
main endp

jj2007

It would be helpful if you
- told us what the problem is (error messages? where?), and what your intentions are (see Forum Rules)
- provided complete code (most of us are very lazy, and don't want to add the missing headers etc)
- added some error checking

Apart from these generic comments: the translation looks plausible at first sight. Welcome to the Forum :thup:

TouEnMasm


miss .data .code who is enough to put on trouble.
Better is to post a zip with  test.exe who is readed and with the headers in use.
Fa is a musical note to play with CL

TouEnMasm

++++ later
miss correct start
Quote
.data
...
.code
main proc
.........
main endp

start:
    call main
    invoke ExitProcess,NULL
end start
Fa is a musical note to play with CL

TimoVJL

A C source is easier to convert.
cl -c -GS- -MD -Fa test_debug_1.c
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

//int main(void)
void __cdecl mainCRTStartup(void)
{
    PROCESS_INFORMATION pInfo;
    STARTUPINFO sInfo;

    ZeroMemory(&sInfo, sizeof(sInfo));
    sInfo.cb = sizeof(sInfo);
    ZeroMemory(&pInfo, sizeof(pInfo));

    CreateProcess("test.exe", NULL, NULL, NULL, FALSE, DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &sInfo, &pInfo);

    DWORD bpAdress = 0x004012A5;

    HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, pInfo.dwThreadId);

    CONTEXT ctx = { 0 };
    ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;

    if (GetThreadContext(hThread, &ctx)) {
        ctx.Dr0 = bpAdress;
        ctx.Dr7 = 0x00000001;
        SetThreadContext(hThread, &ctx);
    }

    BOOL dbgDvm = FALSE;
    while (TRUE) {
        DEBUG_EVENT dbg = { 0 };

        if (!WaitForDebugEvent(&dbg, INFINITE)) {
            break;
        }
        if (dbg.dwDebugEventCode == EXCEPTION_DEBUG_EVENT) {
            if (dbg.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_SINGLE_STEP) {
                if (dbg.u.Exception.ExceptionRecord.ExceptionAddress == (LPVOID)bpAdress) {
                    ctx.ContextFlags = CONTEXT_FULL;
                    SuspendThread(hThread);
                    GetThreadContext(hThread, &ctx);
                    HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pInfo.dwProcessId);
                    ReadProcessMemory(hProc, (LPVOID)ctx.Eip, 0, 0, 0);
                    //cout << hex << "adress :" << ctx.Eip << " breakpoint is here" << endl;
                    ctx.Eip += 1;
                    SetThreadContext(hThread, &ctx);
                    ResumeThread(hThread);
                    dbgDvm = TRUE;
                }
            }
        }
        if (dbgDvm) {
            ContinueDebugEvent(pInfo.dwProcessId, pInfo.dwThreadId, DBG_CONTINUE);
            dbgDvm = FALSE;
        }
        else {
            ContinueDebugEvent(pInfo.dwProcessId, pInfo.dwThreadId, DBG_EXCEPTION_NOT_HANDLED);
        }

    }

    //system("pause");
ExitProcess(0);
}
; Listing generated by Microsoft (R) Optimizing Compiler Version 19.28.29335.0

TITLE C:\code\PellesC\_ForumMasm32\_Test\test_debug_1.c
.686P
.XMM
include listing.inc
.model flat

INCLUDELIB MSVCRT
INCLUDELIB OLDNAMES

PUBLIC _mainCRTStartup
EXTRN _memset:PROC
EXTRN __imp__OpenProcess@12:PROC
EXTRN __imp__ExitProcess@4:PROC
EXTRN __imp__OpenThread@12:PROC
EXTRN __imp__ReadProcessMemory@20:PROC
EXTRN __imp__GetThreadContext@8:PROC
EXTRN __imp__SetThreadContext@8:PROC
EXTRN __imp__SuspendThread@4:PROC
EXTRN __imp__ResumeThread@4:PROC
EXTRN __imp__WaitForDebugEvent@8:PROC
EXTRN __imp__ContinueDebugEvent@12:PROC
EXTRN __imp__CreateProcessA@40:PROC
EXTRN _memset:PROC
_DATA SEGMENT
$SG34856 DB 'test.exe', 00H
_DATA ENDS
; Function compile flags: /Odtp
_TEXT SEGMENT
_ctx$ = -912 ; size = 716
_sInfo$ = -196 ; size = 68
_dbg$1 = -128 ; size = 96
_pInfo$ = -32 ; size = 16
_hProc$2 = -16 ; size = 4
_bpAdress$ = -12 ; size = 4
_dbgDvm$ = -8 ; size = 4
_hThread$ = -4 ; size = 4
_mainCRTStartup PROC
; File C:\code\PellesC\_ForumMasm32\_Test\test_debug_1.c
; Line 9
push ebp
mov ebp, esp
sub esp, 912 ; 00000390H
; Line 13
push 68 ; 00000044H
push 0
lea eax, DWORD PTR _sInfo$[ebp]
push eax
call _memset
add esp, 12 ; 0000000cH
; Line 14
mov DWORD PTR _sInfo$[ebp], 68 ; 00000044H
; Line 15
push 16 ; 00000010H
push 0
lea ecx, DWORD PTR _pInfo$[ebp]
push ecx
call _memset
add esp, 12 ; 0000000cH
; Line 17
lea edx, DWORD PTR _pInfo$[ebp]
push edx
lea eax, DWORD PTR _sInfo$[ebp]
push eax
push 0
push 0
push 2
push 0
push 0
push 0
push 0
push OFFSET $SG34856
call DWORD PTR __imp__CreateProcessA@40
; Line 19
mov DWORD PTR _bpAdress$[ebp], 4199077 ; 004012a5H
; Line 21
mov ecx, DWORD PTR _pInfo$[ebp+12]
push ecx
push 0
push 2097151 ; 001fffffH
call DWORD PTR __imp__OpenThread@12
mov DWORD PTR _hThread$[ebp], eax
; Line 23
push 716 ; 000002ccH
push 0
lea edx, DWORD PTR _ctx$[ebp]
push edx
call _memset
add esp, 12 ; 0000000cH
; Line 24
mov DWORD PTR _ctx$[ebp], 65552 ; 00010010H
; Line 26
lea eax, DWORD PTR _ctx$[ebp]
push eax
mov ecx, DWORD PTR _hThread$[ebp]
push ecx
call DWORD PTR __imp__GetThreadContext@8
test eax, eax
je SHORT $LN4@mainCRTSta
; Line 27
mov edx, DWORD PTR _bpAdress$[ebp]
mov DWORD PTR _ctx$[ebp+4], edx
; Line 28
mov DWORD PTR _ctx$[ebp+24], 1
; Line 29
lea eax, DWORD PTR _ctx$[ebp]
push eax
mov ecx, DWORD PTR _hThread$[ebp]
push ecx
call DWORD PTR __imp__SetThreadContext@8
$LN4@mainCRTSta:
; Line 32
mov DWORD PTR _dbgDvm$[ebp], 0
$LN2@mainCRTSta:
; Line 33
mov edx, 1
test edx, edx
je $LN3@mainCRTSta
; Line 34
push 96 ; 00000060H
push 0
lea eax, DWORD PTR _dbg$1[ebp]
push eax
call _memset
add esp, 12 ; 0000000cH
; Line 36
push -1
lea ecx, DWORD PTR _dbg$1[ebp]
push ecx
call DWORD PTR __imp__WaitForDebugEvent@8
test eax, eax
jne SHORT $LN5@mainCRTSta
; Line 37
jmp $LN3@mainCRTSta
$LN5@mainCRTSta:
; Line 39
cmp DWORD PTR _dbg$1[ebp], 1
jne $LN6@mainCRTSta
; Line 40
cmp DWORD PTR _dbg$1[ebp+12], -2147483644 ; 80000004H
jne $LN6@mainCRTSta
; Line 41
mov edx, DWORD PTR _dbg$1[ebp+24]
cmp edx, DWORD PTR _bpAdress$[ebp]
jne $LN6@mainCRTSta
; Line 42
mov DWORD PTR _ctx$[ebp], 65543 ; 00010007H
; Line 43
mov eax, DWORD PTR _hThread$[ebp]
push eax
call DWORD PTR __imp__SuspendThread@4
; Line 44
lea ecx, DWORD PTR _ctx$[ebp]
push ecx
mov edx, DWORD PTR _hThread$[ebp]
push edx
call DWORD PTR __imp__GetThreadContext@8
; Line 45
mov eax, DWORD PTR _pInfo$[ebp+8]
push eax
push 0
push 2097151 ; 001fffffH
call DWORD PTR __imp__OpenProcess@12
mov DWORD PTR _hProc$2[ebp], eax
; Line 46
push 0
push 0
push 0
mov ecx, DWORD PTR _ctx$[ebp+184]
push ecx
mov edx, DWORD PTR _hProc$2[ebp]
push edx
call DWORD PTR __imp__ReadProcessMemory@20
; Line 48
mov eax, DWORD PTR _ctx$[ebp+184]
add eax, 1
mov DWORD PTR _ctx$[ebp+184], eax
; Line 49
lea ecx, DWORD PTR _ctx$[ebp]
push ecx
mov edx, DWORD PTR _hThread$[ebp]
push edx
call DWORD PTR __imp__SetThreadContext@8
; Line 50
mov eax, DWORD PTR _hThread$[ebp]
push eax
call DWORD PTR __imp__ResumeThread@4
; Line 51
mov DWORD PTR _dbgDvm$[ebp], 1
$LN6@mainCRTSta:
; Line 55
cmp DWORD PTR _dbgDvm$[ebp], 0
je SHORT $LN9@mainCRTSta
; Line 56
push 65538 ; 00010002H
mov ecx, DWORD PTR _pInfo$[ebp+12]
push ecx
mov edx, DWORD PTR _pInfo$[ebp+8]
push edx
call DWORD PTR __imp__ContinueDebugEvent@12
; Line 57
mov DWORD PTR _dbgDvm$[ebp], 0
; Line 58
jmp SHORT $LN10@mainCRTSta
$LN9@mainCRTSta:
; Line 60
push -2147418111 ; 80010001H
mov eax, DWORD PTR _pInfo$[ebp+12]
push eax
mov ecx, DWORD PTR _pInfo$[ebp+8]
push ecx
call DWORD PTR __imp__ContinueDebugEvent@12
$LN10@mainCRTSta:
; Line 63
jmp $LN2@mainCRTSta
$LN3@mainCRTSta:
; Line 66
push 0
call DWORD PTR __imp__ExitProcess@4
$LN11@mainCRTSta:
; Line 67
mov esp, ebp
pop ebp
ret 0
_mainCRTStartup ENDP
_TEXT ENDS
END
objconv is good too.
May the source be with you

hutch--

One of the things I learnt long ago is if you want to convert C code to assembler, set the C output to UN_optimised and when you have the assembler output, then manually optimise it and you will generally get faster code. It takes a bit of practice to get used to where that arguments are and in most instances you work on one algorithm at a time.

daydreamer

here is more info
you can expect C style programming =local arrays :lots of esp+ some number is used for accessing local arrays,because local variables end up on the stack
http://masm32.com/board/index.php?topic=7708.0
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

sergei_lost_mail

Thanks for your answers, I think more practice will be required.
http://masm32.com/board/index.php?topic=7708.0 solid information.