C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Assembly to C Posted by boschow on 11 Jun 2007 at 6:46 AM
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 ?
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;
}

Thx and best regards,
BoSCHoW.
Report
Re: Assembly to C Posted by IDK on 11 Jun 2007 at 9:06 AM
: 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 ?
:
: 
: 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;
: }
: 
:
: 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.
Report
Re: Assembly to C Posted by Jonathan on 12 Jun 2007 at 1:28 AM
: 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%2520Instruction%2520Set.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.");
Report
Re: Assembly to C Posted by Lundin on 12 Jun 2007 at 1:40 AM
: 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?
Report
Re: Assembly to C Posted by boschow on 12 Jun 2007 at 8:03 AM
: : 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.
Report
Re: Assembly to C Posted by Lundin on 13 Jun 2007 at 12:28 AM
: 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?
Report
Re: Assembly to C Posted by boschow on 15 Jun 2007 at 7:15 AM
: : 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.
Report
Re: Assembly to C Posted by Lundin on 17 Jun 2007 at 11:41 PM
: 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.
Report
Re: Assembly to C Posted by boschow on 18 Jun 2007 at 3:19 AM
: : 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.
Report
Re: Assembly to C Posted by Lundin on 18 Jun 2007 at 4:32 AM
: 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.
Report
Re: Assembly to C Posted by Lundin on 18 Jun 2007 at 4:36 AM
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?
Report
Re: Assembly to C Posted by boschow on 18 Jun 2007 at 9:57 AM
: 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.
Report
Re: Assembly to C Posted by Lundin on 18 Jun 2007 at 11:20 PM
: : 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.
Report
Re: Assembly to C Posted by boschow on 19 Jun 2007 at 1:10 AM
: : : 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.
Report
Re: Assembly to C Posted by Lundin on 19 Jun 2007 at 4:58 AM
: : 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.
Report
Re: Assembly to C Posted by totoaus on 26 Jun 2007 at 5:59 AM
: 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 ?
:
: 
: 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;
: }
: 
:
: 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.
Report
Re: Assembly to C Posted by totoaus on 26 Jun 2007 at 6:05 AM
: : 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 ?
: :
: : 
: : 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;
: : }
: : 
: :
: : 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.

P.S. I should have added that you need to get the right resources for the CPU chip. Download from freescale.com the CPU12RM and CPU12RG/D, they're PDF and free. That'll make you more independent as well.
It would also help in future if you provided more information. As I said, code without context is risky to work with. Almost all of the traffic on your question has been trying to identify the CPU and gain the required information to help you.
Report
Re: Assembly to C Posted by Lundin on 26 Jun 2007 at 6:38 AM
You translate the code correctly, and that is why I said that it would definitely not work - you don't know where on the stack the compiler has placed the variables. To fiddle around with the variables placed on the stack by the compiler is to ask for trouble. In safety-critical systems, it is completely unacceptable.

In this case the compiler is Codewarrior for HCS12, it will place the function parameters on the stack or in accumulators depending on their types. The last parameter in the list will always end up in an accumulator. There is an app not for Codewarrior describing this.

Note that this is HCS12 and not HC12, the latter is obsolete, although the assemble is code-compatible with HCS12.

To the OP: I can help, I have worked pretty much non-stop with this particular MCU and compiler/debugger since it was released five years ago. But I won't provide any complete solution without any effort made by the poster. As mentioned before, the code posted is really just nonsense and has nothing to do with nvm programming.

It goes without saying that you need to get the manual and read the whole of it. The HCS12 is a particulary complicated MCU with 800 or so registers, banked memory and complex peripherals. If you just grab it and start programming without knowing the chip... best of luck with that.
Report
Re: Assembly to C Posted by totoaus on 26 Jun 2007 at 7:22 AM
: You translate the code correctly, and that is why I said that it
: would definitely not work - you don't know where on the stack the
: compiler has placed the variables. To fiddle around with the
: variables placed on the stack by the compiler is to ask for trouble.
: In safety-critical systems, it is completely unacceptable.
:
: In this case the compiler is Codewarrior for HCS12, it will place
: the function parameters on the stack or in accumulators depending on
: their types. The last parameter in the list will always end up in an
: accumulator. There is an app not for Codewarrior describing this.
:
: Note that this is HCS12 and not HC12, the latter is obsolete,
: although the assemble is code-compatible with HCS12.
:
: To the OP: I can help, I have worked pretty much non-stop with this
: particular MCU and compiler/debugger since it was released five
: years ago. But I won't provide any complete solution without any
: effort made by the poster. As mentioned before, the code posted is
: really just nonsense and has nothing to do with nvm programming.
:
: It goes without saying that you need to get the manual and read the
: whole of it. The HCS12 is a particulary complicated MCU with 800 or
: so registers, banked memory and complex peripherals. If you just
: grab it and start programming without knowing the chip... best of
: luck with that.

1. Thanks for your comments, I wasn't sure of the translation, given lack of experience with the HCS 12 and time away from this stuff.
2. I also didn't mention that I have only ever used hand assembly of small programs, so Code Warrior, et al are foreign to me.
3. I found the posting on Google while searching for something else and only signed and and replied because it got me curious.
4. Thanks for clearing explaining what bothered me but evaded clear explanation at the time: the risks of unknown stack alignment.
5. The only thing that makes sense about this code is an academic exercise: a lecturer requiring students to convert this code. It only occurs to me but I have just had another close look at Ron Bishop's 1979 book: Basic Microprocessors and the 6800. Much of that code seems senseless in any useful manner.
6. It's after midnight and I have the flu, better try and sleep now.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.