FASM - Create and execute batch file

0

In Assembly (using FASM assembler), I would like to create a .bat file and then run it.

I can create the file normally using the functions contained in msvcrt.dll. I close the file and try to run using system, but nothing happens (not even an error message)

The file is created correctly. Program code section:

main:
push mode ; w
push fileN ; run.bat
call [fopen]

pop [fileP] ; pego o endereço do stream do arquivo e jogo em fileP

push [fileP] ; coloco novamente o valor na pilha, para o fwrite
push eax
mov eax, 85 ; número de valores para escrever
push eax
mov eax, 1 ; tamanho em bytes de cada valor
push eax
push dataB ; conteúdo do arquivo batch
call [fwrite] ; -> até aqui tudo ok

push [fileP] ; endereço stream do arquivo
call [fclose] ; Creio que funcione corretamente

push pExec ; string contento os comandos pada executar
call [system] ; executado corretamente, mas não executa o .bat
call [exit]

I assumed that it might not be closing the file correctly, or that at runtime the file would still not be closed (by system delay)

I tried to use system to use the timeout, anyway nothing happened. (except timeout execution)

I thought about using sleep, but I do not know how to use it in ASM. (I've also tried to do delay with a loop, same result.)

Would anyone know the error?

More details: I am mounting to 32bits, the S.O. is Windows 7 64bit.

    
asked by anonymous 20.10.2016 / 02:33

1 answer

0

I ended up discovering the error (this being stubborn and not using the cinvoke macro.

Actually the function returns values in eax, and was picking up the stack value. I wrote correctly because I was pushing eax (throwing the stream address correctly for fwrite)

I got confused at the time of writing this, and so ended up giving this result. In the end, the error was actually closing the file.

The correct code:

main:
push mode ; w
push fileN ; run.bat
call [fopen]

mov [fileP], eax ; pego o endereço do stream do arquivo e jogo em fileP
push eax ; jogo na pilha para o fwrite
mov eax, 85 ; número de valores para escrever
push eax
mov eax, 1 ; tamanho em bytes de cada valor
push eax
push dataB ; conteúdo do arquivo batch
call [fwrite]

push [fileP] ; endereço stream do arquivo
call [fclose]

push pExec ; string contento os comandos para executar
call [system]
call [exit]
    
20.10.2016 / 04:00