C

To watch a global variable in arm cortex debugging (explaining CISC and RISC)

To watch a global variable in arm cortex debugging:

1. As the variable is not local anymore we will not see the variable in local windows in the debugging mode

2. we need to click view and then click watch and in the watch window we need to add the variable that we want to watch.

for example for the code below:

int counter = 0;
int main(){

while (counter< 21){
counter++;
}
return 0;
}

to see counter we need to add watch window. now click the disassemble window to watch the instructions executing:

 CSIC and RISC-2

 

1. when LDR.N instruction executes: R0 register loads the “address” of the “counter” variable. 

CSIC and RISC

2. next LDR instruction loads R0 again but this time the “value” of the “address” R0 was holding loads to R0

3. next add instruction adds 1 to the value of R0 

4. next LDR.N instruction loads the “address” of the “counter” variable to R1

5. Next STR instruction store the “value” of “R0″ to the “address” which “R1″ is pointing to

CSIC and RISC-3

when this instruction executes > the value transfer to the address to counter. The way how arm processor works is an example of RISC = Reduced Instruction Set Computing comparing to CISC = Complex Instruction Set Computing like x86v in personal computer.

in RISC > memory can only read by “Load” instruction and all data manipulation occurs in the registers and then modified data stored back into the memory by special str / store instructions.  

Not all the data manipulation needs to happened in the register but can be happened in the memories directly.

C language can be used to hole this memory address by using a spacial variable called pointers.

so the above program can be written as:

int counter = 0;

int main(){
int *p_int;
p_int = &counter;
while (*p_int< 21){
(*p_int)++;
}
return 0;
}

CSIC and RISC-4

 

LDR.N is moved to main function and the 2nd LDR.N from the “without declaring pointer” code is removed from the assembly code, that increase the efficiency of the code and simplified the code. 

LinkedInFacebookGoogle+Twitter

set up and debug arm cortex board for c with IAR EWARM

Set Up:

Poject>options>

Device>select button; choose texasInstrument > LM4F > (LM4F1205QR for example)

C/C++ compiler > default is C

optimizations tab > optimization level is low

 

Tools> IDE Options > 

common fonts > choose fonts ; editor > indent = 4 (for example) ;

tab key functions = preferred “indent with space” instead of “indent with tab”

 

Debug

make sure that projections>options>debugger > driver > simulator

after pressing download and debug the program goes to Debug mode

1. View>Disassembly  2. View>memory 3. view>register 3. view>locals

to debug at each step at each line we need to press step into button.

when we press step into once at a time we can see that

>in the disassmebly window we can see that the corresponding assembly code and the machine instruction (hex numbers) and the location/memory address of the instructions (hex numbers)

>in the memory window the instructions and their memory address are shown more clearly ans sequential staring from zero (not following what instruction is the disassembly) arm cortex processor instructions occupies 2 bytes in the memory. 

>in the locals window we can see that all the variables, their values and their location registers (for example R1)    

>in the registers we can see that PC (Program Counter) contains the memory address of the instruction set and it’s changing for different instruction as the we move forward

also we can see that the value of the variable’s location registers (for example R1) in the locals window

 

in arm cortex M there are 16 this kind of registers. R0~R14 and the PC. All these registers can hold 32 bit numbers. M/C instruction can manipulate the registers in one clock cycle.  

 

 

 

 

 

 

 

LinkedInFacebookGoogle+Twitter

Parameter of main function: argc and argv

argc parameter is the count of total command line arguments passed to executable on execution (including name of executable as first argument). argv parameter is the array of character string of each command line argument passed to executable on execution.

Code:

#include <stdio.h>

int main (int argc, char *argv[]) {
int i=0;
printf(“cmd-line args count=%d\n”, argc);

for (i=0; i< argc; i++) {

printf(“arg%d=%s\n”, i, argv[i]);
}

return 0;
}

Result from Eclipse:

cmd-line args count=1

arg0=C:\Users\knsakib\workspace\test\Debug\test.exe

Result to run in console (if the file is test):

./test a1 b2 c3

cmd-line args count=4
a1
b2
c3

 

LinkedInFacebookGoogle+Twitter

Pointer and pointer function in C

Pointer is pointing to a memory address that contains some value. To demonstrate:

Code:

#include <stdio.h>

int main(void) {
int *k;
int n=8;

k=&n;
printf(“the address of n is: %x\n”, k);
printf(“the value of n is: %d\n”, n);
printf(“the value of n pointing to address of n is: %d\n\n”, *k);

return 0;
}

pointer

Result:

the address of n is: 28fee8
the value of n is: 8
the value of n pointing to address of n is: 8

 

Now after introducing another variable m=17, we can see that the variable is contained right next to the address where k’s address value is contained. to clarify:

Code:

#include <stdio.h>

int main(void) {
int *k;
int n=8;
int m=17;

k=&n;
printf(“the address of n is: %x\n”, k);
printf(“the value of n is: %d\n”, n);
printf(“the value of n pointing to address of n is: %d\n\n”, *k);

printf(“now the address of k (k contains the address of n): %x\n”, &k);
printf(“the address next to k’s address: %x\n”, k+1);
printf(“the address 2nd next to k’s address: %x\n”, k+2);
printf(“the value contained in the address 2nd next address to k’s address: %d\n”, *(k+2));
return 0;
}

pointer1

Result:

the address of n is: 28fee4
the value of n is: 8
the value of n pointing to address of n is: 8

now the address of k (k contains the address of n): 28fee8
the address next to k’s address: 28fee8
the address 2nd next to k’s address: 28feec
the value contained in the address 2nd next address to k’s address: 17

 

Functional Pointer:

To demonstrate functional pointer we can see the following recursive function to find factorial. Here pointer is defined by:

long (*func_pointer)(int);
func_pointer=&factorial;

Code:

#include<stdio.h>

long factorial(int);

int main()
{
int n;
long f;

setbuf(stdout, NULL);
printf(“Enter an integer to find factorial\n”);
scanf(“%d”, &n);

long (*func_pointer)(int);
func_pointer=&factorial;

if (n < 0)
printf(“Negative integers are not allowed.\n”);
else
{
f = func_pointer(n);
printf(“%d! = %ld\n”, n, f);
}

return 0;
}

long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}

Result:

Enter an integer to find factorial

4

4! = 24

 

 

LinkedInFacebookGoogle+Twitter