FASM - Create and run batch file

Asked

Viewed 111 times

0

Under 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 occurs(not even an error message)

The file is created correctly. Code section of the program:

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 I might not be closing the file properly, or that at runtime the file would still not be closed (by system delay)

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

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

Would anyone know the error?

A few more details: I’m riding for 32bit, the S.O. is Windows 7 64 bit.

1 answer

0


I ended up discovering the error. (This is what you get for being stubborn and not using the cinvoke macro >.<)

In fact the function returns values in eax, and was taking the value of the stack. Wrote correctly because it was pushing eax (playing the stream address correctly for fwrite)

I got confused when it came to writing this, which is why it turned out this way. After all, the error was just 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]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.