Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Assembly to C

boschowboschow Posts: 6Member
Hi all,
i have a piece of program written in assembly. I have to rewrite it to C, but i dont know how, can somebody help me with it ?
[code]
unsigned int iz32prvi( //read word from nonvolatile data buffer
TNVVar *nvv, // pointer to nonvolatile memory
UINT16 dat) // address of doulbe word in nonvolatile memory
{
UINT16 vh1 = dat;
unsigned int bla=0;
asm {
LDD 7,SP
ADDD 2,SP
TFR D,X
LDD 0,X
STD 0,SP
}

return bla;
}
[/code]
Thx and best regards,
BoSCHoW.
«1

Comments

  • IDKIDK Posts: 1,784Member
    : Hi all,
    : i have a piece of program written in assembly. I have to rewrite it
    : to C, but i dont know how, can somebody help me with it ?
    : [code]:
    : unsigned int iz32prvi( //read word from nonvolatile data buffer
    : TNVVar *nvv, // pointer to nonvolatile memory
    : UINT16 dat) // address of doulbe word in nonvolatile memory
    : {
    : UINT16 vh1 = dat;
    : unsigned int bla=0;
    : asm {
    : LDD 7,SP
    : ADDD 2,SP
    : TFR D,X
    : LDD 0,X
    : STD 0,SP
    : }
    :
    : return bla;
    : }
    : [/code]:
    : Thx and best regards,
    : BoSCHoW.
    :

    Wow, I've never seen any assembly like that... What platform is it?

    I don't know what TFR and ADDD do, but would it be easier to just rewrite what the code is supposed to do?

    Some things can't be done in C, and this seems to be such a situation.

    It could maybe be replaced by return nvv[dat];.

    I'm pretty sure that this is a situation where you'll need to use assembly.
  • JonathanJonathan Posts: 2,914Member
    : Wow, I've never seen any assembly like that... What platform is it?
    :
    Appears to be Motorola 6811. Here's a handy instruction reference.

    http://66.102.9.104/search?q=cache:DTkzKefIq6cJ:www.engineer.tamuk.edu/CLeung/EEEN3449/HCS12%20Instruction%20Set.pdf+Motorola+6811+assembly&hl=en&ct=clnk&cd=6&gl=uk&client=firefox-a

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
  • LundinLundin Posts: 3,711Member
    : Wow, I've never seen any assembly like that... What platform is it?
    :
    : I don't know what TFR and ADDD do, but would it be easier to just
    : rewrite what the code is supposed to do?
    :
    : Some things can't be done in C, and this seems to be such a
    : situation.
    :
    : It could maybe be replaced by return nvv[dat];.
    :
    : I'm pretty sure that this is a situation where you'll need to use
    : assembly.


    It is Motorola/Freescale 6800 assembler. TFR transfers data from one register to another, ADDD adds two integer values together and places them in accumulator D. There is nothing in the code that can't be rewritten in C.

    And yeah, it would be easier to rewrite it. First of all, you don't know if the compiler will place the local variables on the stack or in accumulators, nor in which accumulator, so that code looks dangerous.

    The code doesn't make much sense. Now if you haven't given the function a nonsense name like "iz32prvi" one could actually have guessed what it is supposed to do...

    If you simply wish to read from nvm that requires nothing fancy, you can typecast between integers and pointers to get absolute addresses. Perhaps you can tell us what you want the code to do?
  • boschowboschow Posts: 6Member
    : : Wow, I've never seen any assembly like that... What platform is it?
    : :
    : : I don't know what TFR and ADDD do, but would it be easier to just
    : : rewrite what the code is supposed to do?
    : :
    : : Some things can't be done in C, and this seems to be such a
    : : situation.
    : :
    : : It could maybe be replaced by return nvv[dat];.
    : :
    : : I'm pretty sure that this is a situation where you'll need to use
    : : assembly.
    :
    :
    : It is Motorola/Freescale 6800 assembler. TFR transfers data from one
    : register to another, ADDD adds two integer values together and
    : places them in accumulator D. There is nothing in the code that
    : can't be rewritten in C.
    :
    : And yeah, it would be easier to rewrite it. First of all, you don't
    : know if the compiler will place the local variables on the stack or
    : in accumulators, nor in which accumulator, so that code looks
    : dangerous.
    :
    : The code doesn't make much sense. Now if you haven't given the
    : function a nonsense name like "iz32prvi" one could actually have
    : guessed what it is supposed to do...
    :
    : If you simply wish to read from nvm that requires nothing fancy, you
    : can typecast between integers and pointers to get absolute
    : addresses. Perhaps you can tell us what you want the code to do?

    This function is used for transfering variables from MODBUS to ROM. Iz means "from32first". There are also other similar functions for coils, words and double words, but i guess if one can be compailed into c, then all of them can be compailed to c. Our programer said that he dosent know how to rewrite the piece of code from assembler to c.
  • LundinLundin Posts: 3,711Member
    : This function is used for transfering variables from MODBUS to ROM.
    : Iz means "from32first". There are also other similar functions for
    : coils, words and double words, but i guess if one can be compailed
    : into c, then all of them can be compailed to c. Our programer said
    : that he dosent know how to rewrite the piece of code from assembler
    : to c.
    :


    When writing to nvm, the only thing you have to do in asm is to set/clear the interrupt flag in the condition code register, in case it is needed. Everything else can be done in C. Why doesn't he know how to rewrite it, because he doesn't know C or because there is some hardware-related issue that can't be done in C?
  • boschowboschow Posts: 6Member
    : : This function is used for transfering variables from MODBUS to ROM.
    : : Iz means "from32first". There are also other similar functions for
    : : coils, words and double words, but i guess if one can be compailed
    : : into c, then all of them can be compailed to c. Our programer said
    : : that he dosent know how to rewrite the piece of code from assembler
    : : to c.
    : :
    :
    :
    : When writing to nvm, the only thing you have to do in asm is to
    : set/clear the interrupt flag in the condition code register, in case
    : it is needed. Everything else can be done in C. Why doesn't he know
    : how to rewrite it, because he doesn't know C or because there is
    : some hardware-related issue that can't be done in C?

    Well when i asked him why he didnt write this part of the program in c he said is because this is quite a delicate thing and that he trided to do it but when he compailed the code in c he check it with the debugger and the results werent the same as the code written assembley. That is why he choose to write this part of the code in assembley. And because i dont know how to write the code from assebley to c i asked you guys to give me a hands and now things are more clear for me ... Thank you.
  • LundinLundin Posts: 3,711Member
    : Well when i asked him why he didnt write this part of the program in
    : c he said is because this is quite a delicate thing and that he
    : trided to do it but when he compailed the code in c he check it with
    : the debugger and the results werent the same as the code written
    : assembley. That is why he choose to write this part of the code in
    : assembley. And because i dont know how to write the code from
    : assebley to c i asked you guys to give me a hands and now things are
    : more clear for me ... Thank you.
    :


    Out of curiousity, which platform is this for? It is not a "delicate thing" for any modern Motorola/Freescale processor, I have nvm code for several of their microcontrollers and all of it is purely C, apart from the "CLI" and "SEI" interrupt-flag instructions that can't be done in C.

    Most of said processors don't like to get interrupted during programming. If you for example single-step through the nvm routine with a debugger, chances are that it will screw up the nvm-programming. But this is naturally very much cpu-dependant.
  • boschowboschow Posts: 6Member
    : : Well when i asked him why he didnt write this part of the program in
    : : c he said is because this is quite a delicate thing and that he
    : : trided to do it but when he compailed the code in c he check it with
    : : the debugger and the results werent the same as the code written
    : : assembley. That is why he choose to write this part of the code in
    : : assembley. And because i dont know how to write the code from
    : : assebley to c i asked you guys to give me a hands and now things are
    : : more clear for me ... Thank you.
    : :
    :
    :
    : Out of curiousity, which platform is this for? It is not a "delicate
    : thing" for any modern Motorola/Freescale processor, I have nvm code
    : for several of their microcontrollers and all of it is purely C,
    : apart from the "CLI" and "SEI" interrupt-flag instructions that
    : can't be done in C.
    :
    : Most of said processors don't like to get interrupted during
    : programming. If you for example single-step through the nvm routine
    : with a debugger, chances are that it will screw up the
    : nvm-programming. But this is naturally very much cpu-dependant.

    We use the HCS12 motorola processor. If i am not mistaken our platform is MC9S12NE64. If there is a posibility that your nvm source in c could work with our type of processor can you send it to my e-mail address, which is boschow2003@yahoo.com.

    Thanks,
    best regards,
    BoSCHoW.
  • LundinLundin Posts: 3,711Member
    : We use the HCS12 motorola processor. If i am not mistaken our
    : platform is MC9S12NE64. If there is a posibility that your nvm
    : source in c could work with our type of processor can you send it to
    : my e-mail address, which is boschow2003@yahoo.com.
    :
    : Thanks,
    : best regards,
    : BoSCHoW.
    :


    HCS12 happens to be the MCU I'm most familiar with of them all. :-)
    Yes, I have written pure C code both for eeprom and flash programming on that particular MCU, nothing needs to be in assembler. Unfortunately, all my code written for it is owned by my employer and I can't share it.

    That code you posted in itself has nothing to do with nvm handling on HCS12, it is just formatting data and isn't critical at all. Writing/erasing eeprom or flash is handled in pretty much the same way:

    - Clear error flags
    - Write data to the address
    - Write a command to a register, write/erase etc
    - Check error flags
    - If everything is ok, execute the command
    - Wait til the command is executed by checking a flag

    Freescale has the full C source for writing to flash available in some app note on their homepage. For the smaller HCS12 derivates such as NE64, you need to run the flash programming code from RAM or it won't work, but it is all there in their app note. If you are doing eeprom programming, you'll however have to write it by yourself.
  • LundinLundin Posts: 3,711Member
    Oh and btw, that code will certainly not work on HCS12 with any compiler I know, since you don't know where the local variables will end up, as I mentioned in an earlier reply.

    Which compiler are you using?
«1
Sign In or Register to comment.