Assembly and C

C and PASCAL (or any other high-level languages) in here please

Moderators: simonsunnyboy, Mug UK, Zorro 2, Moderator Team

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Assembly and C

Postby DrCoolZic » Mon Jan 22, 2007 6:43 pm

Sorry basic questions ... but as I do not have documentation ...
I am using the Lattice C compiler, and I need to code few routines in assembly. I have succeded in writing a skeleton for an assembly routine that I call from C (I had to turn a flag in the assembler that add an _ in front of the symbol names):

Code: Select all

        TEXT
         XDEF   testasm
testasm:
      movem.l      d2-d7/a2-a6,savereg      ; save C register
      move.w      #10,d0
      
restore:
      movem.l      savereg,d2-d7/a2-a6      ; restore C register
      rts
       
        BSS
savereg   ds.l      11
         END

The C code
int testasm(int a, int b);

result = testasm(10, 20);

I found that result must be returned in D0 but how do I get the parameters in the asm code? Say for example the code would return a+b
I thought it was passed on the stack and tried:

Code: Select all

move.w   4(sp),d0
add.w    8(sp),d0
but does not work.
Can someone help?

Is it the same for Pure C that I am using from time to time?

Is there a way to include some assembly instruction in a C code? I have seen for ??? C copiler the following syntax
....
ASM {
....
}
Does this exist for Lattice or Pure C compilers?

Thanks

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Re: Assembly and C

Postby Nyh » Mon Jan 22, 2007 8:37 pm

DrCoolZic wrote:I found that result must be returned in D0 but how do I get the parameters in the asm code? Say for example the code would return a+b
I thought it was passed on the stack and tried:

Code: Select all

move.w   4(sp),d0
add.w    8(sp),d0
but does not work.
Can someone help?

Ok, I am a Pure C and assembly coder. My manuals went to someone who wanted to scan them so I cannot look it up for you. However the convention is parameters are passed in D0, D1, D2 and A0, A1. The result is in D0.

Code: Select all

XDEF testasm

;int testasm(int a, int b, int* c) returns a + b + *c
testasm:
     movem.l  d2-d7/a2-a6,-(sp)    ; usually we store the registers on the stack. That way we can make recursive functions
     add.w   d1,d0
     add.w  (a0),d0
     movem.l  (sp)+,d2-d7/a2-a6
     rts


DrCoolZic wrote:Is there a way to include some assembly instruction in a C code? I have seen for ??? C copiler the following syntax
....
ASM {
....
}
Does this exist for Lattice or Pure C compilers?

Pure C has something like that. No inline assembly but with:

Code: Select all

#define save_regs movem_save();reglist();
#define restore_regs movem_load();reglist();
void movem_save(void) 0x48e7;     /* movem.l reglist,-(sp) */
void movem_load(void) 0x4cdf;     /* movem.l (sp)+,reglist */
void reglist(void) 0xffff;        /* -1, alle registers */

You can do some inline code. See for more info: http://www.atari-forum.com/viewtopic.php?t=10006

Hans Wessels

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Postby DrCoolZic » Tue Jan 23, 2007 6:47 am

I tried what you suggest and it does not work for Lattice.
1) not sure I understand the logic of param passing with Pure C. Is it Int parameters passed in D register 0,1,2,... and pointers passed in A register 0,1,2,... ? What if you have a lot of parameters ??? I will reinstall Pure and give a try.
2) I have a very old Lattice doc (V3.04) and it says parameters passed on stack from left to right (all promoted to 4 or 8 bytes). This seems to make sense to pass any number of param. If this is what is actually used by Lattice C 5.60 would the instructions move.w 4(sp),d0 add.w 8(sp),d0 do the job?
3) great advice for storing register on stack.

About the Lattice C doc. I am wandering what Bolle is doing??? So many months now and not a word? I left several pm and he never replied?! Is it still doing something or run away?

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Postby Nyh » Tue Jan 23, 2007 10:00 am

DrCoolZic wrote:I tried what you suggest and it does not work for Lattice.
1) not sure I understand the logic of param passing with Pure C. Is it Int parameters passed in D register 0,1,2,... and pointers passed in A register 0,1,2,... ? What if you have a lot of parameters ??? I will reinstall Pure and give a try.

