This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

memory problem

hello everyone,
i am writing program in 'c'

i have bcd to hex routine in which i am using 2 variable of unsigned int data type initialised to '0' and '1000' resp
eg: unsigned int a=0;

program compiles well and downloads in chip of c8051f020

but when i debug, one of the variables contents remain unchanged

i checked the ram location where it is stored,ram location contents are not changed by statement

why this is happening,

data declared in the function is data i.e stored in genereal purpose ram

in c memory, register are all taken care by compiler but my locations are affecting

i have bcd to hex routine in one '.c' file and calling is in another '.c' file

is it creating a problem? but i have many such files
and they are not affecting

even i checked stack also but nothing i could make out

thanx to all
take care

Parents
  • You have the comment "//code of conversion" but no conversion code can be found. Did you forget to write any code?

    If you convert 255 to 0x255 then you are converting decimal to BCD, not BCD to hex.

    Note that a conversion BCD to hex is not meaningful. Hex is a numeric base, not a numeric format. It is just a way of _presenting_ the data. Or was the intention to generate four ASCII characters in the range '0'..'9','A'..'F', i.e. to generate four bytes of hex output? But you can't return such information in your function - four characters can't fit in a unsigned int.

    To convert from BCD to a "normal" number (for later presentation with printf() in decimal with %d or hexadecimal with %x) you can do:

    Use a mask 0x000f. Use a multiplier 1.
    Set result to zero.
    While the number is non-zero:
    - Add number bit-and mask multiplied by multiplier to result.
    - multiply multiplier by ten.
    - shift number right four steps (i.e. dividing by 16)
    Return the result.

    But remember: I don't really know exactly what you do want to do, since the name of your function doesn't seem to match your described intention. And the reason your function does not work is because it does not contain any conversion code at all.

    Walk through your input number. Add to

Reply
  • You have the comment "//code of conversion" but no conversion code can be found. Did you forget to write any code?

    If you convert 255 to 0x255 then you are converting decimal to BCD, not BCD to hex.

    Note that a conversion BCD to hex is not meaningful. Hex is a numeric base, not a numeric format. It is just a way of _presenting_ the data. Or was the intention to generate four ASCII characters in the range '0'..'9','A'..'F', i.e. to generate four bytes of hex output? But you can't return such information in your function - four characters can't fit in a unsigned int.

    To convert from BCD to a "normal" number (for later presentation with printf() in decimal with %d or hexadecimal with %x) you can do:

    Use a mask 0x000f. Use a multiplier 1.
    Set result to zero.
    While the number is non-zero:
    - Add number bit-and mask multiplied by multiplier to result.
    - multiply multiplier by ten.
    - shift number right four steps (i.e. dividing by 16)
    Return the result.

    But remember: I don't really know exactly what you do want to do, since the name of your function doesn't seem to match your described intention. And the reason your function does not work is because it does not contain any conversion code at all.

    Walk through your input number. Add to

Children
  • "[...] stored,ram location contents are not changed by statement"

    Your code basically looks like (after stripping away meaningless code that does not give any side effects):

    unsigned int Bcd4toHex4(unsigned int a) {
        return 0;
    }
    


    So, why should the compiler change any contents of any memory area? You must _do_ something in your function, to get the compiler to bother to generate any code.