Pelles C forum
C language => Expert questions => Topic started by: skirby on May 30, 2007, 02:50:55 PM
-
I would like to delimit a piece of code.
I have written a small example but it does not work.
#include <stdio.h>
int main(void) {
int a, *b, c;
_asm { lbl1: }
for (a = 0; a < 10; a++) {
printf("a : %d\n", a);
}
_asm { lbl2: }
for (b = &lbl1; b < &lbl2; b++) {
printf("%X ", (unsigned char)*b);
}
LblBegin:
c = LblBegin;
printf("LblBegin : %X ", &LblBegin);
return 0;
}
In assembly, I believe you can use something like this:
_asm
{
BeginCode:
...
...
EndCode:
MOV EAX,OFFSET EndCode // EAX contains the address of the begin of the piece of code
MOV ECX,OFFSET EndCode
SUB ECX,OFFSET BeginCode // ECX contains the size of piece of code
}
Is it possible to do the same thing in C?
I have tried with C label but it does not work (compiler cannot get address value of a label)
Have got the following errors:
Building main.obj.
C:\TestLabel\main.c(12): error #2048: Undeclared identifier 'lbl1'.
C:\TestLabel\main.c(12): error #2048: Undeclared identifier 'lbl2'.
C:\TestLabel\main.c(17): error #2048: Undeclared identifier 'LblBegin'.
*** Error code: 1 ***
Done.
Any idea?
Thanks in advance and have a nice day.
-
Sample:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
unsigned char *p1, *p2, *p;
__asm { lbl1: mov eax,lbl1
mov p1, eax
}
printf("Hello!\n");
// If you make main a void, you also get the
// return code printed...
__asm { lbl2: mov eax,lbl2
mov p2, eax
}
for(p=p1; p<=p2; p++)
{
printf ("Byte @ 0x%p = %0.2x - '%c'\n", p, *p, isalpha(*p) ? *p : '.' );
}
return 0;
}
-
Hello frankie,
It is exactly what I wanted to test.
Thanks a lot for your answer.
I simply changed:
__asm {
lbl1:
mov eax,lbl1
mov p1, eax
}
by
__asm {
mov eax,lbl1
mov p1, eax
lbl1:
}
and
for(p=p1; p<=p2; p++)
by
for(p=p1; p<p2; p++)
in order to delimit precisely the code.