News:

Let's find out together what makes a PIC Tick!

Main Menu

Just curious.

Started by david, Oct 29, 2023, 01:12 AM

Previous topic - Next topic

david

Hi All,
I decided to change a Word variable to a Sword variable but when I compiled it added 26 more program bytes.   When changing other Word variables in the same program some produced no change while others added less than 26 bytes.   Can someone explain how and where the extra bytes are used?

In the original code I had added a fixed value of 30,000 to the Word variable to cope with the odd time a counter may go below zero.  Effectively Word variable =30000 + (variable count of -50 to +4000).  That worked fine but made the some of the downstream maths a bit more complicated so I tried the SWord which then compiled even bigger.

Cheers,
David

trastikata

Quote from: david on Oct 29, 2023, 01:12 AMI decided to change a Word variable to a Sword variable but when I compiled it added 26 more program bytes.   When changing other Word variables in the same program some produced no change while others added less than 26 bytes.   Can someone explain how and where the extra bytes are used?

Hello david,

the extra bytes are from different math related assembler subs - word and signed word math will call different subs.

Quote from: david on Oct 29, 2023, 01:12 AMIn the original code I had added a fixed value of 30,000 to the Word variable to cope with the odd time a counter may go below zero.  Effectively Word variable =30000 + (variable count of -50 to +4000).  That worked fine but made the some of the downstream maths a bit more complicated so I tried the SWord which then compiled even bigger.

You might run in an overflow situation since signed words hold values from -32768 to + 32767 and 30000 +4000 is not in this range.

Regards

david

Hello trastikata,
Thank you for the reply.  Yes that makes sense  - I was just a bit surprised in the apparent overhead of the SWord in this particular case.

Sorry but I've managed to confuse you with my explanation of the use of the Word variable.  The actual value I have to deal with varies from about -50 to 4000 so rather than work with signed variables I thought I would just add an arbitrary offset to it of 30,000.  So my Word variable range is 29950 to 34,000.  That's when I thought it was a bit painfull to always subtract 30,000 from the value to get the real count value so I tried the SWord and removed the offset.

BTW - the reassigning of an unused UART rx pin is working out nicely and I've even had a little 12F1840 outputting serial at 230400 baud down about 200mm of ribbon cable.  Sending a 4 digit number, in Hex format, preceded by a starting letter ("A") takes about 180uS which I can output after each stepper pulse.

Cheers,
David

trastikata

Quote from: david on Oct 29, 2023, 10:46 AMBTW - the reassigning of an unused UART rx pin is working out nicely and I've even had a little 12F1840 outputting serial at 230400 baud down about 200mm of ribbon cable.  Sending a 4 digit number, in Hex format, preceded by a starting letter ("A") takes about 180uS which I can output after each stepper pulse.

Thanks for sharing david, it's good to know for future projects! This is forum is great, always can learn something new!

top204

#4
Signed variables, especially variables larger that 8-bits, usually take more mnemonics because they have to take the sign into account and not just a simple comparison or binary mathematic operations. This is true on all microprocessors and microcontrollers, unless they have some specialised sign capable mnemonics. There are a few exceptions where both variable types are the same or equals or not equals are compared, or a test for less than 0 can sometimes be a comparison of the sign bit of the 2s complement value. But they generally take more time and space.

That's why I do not understand when viewing C and C++ code written for a microcontroller, the uninitiated writers using the int directive for general purpose variables, that create a signed 16-bit variable, or a signed 32-bit variable on the larger devices. It is a total waste of both space and time on all microcontrollers when an unsigned variable will do the job.

david

Many thanks for the detailed explanation Les.  That's about all I need to know.  So adding an offset to a Word variable may be quicker than using the SWord.  I may try some tests to quantify the difference but I guess if it's more code it's generally going to be slower.

Cheers,
David