Assembly to C

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

  • : 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.
  • : 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.");
  • : 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?
  • : : 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.
  • : 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?
  • : : 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.
  • : 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.
  • : : 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 [email protected]

    Thanks,
    best regards,
    BoSCHoW.
  • : 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 [email protected]
    :
    : 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.
  • 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?
  • : 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?
    Well i am using codewarrior and hiwave compiler.
  • : : 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?
    : Well i am using codewarrior and hiwave compiler.
    :


    Then it will definitely not work.
  • : : : 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?
    : : Well i am using codewarrior and hiwave compiler.
    : :
    :
    :
    : Then it will definitely not work.
    I have one more question. What do i have to know before i start makeing a source file for nvm? Do you have any recomandations?

    Besr regards,
    BoSChoW.
  • : : Then it will definitely not work.
    : I have one more question. What do i have to know before i start
    : makeing a source file for nvm? Do you have any recomandations?
    :
    : Besr regards,
    : BoSChoW.
    :

    You'll obviously have to know C and the basics of the specific microcontroller. Then carefully read everything in detail in the eeprom or flash chapter of the manual. There is also app notes available to help out. The critical part is to get the prescaler right, the HCS12 nvm must be programmed with a clock between 150 to 200kHz or bad things might happen (most likely, the routines won't work, less likely, you'll damage the flash). In order to calculate the prescaler, you must know the frequency of the oscillator as well as the bus frequency. In the normal case for HCS12, bus freq = osc freq / 2. If you are using the internal PLL, things will get trickier.

    Then follow the state chart and formulas in the manual and it should work.
  • : 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.
    :
    Well, in the absence of context it is not easy to disassmeble this code, even assuming you know the processor. Who wrote the code as they should know what it is doing?
    If you don't know, then a literal disassembly by hand may well be wrong, but here goes (mind you my background is 6800/6802 not 6812):
    LDD 7,SP 'Load D (16-bit Accumulator) Indexed Indirect from the stack pointer (the 7 is probably an offset from the SP value).
    ADDD 2,SP 'Add D Indexed Indirect from the stack pointer (offset of 2)
    TFR D,X 'Transfer the result to the Index Register.
    LDD 0,X ' Load D with the value from the memory location in the Index Register, no offset
    STD 0,SP 'Store it in the location given by the stack pointer

    Mind you, as I said I am not familiar with anything near as new as this chip (but starting to update) after not doing 6800 assembly since 1985 or so. It seems highly odd to me that you would calculate an address from old stack values, grab that value then shove it on the stack; that is too dangerous for my style, but hey whatever thrills you.
Sign In or Register to comment.

Howdy, Stranger!

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

Categories