NO

Author Topic: How to delimit piece of code  (Read 4538 times)

skirby

  • Guest
How to delimit piece of code
« 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.

Code: [Select]
#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:

Code: [Select]
_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.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: How to delimit piece of code
« Reply #1 on: May 30, 2007, 06:01:48 PM »
Sample:
Code: [Select]
#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;
}
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

skirby

  • Guest
Re: How to delimit piece of code
« Reply #2 on: May 30, 2007, 09:01:06 PM »
Hello frankie,

It is exactly what I wanted to test.
Thanks a lot for your answer.

I simply changed:
Code: [Select]
__asm {
    lbl1:
    mov eax,lbl1
    mov p1, eax
}
by
Code: [Select]
__asm {
    mov eax,lbl1
    mov p1, eax
    lbl1:
}

and
Code: [Select]
for(p=p1; p<=p2; p++)by
Code: [Select]
for(p=p1; p<p2; p++)
in order to delimit precisely the code.