int vs uint8_t

I'm implementing a tiny 8 bit emulator in my Arduino and I'm using some example code, however, the person is using type "int" for the 8 bit registers and the masking off the front with a & 0xFF after every operation. That seems to me to be a waste of space and very repetitious. Is there a reasoning behind this? Looking closer, it seems that if you do bitwise operations in C, the type with get "promoted" to an int. Is that true for the Arduino too? I'm also seeing that that using the -mint8 switch to make all ints 8 bit for compiling is a no-no because it will break lots of stuff that assumes int is 16 bits.

Should I just deal with the mask? I feel that there should be a macro or something that will make my life easier, but I'm kind of weak in that.

Something is telling me I should just deal with it :)

example code:

// popByte() - Pops a byte
int popByte() {
  return( memory[regPC++] & 0xff );
}

Without more context, I'd just go ahead and use uint8_t if that's what the bits represent.

Most literals are interpreted as ints unless you tell them specifically otherwise, like with a cast

How is "memory" declared? Looks silly to me, if "memory" is a byte type. Maybe they are trying to sign-extend.

halkun:
I’m implementing a tiny 8 bit emulator in my Arduino and I’m using some example code, however, the person is using type “int” for the 8 bit registers and the masking off the front with a & 0xFF after every operation. That seems to me to be a waste of space and very repetitious. Is there a reasoning behind this?

No, I suspect the person was just being lazy.

halkun:
Looking closer, it seems that if you do bitwise operations in C, the type with get “promoted” to an int. Is that true for the Arduino too?

No.

halkun:
Should I just deal with the mask? I feel that there should be a macro or something that will make my life easier, but I’m kind of weak in that.

Something is telling me I should just deal with it :slight_smile:

example code:

// popByte() - Pops a byte

int popByte() {
  return( memory[regPC++] & 0xff );
}

You should definitely change that function to return a “byte” (uint8_t).

a) It’s not true that the function returns an “int” - even the name of the function says so!

b) Promoting everything ints is very inefficient on an 8-bit CPU like the one in the Arduino, both in terms of speed and code size. Avoid doing it at all costs.

PS: I hope “memory” isn’t defined as ints…

I'd change the return type as byte, as others suggested. The bitmask operation would then become pointless. Also, I think the name of the function is inappropriate, because it seems it's not dealing with a stack and it's not removing(*) anything from the memory array. It's just reading (peek ?) a byte and incrementing the program counter register (or so it seems). So I better name would be IMHO fetchNextInstruction() or something.

(*) as is "remove the top of the stack and return it to the caller"

my 2 cents.

using type "int" for the 8 bit registers

That's not uncommon.

You also see the complete disregard for signage too.

Not uncommon.