The first 3 int type parameters are passed in d0-d2, the first two pointers in a0-a1 the rest goes via the stack.

DrCoolZic wrote:2) I have a very old Lattice doc (V3.04) and it says parameters passed on stack from left to right (all promoted to 4 or 8 bytes). This seems to make sense to pass any number of param. If this is what is actually used by Lattice C 5.60 would the instructions move.w 4(sp),d0 add.w 8(sp),d0 do the job?

Almost. You say they are promoted to longs so you can find them at:
move.w 6(sp),d0 and add.w 10(sp),d0 or use them as longs: move.l 4(sp),d0, add.l 8(sp),d0.
Remember to add the offset for saved registers on the stack.
Passing the first parameters in registers is more efficient because normally you need the parameters in registers anyway.

Hans Wessels

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Postby DrCoolZic » Tue Jan 23, 2007 11:21 am

Nyh wrote:...
move.w 6(sp),d0 and add.w 10(sp),d0 or use them as longs: move.l 4(sp),d0, add.l 8(sp),d0.

Bingo it works. You have to realize that it is the first lines of 68000 assembly code I ever wrote, and I fill a bit stupid.
Both solutions you provided works but I am a bit confuse.
I thought n(sp) notation meant a displacement of n bytes from sp independently of the type of instruction .b .w or .l and as in both cases the difference is 4 (10-6 and 8-4) this seems to true?
So why the move.w 6(sp),d0? as the return address on stack must be 4 bytes (as in move.l 4(sp),d0) ???

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Postby Nyh » Tue Jan 23, 2007 12:05 pm

DrCoolZic wrote:Bingo it works. You have to realize that it is the first lines of 68000 assembly code I ever wrote, and I fill a bit stupid.

Don't feel stupid. You are allowed to make your share of mistakes.
DrCoolZic wrote:Both solutions you provided works but I am a bit confuse.
I thought n(sp) notation meant a displacement of n bytes from sp independently of the type of instruction .b .w or .l and as in both cases the difference is 4 (10-6 and 8-4) this seems to true?
So why the move.w 6(sp),d0? as the return address on stack must be 4 bytes (as in move.l 4(sp),d0) ???

The stack will look like this:

Code: Select all

00    bit 24-31 return address
01    bit 16-23 return address
02    bit 08-15 return address
03    bit 00-07 return address
04    bit 24-31 parameter 1
05    bit 16-23 parameter 1
06    bit 08-15 parameter 1
07    bit 00-07 parameter 1
08    bit 24-31 parameter 2
09    bit 16-23 parameter 2
10    bit 08-15 parameter 2
11    bit 00-07 parameter2

We know both parameters are word (=2 bytes) sized but extended to a long (=4 bytes) on the stack. To fetch the parameter 1 you can fetch it as a word at 6(sp) , getting bits 00-15, or as a long at 4(sp), getting bits 00-31.

A good trick to see how things should be done is making a simple function in C, compile it and look at the disassembly in the debugger. Then you know where to find your parameters and the general setup of the function. Using this knowledge you can setup your own functions and try to be smarter as the compiler.

Hans Wessels

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Postby DrCoolZic » Tue Jan 23, 2007 1:00 pm

