Skip to content

Possible Endianness Issue with char Function Arguments in EM Code on MC6800 Port #332

@zu2

Description

@zu2

I am currently porting to MC6800 ( https://github.com/zu2/ack-6800 ).

It seems that the generated EM code assumes a little endian environment, which causes an issue when function arguments are of type char.

Here is a sample program:

int func(unsigned char a, unsigned char b)
{
    unsigned char c, d;

    a = &c - &d;
    b = &a - &b;

    return b;
}

The following EM code is generated:

loc 1   !  (&c-&d)
lal 4
sti 1
loc -2  ! (&a-&b)
lal 6
sti 1
  :

For the function arguments a and b, integral promotion is performed at the time of the function call, so they are treated as 2-byte integers. Thus, it makes sense that &a - &b results in -2.

However, when assigning values to a and b, only the first byte of each is written, which causes problems on big endian architectures.

When char is used as a local variable, only 1 byte is allocated, so this issue does not occur.

Is there an existing solution or a recommended way to handle this situation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions