Assembly and C can they live together?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Assembly and C can they live together?

跳至解决方案
4,296 次查看
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,634 次查看
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,635 次查看
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,634 次查看
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 项奖励
回复