Thanks Hans. now it make sense. (Note I was used to Intel way of organizing the memory and it does not help)
About the debugger: There is a debugger in the Lattice enviroment Mon 3.1 from HiSoft. If I am right this is part of the Devpac package? I am looking for the documentation (again!) and found that apparently it is available on the sewer8 disk. However I have not been able to get at the documentation because when I try to load the image in steem it says something like wrong TOS version (and it also corrupted my steem installation http://www.atari-forum.com/viewtopic.php?t=10496) ! Do you know where I can find a text version somewhere (other than sewer8)?

In the Lattice environment I have been able to turn the debug flag during C compilation and I now have a source window in MonST that shows one of the source. Does not seems to useful (at lleast without doc) but I also have some debug information available (variables and entry points) ... But unfortunately I could not find a way to bring the asm debug information (like entry point) to MonSt?

I was also wandering if there was a flag in the C compiler that would allow to keep an intermediate translation from C to asm (like I use to have on Sun platform C compiler)? Could not find it for Lattice but it may be available in Pure C? or other C compiler for Atari? This would help me getting started.

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Postby Nyh » Tue Jan 23, 2007 1:39 pm

DrCoolZic wrote:In the Lattice environment I have been able to turn the debug flag during C compilation and I now have a source window in MonST that shows one of the source. Does not seems to useful (at lleast without doc) but I also have some debug information available (variables and entry points) ... But unfortunately I could not find a way to bring the asm debug information (like entry point) to MonSt?

Sorry, I can be of no help with MonST. My preffered tool is Pure C with Pure Debugger. The Pure debugger is brilliant IMHO. It shows C-code, disassembled C code, watches from variables and the current processor state (see picture). Also possible are memory dumps in ASCII, bytes, words or longwords. Very easy to control with the mouse or keyboard shortcuts.

For really heavy duty debugging I use Bugaboo.

DrCoolZic wrote:I was also wandering if there was a flag in the C compiler that would allow to keep an intermediate translation from C to asm (like I use to have on Sun platform C compiler)? Could not find it for Lattice but it may be available in Pure C? or other C compiler for Atari? This would help me getting started.

No, no intermediate assembly files from Pure C. To get the source with disassembly I misuse the Pure Profiler.

Hans Wessels
You do not have the required permissions to view the files attached to this post.

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Postby DrCoolZic » Tue Jan 23, 2007 2:24 pm

This looks very nice and powerful but again there is the problem of documentation. As far as I know there is no "electronic" documentation for Pure C? Otherwise Pure C seems to be an even better alternative than Lattice. (As you know from another thread I trying to get Lattice doc for scaning).

Based on your previous information and old doc on devpac I have been able to look at the disassembly of one C function...!!! not as nice as Pure C but informative anyway ...

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Postby Nyh » Tue Jan 23, 2007 3:15 pm

DrCoolZic wrote:This looks very nice and powerful but again there is the problem of documentation. As far as I know there is no "electronic" documentation for Pure C? Otherwise Pure C seems to be an even better alternative than Lattice. (As you know from another thread I trying to get Lattice doc for scaning).

Don't know about electronic English documentation. I rely heavily on the German help files included with Pure C. It is said English help files do exist. I don't know about them.

Hans Wessels

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Postby DrCoolZic » Tue Jan 23, 2007 3:47 pm

help files included with Pure C. It is said English help files do exist
I actually found them somewhere...
I did not had too much time to play with Pure C but it seems it deserve a good look!!!
I just installed the Pure C (I have version 1.1) and the English Help files (overwritten the german help) and they all seems to work...

As you seems to be Mr Pure C Here are the English help file if you want to try them?
You do not have the required permissions to view the files attached to this post.

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Postby DrCoolZic » Tue Jan 23, 2007 4:33 pm

Nyh wrote:No, no intermediate assembly files from Pure C. To get the source with disassembly I misuse the Pure Profiler
Could not found anything like a profiler in the distribution I have unless it is buried somewhere in the environment?

User avatar
[ProToS]
Moderator
Moderator
Posts: 2242
Joined: Fri Sep 20, 2002 2:09 am
Location: Lourdes / France
Contact:

Postby [ProToS] » Tue Jan 23, 2007 5:08 pm

DrCoolZic wrote:
help files included with Pure C. It is said English help files do exist
I actually found them somewhere...


you found this here I think ;)
http://www.atari-forum.com/viewtopic.php?p=46611#46611
SeeU
[ProToS]/Facebook

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Postby DrCoolZic » Tue Jan 23, 2007 5:18 pm

[ProToS] wrote:you found this here I think ;)
http://www.atari-forum.com/viewtopic.php?p=46611#46611
You are probably right as I got them from Atari-Forum

I tried to use the Pure C on my fdisk library and I have a huge problem! All my comments are done using // and it looks like Pure C does not like it! Would be a big effort to convert to /* xxxx */
Any flags to allow this ANSI feature?

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Postby Nyh » Tue Jan 23, 2007 6:01 pm

DrCoolZic wrote:I tried to use the Pure C on my fdisk library and I have a huge problem! All my comments are done using // and it looks like Pure C does not like it! Would be a big effort to convert to /* xxxx */
Any flags to allow this ANSI feature?

ANSI feature? AFAIK it is a C++ feature. Not an ANSI C feature. There is no Pure C flag to allow these comments. I think it is a search and destroy for you.

