[MSX-C] Q&A official thread

Страница 6/56
1 | 2 | 3 | 4 | 5 | | 7 | 8 | 9 | 10 | 11

By AxelStone

Prophet (2764)

Аватар пользователя AxelStone

30-08-2015, 11:01

Nice, let's continue Wink

Question: I'd like to save an array of TINY into a file, similar as COPY function in Basic:

10 COPY A TO "file.dat" // Saves the array
20 COPY "file.dat" TO A  // Recovers the array

Where 'A' is an array. I've tried to simulate this in MSX-C, but I'm having a fight with pointers. Example:

TINY xi[2]={1,2}
testPtr() {
    TINY *buf,*buf2;
    buf=xi;
    buf2=&xi[1];
    printf("%d %d", (int)buf, (int)buf2);
}

None of them, buf nor buf2, prints "1" as result. I'd like to point *buf to start of array in order to make that:

fwrite(buf,2,1,fp);

The idea is write all array in the file at once (for an array of n positions, makes fwrite of n bytes) and then make fread of exactly n bytes to fill array, similar as COPY in Basic.

Some idea? Thanks

By ToriHino

Hero (583)

Аватар пользователя ToriHino

30-08-2015, 11:46

If you want to print the content of what buf or buf2 is pointing to you should dereference it first, using *:

printf("%d %d", (int)(*buf), (int)(*buf2);

And remember that arrays in C start counting at zero so buf2 should than point to &xi[0] to get the '1' value.

Actually there is no need for a new pointer at all, since xi itself can also be treated as a TINY* type.

By Manuel

Ascended (16961)

Аватар пользователя Manuel

30-08-2015, 12:29

buf is a pointer, so it's an address. You probably don't want to print the address.
buf[1] is the *second* element of buf.
&buf[1] is the address of the second element of buf.

By AxelStone

Prophet (2764)

Аватар пользователя AxelStone

30-08-2015, 12:47

Nice, now it works, here goes the code:

#include<stdio.h>

TINY xi[2]={1,2};

saveFile() {
	FILE *fp;
	fp=fopen("file.dat","wb");
	fwrite(xi,sizeof(char),sizeof(xi),fp); 
	fclose(fp);
}

readFile() {
	FILE *fp;
	TINY xo[2];
	fp=fopen("file.dat","rb");
	fread(xo,sizeof(char),sizeof(xi),fp);
	printf("%d",(int)xo[1]);
	fclose(fp);
}

main() {
	saveFile();
	readFile();
}

This works exactly as COPY Basic commands. Another working code. Thanks for your help Smile

By AxelStone

Prophet (2764)

Аватар пользователя AxelStone

31-08-2015, 11:05

Question: At this point I think it should be interesting to start making libraries in order to compile source codes to be used in programs. This is, ti should be fine a library "glibex" (glib extended) that:

  1. Includes glib.h
  2. Adds some methods as loadImg and loadPlt seen in this thread.

By this way, you could use in your projects "glibex.h" instead of "glib.h" in order to get extra features. I've found tutorials to make libs using gcc, but this is not applicable to MSX-C. Someone knows how to make that?

Thanks.

By DarkSchneider

Paladin (939)

Аватар пользователя DarkSchneider

31-08-2015, 11:32

PingPong wrote:

Take care of stack size. We are not on 2015 world. I do not know but i think the buffer it's actually allocated on stack. 256 maybe to large.... suggest you to declare as static

Should malloc be used?

By anonymous

incognito ergo sum (118)

Аватар пользователя anonymous

31-08-2015, 11:41

AxelStone wrote:

Question: At this point I think it should be interesting to start making libraries in order to compile source codes to be used in programs. This is, ti should be fine a library "glibex" (glib extended) that:
[...]
By this way, you could use in your projects "glibex.h" instead of "glib.h" in order to get extra features. I've found tutorials to make libs using gcc, but this is not applicable to MSX-C. Someone knows how to make that?

You have two options:

a) Split your big program into several smaller parts that can be reused, and compile/link them separately

