Question: xxA2 * * C to assembler menu hook * */ #include #include #include #include common.h int add_test(int x, int y); void AddTest(int action) { if(action==CMD_SHORT_HELP)
xxA2 * * C to assembler menu hook * */ #include
xx_asm.s @ mycode.s : @ Test code for STM32 and linking assembly to C
@ This is a comment. Anything after an @ symbol is ignored. @@ This is also a comment. Some people use double @@ symbols.
.code 16 @ This directive selects the instruction set being generated. @ The value 16 selects Thumb, with the value 32 selecting ARM.
.text @ Tell the assembler that the upcoming section is to be considered @ assembly language instructions - Code section (text -> ROM)
@@ Function Header Block .align 2 @ Code alignment - 2^n alignment (n=2) @ This causes the assembler to use 4 byte alignment
.syntax unified @ Sets the instruction set to the new unified ARM + THUMB @ instructions. The default is divided (separate instruction sets)
.global mytest @ Make the symbol name for the function visible to the linker
.code 16 @ 16bit THUMB code (BOTH .code and .thumb_func are required) .thumb_func @ Specifies that the following symbol is the name of a THUMB @ encoded function. Necessary for interlinking between ARM and THUMB code.
.type mytest, %function @ Declares that mytest symbol is a function (not strictly required)
@ Function Declaration : int mytest(int x) @ @ Input: r0 (i.e. r0 holds x) @ Returns: r0 @
@ Here is the actual function mytest:
push {r4-r7, lr} @ Put aside registers we want to restore later
mov r0, #1 @ r0 holds our argument for the LED toggle function @ So pass it a value
bl BSP_LED_Toggle @ call BSP C function using Branch with Link (bl) ldr r1, =myTickCount ldr r0, [r1]
pop {r4-r7, lr} @ Bring all the register values back
bx lr @ Return (Branch eXchange) to the address held in the link register (lr)
.size mytest, .-mytest @@ - symbol size (not req)
@@ Function Header Block .align 2 @@ 2^n alignment (n=2) .syntax unified @@ Unified Syntax .global my_Tick @@ Expose my_Tick to the linker .code 16 @@ - 16bit THUMB code (BOTH are required!) .thumb_func @@ /
.type my_Tick, %function @@ - symbol type (not req)
@@ Declaration : void my_Tick( void ) @@ Uses nothing
my_Tick: push {lr}
ldr r1, =myTickCount @@ Address of myTickCount stored in r1 ldr r0, [r1] @@ Load r0 with the address pointed at by r1 (myTickCount address) add r0, r0, #1 @@ Increment r0 str r0, [r1] @@ Store the current r0 value back to the address pointed at by r1
pop {lr}
bx lr @@ Return to the address stored in lr
.size my_Tick, .-my_Tick @@ - symbol size (not req)
@@
@@
@@ Declaration : void my_Init( void ) @@ Uses nothing my_Init: bx lr @@push {lr} @@pop {pc} .size my_Init, .-my_Init @@ - symbol size (not req)
@ Program Data section - Items declared here are read/write
.data
.global myTickCount @ Expose the symbol to the linker
@ Actual declaration of the symbol myTickCount: .word 1 @ A 32-bit variable named myTickCount
@ Assembly file ended by single .end directive on its own line .end
Things past the end directive are not processed, as you can see here.
Modify both the .c and .s codes OBJECTIVES Use and understand our tools Begin actually writing some code TASKS CHANGES TO THE C CODE Modify the .c file as follows: Change the declaration for int add_test(int x, int y) to int xx_led_demo(int count, int delay) where xx is replaced by your initials (i.e. if your name is John Smith, it would be js_led_demo). Change the name of your C function (the one that you add to the menu) to xxA2, where xx corresponds to your initials (i.e. if your name is John Smith, it would be jsA2). Make sure you change the corresponding ADD_CMD macro at the bottom of the file. Modify the xxA2 function to retrieve two arguments from the user as shown in your lab count (uint32_t) and delay (uint32_t). Provide sensible defaults if the user does not provide one or both parameters. Change the user interface text to reflect the fact that we are now doing something other than adding numbers together. CHANGES TO THE ASSEMBLY CODE Modify the xx_asm.s code from your previous lab as follows: Change the name of the function add_test to xx_led_demo, corresponding to the change you made to the declaration in the C file. Be sure it has been changed everywhere it needed to be changed. Remove the old contents of add_test, replacing it with new code that does the following: 1. Toggles all 8 LEDs using a loop. To make your life easier, they do not need to be in order. 2. Delays between LEDs toggling according to the delay passed by the user. 3. Repeats this cycle the number of times specified by the user. REQUIREMENTS 1. Your xx_hook.c and xx_asm.s code as taken directly from the drop box compiles. 2. Your code deploys with sudo make program. 3. Your code runs with no parameters provided (sensible defaults). 4. Your code runs with only a count provided (and that count is used correctly). 5. Your code runs with both a count and a delay value provided (and those values are used correctly). PROPER USE OF REGISTERS, THE STACK, AND LINK REGISTER Only necessary items (no more, no less) are pushed onto the stack. Your function calls use the link register appropriately CODE COMMENTS As this is assembly language, you must extensively comment any code that is unclear (i.e. nearly every line).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
