[I think TSD wrote:]
How to apply the CX register to your program? That doesn't make any sense
whatsoever.
If you go to command and then run debug and debug something.com (or
exe), then you can use the -r command to get the register values, the
CX value here relates to the program size. Thats what I want to get
at.
On an MS-DOS system, then, you may write the following program. But
it only works if you have 'debug' or a 'debug' clone, and it only
works for small programs (because CX can store only the low 16 bits of
the program size, as far as 'debug' is concerned).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *ProgramToExamine = "myprog.exe";
int main(void)
{
char script[FILENAME_MAX];
char output[FILENAME_MAX];
char buffer[3*FILENAME_MAX+100];
unsigned int size = 0;
FILE *fp;
char *p;
/* Create a script file for 'debug' */
fp = fopen(tmpnam(script), "w");
fputs("r cx\n\nq\n\n", fp);
fclose(fp);
/* Call 'debug' */
sprintf(buffer, "debug %s < %s > %s",
ProgramToExamine, script, tmpnam(output));
system(buffer);
remove(script);
/* Retrieve second line of 'debug' output */
fp = fopen(output, "r");
fgets(buffer, sizeof buffer, fp);
p = fgets(buffer, sizeof buffer, fp);
fclose(fp);
remove(output);
/* Process it */
if (p != NULL) {
/* Match line of the form 'CX xxxx' returned under WinXP */
if (sscanf(buffer, "CX %x", &size) != 1) {
puts("Failure!");
exit(EXIT_FAILURE);
}
}
else {
puts("Couldn't open file!\n");
exit(EXIT_FAILURE);
}
printf("Size of program %s is %u (%x) bytes.\n",
ProgramToExamine, size, size);
return 0;
}
This program is standard C, but it uses the implementation-dependent
'system' call to try to execute 'debug'. How 'debug' works is not
topical in this newsgroup.
Im not sure whether its just related to C in the sense that is
newgroup use it but theres the ASM command,
That collection of words does not make sense. But you do seem
to be under the impression that C has an "ASM command," which is
wrong in at least two ways: C does not have "commands," and C does
not have any keyword "ASM" (or "asm", or "Asm", or any variation
thereon).
I could use that get the value of CX but I wouldnt know how to
then use it in the rest of the program,
I've run some tests, and AFAICT the value of CX upon program
entry has nothing to do with the program's size in bytes. And of
course the value of CX in the middle of a program has nothing to
do with anything -- the program is *using* CX to *compute stuff* at
that point!
Again, 8086 stuff is off-topic in comp.lang.c, but I just wanted
to point out that (1) C people can give you really bad x86 advice;
and (2) in this case, there's no good advice to be given except
"You can't do that that way; ask an MS-DOS group."
-Arthur