Quantcast
Channel: TI E2E support forums
Viewing all articles
Browse latest Browse all 4543

I have a capTIVAting secret - flex your ARM to find out what it is!

$
0
0

C.P. Ravikumar

Texas instruments

Assembly language programming on the TIVA launchpad? That certainly sounds like fun!

The TIVA launchpad is based on ARM Cortex-M4. Let us jump right into an example and examine the snippet  of an ARM assembly-language program. Look at the comments that appear after the semicolon on each line to understand what each of these instructions is doing. 

progstart

MOV R0, #0                 ; initialize R0 to 0

MOV R1, #1                 ; start with R1 = 1

loop

ADD R0, R1                 ; R0 = R0 + R1

ADD R1, #1                 ; R1 = R1 + 1

CMP R1, #10                ; Compare the value in R1 to 10

BNE loop                   ; Repeat if we have not got to 10 yet

progend

As you may have guessed by now, this program computes the sum 1+2+3+...+10.  Here is an exercise for you! Make suitable changes to this program to compute the sum of 10 terms in 3+5+7+9+... 

Back to the Launchpad

Code Composer Studio supports in-line assembly coding in a C program by supporting the "asm" function call. In other words, in a C program that you are writing,  if you place a statement such as

asm("\tMOV R0, #1");

the argument "\tMOV R0, #1" will be passed on to the ARM assembler, which interprets the line as an instruction "MOV R0, #1".  The tab character is needed to indicate that MOV is an opcode of the instruction; otherwise, the assembler will treat MOV to be a program label such as progstart in our example.

If we want to take the code snippet that I showed earlier and execute it on the TIVA launchpad, you have to write the following C program.

void main(void) {

asm ("progstart");

asm ("\tMOV R0, #0                 ; initialize R0 to 0");

asm ("\tMOV R1, #1                 ; start with R1 = 1");

asm ("loop");

asm ("\tADD R0, R1                 ; R0 = R0 + R1");

asm ("\tADD R1, #1                 ; R1 = R1 + 1");

asm ("\tCMP R1, #10                ; Compare the value in R1 to 10");

asm ("\tBNE loop                   ; Repeat if we have not got to 10 yet");

asm ("progend");

}


Then start Code Composer Studio and compile the program, download it into the launchpad, and run it in the single-step mode. The debugger can show you the contents of the registers if you wish to debug your code.

Can we avoid the drudgery?

If you were indeed going to develop a fairly lengthy assembly-language program for the ARM processor in the TIVA, you will groan at the thought of manually converting the assembly language program into the C program of the form shown above. Here is where you need to think of using some automation. Use Perl, Python, or good old C to take as input a file test.asm containing the assembly language program and spit out the C program.  

I hope you will have fun doing some assembly-language programming on the ARM and using some of the interesting instructions supported by the processor. Here is a little challenge which you can take on.  The following string contains a secret code in rot-13 encryption. As you will know, rot-13 encoding simply replaces an aplhabet with another alphabet that is 13 positions away - e.g. A will be replaced with N, B with O, C with P, N with A, etc.  You must write a simple assembly language program in ARM and decode the secret code. Assume that any character that is not alphabetic is left as is, without encoding.  You may assume that the string is terminated by a NULL. As a comment to this blog, send your code snippet and the decoded secret. The best entry will receive a prize!

Secret: YNHAPUCNQ YBIR


Viewing all articles
Browse latest Browse all 4543

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>