0% found this document useful (0 votes)
21 views19 pages

Microcontroller Module 3 Notes Assignment

The document outlines common portability issues encountered when writing C programs, particularly on ARM processors, and suggests mitigation strategies. Key issues include differences in char and int types, unaligned data pointers, endian assumptions, function prototyping, and the use of enumerations and inline assembly. The document emphasizes the importance of using proper data types, function prototypes, and memory alignment to enhance code portability across different architectures.

Uploaded by

jayeshsuthar0621
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views19 pages

Microcontroller Module 3 Notes Assignment

The document outlines common portability issues encountered when writing C programs, particularly on ARM processors, and suggests mitigation strategies. Key issues include differences in char and int types, unaligned data pointers, endian assumptions, function prototyping, and the use of enumerations and inline assembly. The document emphasizes the importance of using proper data types, function prototypes, and memory alignment to enhance code portability across different architectures.

Uploaded by

jayeshsuthar0621
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Portability Issues:

Or Define common probability issues when writing c program. How can thse
issues be mitigated.
1. The char type: On ARM processors, the char type is unsigned by default, while on many
other systems, it's signed. This can cause problems in loops. For example, if you use a char
variable like i as a loop counter and write a condition like i >= 0, the loop might never stop.
That’s because an unsigned char can’t be negative, so the condition is always true. To avoid
this, Instead of using char for counting, use an int or another signed type:
2. The int type: Some older architectures use a 16-bit int, which may cause problems when
moving to ARM's 32-bit int type. To avoid this issue Don’t assume int is a certain number of
bits. If your code requires a specific size, use int16_t, int32_t, etc.

3. Unaligned Data Pointers: Some processors allow you to read short or int values even if
they’re not stored at properly aligned memory [Link] on ARM processors, this
can cause errors or slower [Link] avoid this issue Use Properly Aligned Memory

4. Endian assumptions: C code may make assumptions about the endianness of a memory
system, for example, by casting a char * to an int *. To fix this issue Use Endian-Aware
Conversion Functions or Avoid Casting char* to int*

5. Function prototyping: On ARM, the armcc compiler automatically narrows function


arguments to match their declared type — for example, converting a larger type to a smaller
one like char or short. If you don’t use a proper function prototype, the compiler doesn’t
know what types the function expects. This can lead to wrong values being passed, and the
function may return incorrect results. To avoid this issue Always Declare Function
Prototypes and Place Function Declarations in Header Files.

6. Use of enumerations.: Although enum is portable, different compilers allocate different


numbers of bytes to an enum. The gec compiler will always allocate four bytes to an enum
type. The armce compiler will only allocate one byte if the enum takes only eight-bit values
To avoid this issue Explicitly Store Enum in a Fixed-Width Type or Use Fixed-Width Integer
Types Instead.

7. Inline assembly: Using inline assembly in C code reduces portability between architectures.
You should separate any inline assembly into small inlined functions that can easily be
replaced.

8. The volatile keyword: When you're working with memory-mapped hardware (like
registers or sensors) on ARM, you're directly accessing specific memory addresses. C
compilers try to optimize your code, and if it thinks a value hasn't changed, it might skip
reading or writing to that memory — which is bad for hardware control. To avoid this Use
with structs for register maps and Avoid using volatile for general variables
MODULE-3 Assignment questions
1. Explain The Differerent Data-types In C? provide example of how each
data type can be used in -C program. Or Explain local variable types used
in C .

2. Explain looping structure in C.(Fixed number, variable number and loop


unrolling)
OR

Explain how a compilers handles a for loop with variable iteration N , fixed
iteration and loop unrolling with an example.

3. Discuss with c function and target arm assembly code how optimization
can be done with respect to data types (answer is function argument type
in notes)
4. Discuss common probability issues when writing c program. How can
these issues be mitigated.

5. Explain pointer aliasing with Example.

6. How are function calls Handled efficiently in calling function in C

OR
How function calling is efficiently used by ARM through APCS with an
example program.
OR

Explain the process of a function call in C

7. How registers are allocated to optimize the program (Register allocation)?


Develop an assembly level program to find the sum of first two integers
numbers.

You might also like