b) Create an actual library (like MSX-C Library's graphics or math libraries)

I don't think we have enough functions for option b) yet, but the process for both is the same, with b) involving an extra step. Give me a few moments to prepare a quick demo.

By anonymous

incognito ergo sum (118)

Аватар пользователя anonymous

31-08-2015, 12:11

AxelStone wrote:

By this way, you could use in your projects "glibex.h" instead of "glib.h" in order to get extra features. I've found tutorials to make libs using gcc, but this is not applicable to MSX-C. Someone knows how to make that?

Ok, here's how you compile a program by parts using MSX-C:

1) Move into their own files the function(s) that you want to move out of your program.

You need two files:

- A .h file that contains only the function declaration, plus any #includes that this function needs.
- A .c file that #includes the corresponding .h file, followed by the actual code of the function.

2) Your main program #includes the .h of the external function (or functions).

3) Create a new batch file that will compile and assemble without linking:

(note: I called this script LINK.BAT, but it doesn't actually do any linking. I should have called it ASSEMBLE.BAT or something like that)

- Make a copy of the MSXC.BAT script and call it LINK.BAT
- Edit LINK.BAT and remove the linker command (L80) and anything that removes the .REL files. We need those REL files!
- If you want, remove also the line that removes the .MAC files. That's the assembler code for the function.

4) Assemble all the .c files that compose your program

(Again, I should have called this script ASSEMBLE.BAT instead of LINK.BAT)

At this point you will have a couple of .REL files. That's the assembled code for each part of your program in relocatable object format (this means that it's assembled code that can be linked on any memory address). You'll also have the assembler source code in a .MAC file corresponding to each .C file:

5) Link all the relocatable files

In this example I did it manually, but you can put this in your own script (this time it's appropriate to call it LINK.BAT) to avoid having to type all this every single time:

- CK.REL is the C kernel
- MAIN.REL and HELLO.REL are your program relocatables. Link them always after CK
- MLIB.REL is the MSX-C Library
- CLIB.REL is MSX-C's standard library
- CRUN.REL is the C runtime (I think it sets up internal variables, prepares command line parameters, etc)
- CEND.REL is just an almost empty .REL file that marks the end of MSX-C programs
- MAIN/N/Y/E defines the name of the .COM file. If you want to call the final program "FINAL.COM" then change this to FINAL/N/Y/E
- :XMAIN tells the linker that it has to start execution in the main() function

6) Now you have compiled and linked a program that was actually stored in several .C/.H files:

The advantages of this method are:

- You can have a program composed of many files, instead of having a single big file that your text editor won't handle well
- Faster compilation times. You only need to compile the parts that have changed.
- Reusability. You can reuse parts of your program easily by just copying the files that contain the functions you want to reuse.

By anonymous

incognito ergo sum (118)

Аватар пользователя anonymous

31-08-2015, 12:17

Creating a library involves one extra step: using the LIB80 program included with MSX-DOS2 TOOLS to join all these .REL files into a single relocatable library. That's what MLIB.REL, CLIB.REL and CRUN.REL are. The "/s" parameter in the L80 command means "search". This is what happened in the example above:

- L80 added CK.REL to the binary
- L80 added MAIN.REL to the binary
- L80 added HELLO.REL to the binary
- L80 searched the MLIB.REL library for the printf() function. It didn't find it there, so it didn't add anything to the binary.
- L80 searched the CLIB.REL library for the printf() function, found it, and then added the object code for printf() to the binary.
- L80 searched the CRUN.REL library for the printf() function and it didn't find it, so it did nothing. I guess it would have returned an error if CRUN.REL also happened to have a printf() function.
- L80 added CEND.REL to the binary

By AxelStone

Prophet (2764)

Аватар пользователя AxelStone

31-08-2015, 18:21

Thanks Javi this is a very good aproach. If you split code into several parte you will gain compilation split. You can put standar and tested routines to handle graphic,files, sound...in precompiled programa in order to reuse them anywere. I am really surprised by MSX-C flexibility...Let's go for a very general question.

Question: Somebody knows the sintaxis to insert asm code into a C program?

Once more, thanks Wink

Страница 6/56
1 | 2 | 3 | 4 | 5 | | 7 | 8 | 9 | 10 | 11