How to define a marco used assembly language in SGDK

SGDK only sub forum

Moderator: Stef

Post Reply
zhengyaxin_8bit
Very interested
Posts: 101
Joined: Thu Sep 04, 2008 2:57 am
Location: China

How to define a marco used assembly language in SGDK

Post by zhengyaxin_8bit » Fri Jun 29, 2012 3:09 pm

How to write these code in SGDK?
;*****************************************
SetA24IO MACRO BitValue
move.w #7,$a14406
move.w BitValue,$a14404
ENDM
;*****************************************

sasuke
Newbie
Posts: 7
Joined: Tue Jun 09, 2009 5:39 pm
Location: Nowhere, USA

Post by sasuke » Sun Jul 01, 2012 8:35 pm

Depends on if you are using C or assembly:

If in C:

Code: Select all

#include "genesis.h"
#define SetA24IO(bitValue) *(vu16*)0xa14406 = 7;\
                           *(vu16*)0xa14404 = (bitValue);
This does not always generate the same assembly as the asm macro, but has similar results. Note that genesis.h is required for the vu16 keyword.

For inline assembly (within C code):

Code: Select all

#define SetA24IO(bitValue) asm volatile ("move.w #7, 0xa14406\n\t"\
                                         "move.w %0, 0xa14404\n"\
                                         : :"g"(bitValue) : );
This generates the same assembly instructions as your macro. However, it is less readable to users who do not understand gcc's inline asm syntax. The "g" means that any general source operand can be used where there is a "%0" as the source operand. The volatile keyword here is to tell the compiler that we know what we are doing, and the compiler should not optimize the inline assembly.

And finally, in gas assembler:

Code: Select all

.macro SetA24IO BitValue 
   move.w #7, 0xa14406 
   move.w \BitValue, 0xa14404 
 .endm
Note that this can only be used in asm files, not C files.

P.S. Hi everyone. I have actually been a member here for about 3 years, but haven't found a reason to post yet until now. :D

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Sun Jul 01, 2012 9:49 pm

Seems you know a lot of stuff about GCC, i did not known the asm "volatile" to allow GCC optimization :)

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Mon Jul 02, 2012 2:36 am

Stef wrote:Seems you know a lot of stuff about GCC, i did not known the asm "volatile" to allow GCC optimization :)
It's not an optimization, it's an ANTI-optimization. It prevents gcc from changing your assembly thinking that the changes are better. You do this mainly for code that accesses hardware that NEEDS a particular order to the code enforced, like doing VDP commands. Otherwise it might change the order of stores to 0xC00004 and 0xC00000 since they are different locations and CLEARLY not linked. :wink: :lol:

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Mon Jul 02, 2012 2:45 pm

I misread, make more sense actually this way... I did not know that GCC could change your assembly code, i always though we were speaking about bounding C code optimization...

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Mon Jul 02, 2012 5:00 pm

Any inline assembly or assembly files has to be run through gas, and gas is allowed to make optimizations unless otherwise instructed not to. Nearly all gcc assembler backends at least do peephole optimization, and may do more extensive optimizations. The "volatile" keyword, when used with inline assembly, is your way in a C file to limit assembly optimizations that may be dangerous to the proper operation of the code. You also see people insert assembler directives in the code for other purposes. For example, MIPS inline assembly often starts with

Code: Select all

    asm volatile (".noat\n\t"
                  ".noreorder\n\t"
which instructs gas to not generate macro instructions that use the at register, and to not change the order of the instructions for delay slots.
Last edited by Chilly Willy on Mon Jul 02, 2012 5:02 pm, edited 2 times in total.

zhengyaxin_8bit
Very interested
Posts: 101
Joined: Thu Sep 04, 2008 2:57 am
Location: China

Post by zhengyaxin_8bit » Mon Jul 02, 2012 5:02 pm

Oh!Thanks stef , you help me again.
I few know the syntax of gcc and other tools in folder "\bin". Where I can find more information about them?

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Mon Jul 02, 2012 5:03 pm

zhengyaxin_8bit wrote:Oh!Thanks stef , you help me again.
I few know the syntax of gcc and other tools in folder "\bin". Where I can find more information about them?
http://sourceware.org/binutils/docs-2.22/

zhengyaxin_8bit
Very interested
Posts: 101
Joined: Thu Sep 04, 2008 2:57 am
Location: China

Post by zhengyaxin_8bit » Mon Jul 02, 2012 5:33 pm

I have other question.
Before, I use other compiler named "x68k".In this compiler,I can use local
symbol,like this:
;*********************************************
ClearScreenBuffer:
LEA.L ScreenBuffer,A0
LEA.L bg01_PNT_OFFSET,A1
MOVE.W #40*28-1,D0
?LOOP
MOVE.W (a1)+,(A0)+
DBF D0,?LOOP
RTS
;*********************************************
Clear_CRAM: ; CRAM ADDRESS 00000H--0007FH 128 BYTES
MOVE.W #$3F,D0 ;$3F=63
MOVE.L #$C00004,A0 ;A0=$C00004
MOVE.W #$8F02,(A0) ;INCREMENT VALUE=2(REG. #15)

MOVE.W #$C000,(A0)
MOVE.W #0,(A0) ;CRAM WRITE MODE
?LOOP
MOVE.W #0,$C00000
DBF D0,?LOOP
RTS
;******************************************************
There are two place defined "?LOOP"
In sgdk, Do they allow used like above?

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Mon Jul 02, 2012 6:40 pm

zhengyaxin_8bit wrote:Oh!Thanks stef , you help me again.
I few know the syntax of gcc and other tools in folder "\bin". Where I can find more information about them?
I guess you meant Chilly, i didn't any help there :p

zhengyaxin_8bit
Very interested
Posts: 101
Joined: Thu Sep 04, 2008 2:57 am
Location: China

Post by zhengyaxin_8bit » Tue Jul 03, 2012 1:46 am

Stef wrote:
zhengyaxin_8bit wrote:Oh!Thanks stef , you help me again.
I few know the syntax of gcc and other tools in folder "\bin". Where I can find more information about them?
I guess you meant Chilly, i didn't any help there :p
Oh! sasuke writed is right
.macro SetA24IO BitValue
move.w #7, 0xa14406
move.w \BitValue, 0xa14404
.endm :lol:

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Tue Jul 03, 2012 1:54 am

It's not a problem - we know what you meant. :D

As to local labels, gas uses a different form from most other assemblers. You'll find that discussed in the docs I linked to, but the most common local label used in gas tends to be of the form <number>: while the code refers to it via <number>f or <number>b depending on if the label <number> is later in the code (f = forward from your current position), or earlier in the code (b = backward from your current position). So you would do something like this

Code: Select all

    tst.w   d0
    beq.b   2f
    moveq   #10,d1
1:
    move.w  d0,(a0)+
    dbra    d1,1b
2:
    rts

Post Reply