Re: Virtual Machine implementation problem, Please help me to spot the bug



But when I use
to run a UM program, I kept on getting error messages.

What error messages?

#define dprintf(x,...) printf(x,...)

This is non-standard (Conceivably your compiler allows it
as an extension; for the rest of my reply I'll assume
it doesn't).

If your compiler is compliant with C99 you can write:
#define dprintf(x, ...) printf(x, __VA_ARGS__)

after also including <stdarg.h> of course. If your compiler
does not support this then you will have to do something
else, such as:
#define dprintf printf

which would work in this case.

void change_endian(u32* value){
u8 tmp1[4], tmp2[4];
memcpy((u32 *)tmp1, value, sizeof(u32));

tmp1 might not be correctly aligned for u32.

tmp2[0] = tmp1[3];
tmp2[1] = tmp1[2];
tmp2[2] = tmp1[1];
tmp2[3] = tmp1[0];
memcpy(value, (u32 *)tmp2, sizeof(u32));
}

Code like this is likely to cause problems. You should re-write
the code to:
* Not depend on any endian issues
* Not use any casts
* Not rely on alignment

Of course there are one or two rare situations when casts
are necessary, but almost every one in your code is
masking potential undefined behaviour.

The above function could be improved to: (note, better
would be to write code that doesn't use it at all, and
the following would still cause trouble on the DS9000)

void change_endian(u32* value){
u8 *ptr = (u8 *)value;
u8 temp[4];

temp[0] = ptr[3];
temp[1] = ptr[2];
temp[2] = ptr[1];
temp[3] = ptr[0];

*value = temp;
}

.



Relevant Pages

  • Re: Capturing error line on exception (SWI)
    ... > print_message/2 to catch and print error messages. ... > prevent the compiler from stopping after the first error. ... > an explicitely passed input stream to avoid trouble with directives that ... > may modify the current input stream and do really weird things. ...
    (comp.lang.prolog)
  • C Compiler Errors (For Real).
    ... These are some of the error messages produced by Apple's MPW C ... afternoon and decompiled the String resources for the compiler.) ... "Can't cast a void type to type void (because the ANSI spec. ... "This label is the target of a goto from outside of the block containing this ...
    (rec.humor.funny.reruns)
  • Re: The Decline of C/C++, the rise of X
    ... >> Jim points out later, and poorer error messages on a syntax error, as ... > clearly identify the position of an error is a feature of a compiler ...
    (comp.programming)
  • Re: The Decline of C/C++, the rise of X
    ... >> Jim points out later, and poorer error messages on a syntax error, as ... > clearly identify the position of an error is a feature of a compiler ...
    (comp.programming)
  • Re: The Decline of C/C++, the rise of X
    ... >> Jim points out later, and poorer error messages on a syntax error, as ... > clearly identify the position of an error is a feature of a compiler ...
    (comp.programming)