Assembly and C can they live together?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Assembly and C can they live together?

ソリューションへジャンプ
4,340件の閲覧回数
Anastasios
Contributor I
Hello all (again :smileyhappy: ),

I want to write a function in c that looks like this

void defuzzification_function(const Fuzzy_Outputs *si,Fuzzy_Outputs *fi,const unsigned char n,unsigned short *result){

asm{
ldx si
ldy fi
ldab n
wav
ediv
sty result //PROBLEM HERE
}

};

And this is how i call her:

//the definitions of variables

const Fuzzy_outputs Output_Membership_Function={...};
Fuzzy_outputs FO;
const unsigned char outputlabels=5;
static unsigned short Qipercent;

void main(){
defuzzification_function(&Output_Membership_Function,&FO,outputlabels,&Qipercent);
}

logically the result of the ediv should be stored at Qipercent but this doesn't happen and Idon't know where it is stored. The generated assembly looks fine :

(Output_Membership_Function 35377
FO 2056
Qipercent 2071)

LDD #35377
PUSHD
LDX #2056
PUSHX
LDAB #5
PUSHB
LDD #2071
JSR SOMEPLACE

SOMEPLACE:
PUSHD
LDX 7,SP
LDY 5,SP
LDAB 4,SP
WAV
EDIV
STY 0,SP //this should do the job
LEAS 2,SP
RTS
--------
Any Ideas on what is happening here?

Thank you in advance

Anastasios
ラベル(1)
タグ(1)
0 件の賞賛
返信
1 解決策
1,678件の閲覧回数
CrasyCat
Specialist III

Hello

In fact the code you have provided is not correct. When mixing ANSI C and assembler or inline assembler you have to respect th eANSI C calling convention in your assembler source code. You find information around teh calling convention in the Manual_Compiler_HC12.pdf, chapter "Motorola HC12 Back End", section "Call Protocol and Calling Conventions".

Basically the last parameter is passed into register. You then have to save it on the stack and to make sure you do not loose it.

I would write the code as follows:

void defuzzification_function(const Fuzzy_Outputs *si,Fuzzy_Outputs *fi,const unsigned char n,unsigned short *result){

asm{
PSHD  ; Push result
ldx si
ldy fi
ldab n
wav
ediv
PULX  ; Pull result
sty 0,x
}

};

I hope this helps.

CrasyCat

元の投稿で解決策を見る

0 件の賞賛
返信
2 返答(返信)
1,679件の閲覧回数
CrasyCat
Specialist III

Hello

In fact the code you have provided is not correct. When mixing ANSI C and assembler or inline assembler you have to respect th eANSI C calling convention in your assembler source code. You find information around teh calling convention in the Manual_Compiler_HC12.pdf, chapter "Motorola HC12 Back End", section "Call Protocol and Calling Conventions".

Basically the last parameter is passed into register. You then have to save it on the stack and to make sure you do not loose it.

I would write the code as follows:

void defuzzification_function(const Fuzzy_Outputs *si,Fuzzy_Outputs *fi,const unsigned char n,unsigned short *result){

asm{
PSHD  ; Push result
ldx si
ldy fi
ldab n
wav
ediv
PULX  ; Pull result
sty 0,x
}

};

I hope this helps.

CrasyCat

0 件の賞賛
返信
1,678件の閲覧回数
Anastasios
Contributor I
It does not only help,it solves my problem : ).Thanks CrasyCat, it never crossed my mind to check with manual compiler hc12.

Regards

Anastasios
0 件の賞賛
返信