Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Arduino Mega2560 PORTA bits setting

Status
Not open for further replies.

imranahmed

Advanced Member level 3
Advanced Member level 3
Joined
Dec 4, 2011
Messages
817
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,493
Please let me how to write below two line of code into single line of code.
PORTA = PORTA & 0xF0;
PORTA = PORTA | (num & 0x0F);

Thanks.
 

Hi,

Why a single line only?
It won't change the resulting machine code.

Klaus
 
It only makes the source code one line shorter, the compiled program will be approximately the same and may even be bigger.

Brian.
Ok I want to make shorter code no matter for compiled size but thank you for tell me I did not know that.
 

Its an easy mistake to make.

The source code is read by the compiler and it converts it to machine code. The resulting machine code is a combination of simple instructions and library files linked together. A good compiler will look for things it can simplify, for example if part of a calculation always gives the same value it will use the result directly instead of calculating it each time. Often, if the same few lines of code are repeated several times they will automatically be placed in one subroutine and called several times instead of being inserted as duplicate instructions. The linking process will also try to fit blocks of code together to find a best fit in the processor memory so small changes may have no effect or cause a complete rearrangement of the memory blocks.

It follows that you can't assume fewer lines of source code will result in smaller executable programs.

In theory, you can write all programs in 'C' on a single line although I certainly wouldn't recommend trying ! Find the best trade off between size of source code and it being logically arranged and of course be readable.

Brian.
 
Its an easy mistake to make.

The source code is read by the compiler and it converts it to machine code. The resulting machine code is a combination of simple instructions and library files linked together. A good compiler will look for things it can simplify, for example if part of a calculation always gives the same value it will use the result directly instead of calculating it each time. Often, if the same few lines of code are repeated several times they will automatically be placed in one subroutine and called several times instead of being inserted as duplicate instructions. The linking process will also try to fit blocks of code together to find a best fit in the processor memory so small changes may have no effect or cause a complete rearrangement of the memory blocks.

It follows that you can't assume fewer lines of source code will result in smaller executable programs.

In theory, you can write all programs in 'C' on a single line although I certainly wouldn't recommend trying ! Find the best trade off between size of source code and it being logically arranged and of course be readable.

Brian.
Wow, I am thankful to you for providing me necessary knowledge about internal working processor.
One thing I want to know about learning about state machine programming of microcontrollers.
Easiest way to understand state machine theory?
 

State machines are just a different way of looking at a programming task and not all programs will benefit from it. The best resource for information is the Internet but to give you some idea:

Conventional programming assumes each step of the program makes a small change and those changes keep happening until you achieve the result you want. State machine programming looks at it in a different way, it defines different fixed outcomes and decides which one to use.

Brian.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top