[MSX-C] Q&A official thread

Page 1/68
| 2 | 3 | 4 | 5 | 6

Par AxelStone

Prophet (3199)

Portrait de AxelStone

25-08-2015, 20:02

Hi everybody, since some of us are starting with MSX-C I think that should be a good idea to centralice main questions in a common thread in order not to repeat the same questions. If this thread grows and contains a lot of useful information, it should be a good idea to update the wiki section of MSX-C in order to have all information ordered. Having said that, here goes the first question of the thread.

Question: I'm reading about data types on MSX, but I don't understand the following types: TINY, NAT and REGS. They has been explained by JaviLM in other thread (http://www.msx.org/forum/msx-talk/development/i-want-learn-msx-c-where-do-i-start) but I've started some experiments with first one (TINY) and the result is strange:

TINY x=0;
TINY y=20;
printf("%d,%d",x,y)

It returns "2560,2580" :-?. Why it's adding 2560 to the value of the var? I thought that TINY value was a 8 bit number (this is, 0-255). More info: if I insert any command before using TINY values, it works fine:

TINY x=0;
TINY y=20;
...any command, even void operations as printf("");...
printf("%d,%d",x,y)

It returns "0,20". It's as TINY values engine were correctly initiated in this second code. Really weird :-?
Thanks.

!login ou Inscrivez-vous pour poster

Par ARTRAG

Enlighted (6976)

Portrait de ARTRAG

25-08-2015, 20:36

Par AxelStone

Prophet (3199)

Portrait de AxelStone

25-08-2015, 20:51

It seems really a printf notation problem. If I use %u it prints another value, and if I use %x it prints other one. The most weird questions are:

  1. What is the correct modificator?
  2. Why they are printed correctly afted a void instruction? Not necessarily a printf(""), I ve tried to call a function that does nothing and it prints ok too after this call.

I suppose that if comparations works fine then its fine, this code works right:

if(x==0) {
    printf("x value is 0");
}

So x is really 0, but doesn't prints right.

Par ARTRAG

Enlighted (6976)

Portrait de ARTRAG

25-08-2015, 22:21

In this case it is better to post the whole test program

Par AxelStone

Prophet (3199)

Portrait de AxelStone

25-08-2015, 23:07

ARTRAG wrote:

In this case it is better to post the whole test program

#include <stdio.h>

main() {
  TINY x=0;
  TINY y=20;
  printf("%d,%d",x,y)
  if (x==0) {
    printf("Value of x is 0");
  }
}

It prints 2560,2580,Value of x is 0.

#include <stdio.h>

init(){}

main() {
  TINY x=0;
  TINY y=20;
  init();
  printf("%d,%d",x,y)
  if (x==0) {
    printf("Value of x is 0");
  }
}

It prints 0,20,Value of x is 0.

As you can see the printf works fine in second example, however the comparation works fine in boths.

Par ARTRAG

Enlighted (6976)

Portrait de ARTRAG

25-08-2015, 22:40

Humm... this seems a true compiler glitch, sorry I do not use msx-c.
maybe JaviLM could tell you more

Par AxelStone

Prophet (3199)

Portrait de AxelStone

25-08-2015, 23:10

ARTRAG wrote:

Humm... this seems a true compiler glitch, sorry I do not use msx-c.
maybe JaviLM could tell you more

I think the same, it seems a compiler bug. At least the comparations works, so it's fine for me. One question, what compiler do you use? I suposse that you can't use MSX-Library then, right?

Par ARTRAG

Enlighted (6976)

Portrait de ARTRAG

25-08-2015, 23:58

yes, I use the msdos version of Hi Tech C v7.8pl2. It comes with its own libraries but none msx specific.
It is a cross compilers and now it needs dosbox to be executed.

It is not free, but can be considered abandoned: it is the z80 version of this
http://www.hte.com/html/hitechc.htm

Par anonymous

incognito ergo sum (116)

Portrait de anonymous

26-08-2015, 01:55

AxelStone wrote:

Question: I'm reading about data types on MSX, but I don't understand the following types: TINY, NAT and REGS.

I'll post more details later (I have to go to work now), but for now:

TINY is a byte (actually a char), NAT is an unsigned 16-bit word, and REGS is a struct that contains values for the Z80 registers: A, F, HL, BC, DE in two TINYs for A and F, and three NATs for HL, BC and DE. This is not an actual 'window' into the CPU registers, but an 8-byte struct that we use to set the register values when calling BIOS routines.

In TYPE.H:

typedef	char	TINY;

In MSXBIOS.H:

typedef unsigned NAT;

typedef struct _regs {
	TINY	f;
	TINY	a;
	NAT	bc;
	NAT	de;
	NAT	hl;
} REGS;

Par anonymous

incognito ergo sum (116)

Portrait de anonymous

26-08-2015, 05:21

It's not a compiler bug. The MSX-C V1.2 User's Manual explains this issue in the description of the printf() function (page 208):

Quote:

Warning
When printing char values with %d, %x or %o always cast the variable to an int. This is because the value is passed to printf() as an int. Without the cast, the high order byte is undefined and it may be displayed incorrectly. The same happens with other types that are a single byte: BOOL, STATUS and TINY. %c doesn't require casting to int.

If you don't cast to int in the call to printf() then the value printed on the screen will change randomly depending on what the memory around your variable happens to contain. Just change your printf() line to something like this:

printf("%d", (int)x);

Example:

Par ARTRAG

Enlighted (6976)

Portrait de ARTRAG

26-08-2015, 08:11

It makes sense
TINY is char after all

Page 1/68
| 2 | 3 | 4 | 5 | 6