-1

I created a simple C program a while ago. It's a simple command-line generator that takes some number, prints the results and stops. I always ran it in the editor's command line enviroment that automatically paused after the program ran, so I omitted adding a getchar() at the end.

I now regret this, because I managed to lose the source. All I have now is the complied .o and .exe file, and the latter - of course - exits immediately after it prints the output, so it's unusable. It wasn't that long, about 100 lines, but I'd like to avoid rewriting it. (Also, I might even learn something new from this way.)

Now I have very basic knowledge of C, and about zero on computer-degree x86 assembly (though I learnt the basics of 8086-assembly for microcontrollers, it won't be that helpful now I guess), so I'm kinda stuck here. Can I either add that getchar() like pausing function to the complied code, or is there any way I can make that .exe stop before exiting while still keeping it standalone?

The program will run on a Windows 10 system.

Neinstein
  • 958
  • 2
  • 11
  • 31
  • 2
    What operating system are you programming on? Generally, this sort of thing is tricky but can be done using a binary editor. Have you considered wrapping your program in a batch file that executes `pause` just after running your program? – fuz Mar 10 '18 at 14:35
  • @IharobAlAsimi I see, as a beginner that's the way I usually do it. But it can be anything. – Neinstein Mar 10 '18 at 14:36
  • @fuz I'm on Windows 10 (that's why the .exe). Wrapping can be an answer too, but I'm a bit curious for the assembly-way. – Neinstein Mar 10 '18 at 14:37
  • 2
    The idea of editing a command line program to be worse, in order to work around terminal limitations, is very unpleasant to me. The correct solution is wrapping it, or setting some kind of option in the terminal -- I'm not familiar enough with Windows to know if that would work. https://stackoverflow.com/q/9244280/3650362 has some suggestions – trent Mar 10 '18 at 14:50
  • 1
    You probably should get some kind of version control, so you got your source code safe. – ulmer-a Mar 10 '18 at 15:10
  • @Neinstein It's rather difficult, you typically use a special program like IDApro that allows you to edit the binary. This requires a lot of experience with assembly, it's unlikely that you are able to do that without any prior experience with assembly. – fuz Mar 10 '18 at 16:02

2 Answers2

7

I would write some sort of batch script in which you call your program and then just run pause, which waits for you to hit a key before it continues.

wrapper.bat:

yourprogram.exe
pause

Of course you can disassemble your executable into raw x86 assembly code, then look up the code for a simple getchar() on Windows, add that and reassemble. However, it would probably be less time consuming to rewrite the program, depending on how complex it was or just create a wrapper batch-script.

ulmer-a
  • 387
  • 1
  • 11
  • Does Windows's built-in terminal window really not have an option to keep the terminal window open after the program exits? This is a pretty basic feature which some other terminal emulators have, and is obviously widely desired for the way Windows terminals are used (opened for a single program, rather than for a typed command exiting back to a text shell.) e.g. [`urxvt ` has a `-hold` option](https://linux.die.net/man/1/urxvt) to keep the window open after the program exits. – Peter Cordes Mar 11 '18 at 01:23
1

It's possible to hijack .o file, you can even do it with .exe, .dll, ... but it's not simple and requires a lot of know-how. What I would suggest is to use some sort of decompiler to try to restore the original source code, make the change and compile it again. You can find suggestions of decompilers in this old answer.

Simon Berthiaume
  • 643
  • 4
  • 11