Hans Wessels

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Postby Nyh » Tue Jan 23, 2007 6:02 pm

DrCoolZic wrote:
Nyh wrote:No, no intermediate assembly files from Pure C. To get the source with disassembly I misuse the Pure Profiler
Could not found anything like a profiler in the distribution I have unless it is buried somewhere in the environment?

No, the profiler is a seperate program.

Hans Wessels

belboz
Captain Atari
Captain Atari
Posts: 164
Joined: Sat Aug 23, 2003 9:50 pm

Postby belboz » Tue Jan 23, 2007 7:43 pm

My scanner is acting up on the system it is connected to (haven't used it in a long time).

Since you wanted info on the C/assembly interfacing I took my digital camera and snapped some pics of the relevant section of the Lattice manual.

Hopefully they look ok.
You do not have the required permissions to view the files attached to this post.

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Postby DrCoolZic » Tue Jan 23, 2007 7:49 pm

Nyh wrote:ANSI feature? AFAIK it is a C++ feature. Not an ANSI C feature.
Yes I know you are right it is not part of the ANSI C but it has been available in so many C compilers and it is so much more practical that I would have thought it would be available in Pure C. Too bad ...

belboz
Captain Atari
Captain Atari
Posts: 164
Joined: Sat Aug 23, 2003 9:50 pm

Postby belboz » Tue Feb 20, 2007 4:04 pm

DrCoolZic,

Did the info I posted help you at all?

Curious if you got the information before the board went down, and if you have had time to use it.

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Postby DrCoolZic » Tue Feb 20, 2007 8:03 pm

Wonderful the forum is back !!!!!!
Yes I had time to get it, time to scan/enter/edit it, and yes plenty of time to use it !!!
It helped a lot - Thanks again.
You will find attached the Word version of the pictures.
Jean
You do not have the required permissions to view the files attached to this post.

belboz
Captain Atari
Captain Atari
Posts: 164
Joined: Sat Aug 23, 2003 9:50 pm

Postby belboz » Tue Feb 20, 2007 8:31 pm

Glad to hear it was some help.

Let me know if there is anything else you need from the manuals.

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Postby DrCoolZic » Sat Feb 24, 2007 9:05 pm

Again it has helped a lot for the lib I am writing.
See http://www.atari-forum.com/viewtopic.php?p=84289#84289

But still my dream is to get an electronic version of the doc. I am actually in discussion with someone to borrow his doc for me to scan.

What you have provided on C/ASM liking is a good example of a lot of information I could not have guessed without the doc.

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Postby DrCoolZic » Tue Feb 27, 2007 1:53 pm

belboz wrote:Let me know if there is anything else you need from the manuals.
Actually I would like to know if there is a directive that allow to align a varaible declaration.
I pass the address of a buffer declared as

Code: Select all

unsigned char* buffer[7000];

to the DMA chip and for performance reason this is fastest if the address is even.
Is there a way to force the buffer to be word align?
Thanks
Jean

Nyh if your still around is it possible with Pure C?

ijor
Hardware Guru
Hardware Guru
Posts: 3470
Joined: Sat May 29, 2004 7:52 pm
Contact:

Postby ijor » Tue Feb 27, 2007 5:23 pm

Allocate the buffer using a wider type, say:

Code: Select all

WORD buffer[ 3500];
BYTE *bufPtr;
bufPtr = (BYTE *) buffer;


Or use malloc, it is guaranteed to be aligned. Or just align it yourself, add one and clear bit 0.

belboz
Captain Atari
Captain Atari
Posts: 164
Joined: Sat Aug 23, 2003 9:50 pm

Postby belboz » Tue Feb 27, 2007 5:28 pm

I will take a look and see if I can find anything.

DrCoolZic wrote:Actually I would like to know if there is a directive that allow to align a varaible declaration.
I pass the address of a buffer declared as

Code: Select all

unsigned char* buffer[7000];

to the DMA chip and for performance reason this is fastest if the address is even.
Is there a way to force the buffer to be word align?
Thanks
Jean

Nyh if your still around is it possible with Pure C?


Social Media

     

Return to “C / PASCAL etc.”

Who is online

Users browsing this forum: No registered users and 3 guests