Needing help with my Arm Assembly Embedded Systems Assignment

P R O G 1 3 6 0 L A B 4 – W R I T I N G C O D E A N D U S I N G G D B O B J E C T I V E S  Write some ARM Assembly  Experiment with GDB  Give you the foundation for your next assignment PL EASE N OT E: YOU AR E R ESPON SIB L E FOR TH E CON TEN T OF TH IS L AB , FOR B OT H T EST S AN D YOUR ASSIGN M EN TS. M A J O R T A S K S B U I L D I N G O N P R E V I O U S L A B S This lab will continue on the foundation of the labs we have done so far. Also note that it is expected that you are able to use the techniques from the labs on your assignments without assistance. U S I N G G D B While the GDB de bugger is nowhere near as friendly as Visual Studio, it is still an essential tool for debugging problems in your code. W R I T I N G S O M E C O D E Your next assignment requires that you write some code. This lab lays the foundation. G D B E S S E N T I A L S You have already previously used GDB to set and clear breakpoints in your code. This portion of the lab builds on that, showing you more of th e features of GDB that you can use in your debugging. I N T E R A C T I N G W I T H G D B CL O SI NG GD B Note – do not close the terminal window to c lose GDB, you may end up with orphaned processes preventing you from continuing to debug. To close GDB, type “q” at the (gdb) prompt and the program will ask you to exit. If your program is running, hit CTRL -C to break execution. L A Y O U T S GDB has several di fferent layouts that allow you to see different information about the state of your code. layout src Display the source window only. layout asm Display the assembly window only. layout split Display the source and assembly window. layout regs Display the register window together with the source or assembly window. More information about the GDB display commands can be found at https://sourceware.org/gdb/onlinedocs/gdb/TUI -Comma nds.html . I N T E R A C T I N G W I T H Y O U R P R O G R A M Here are some useful commands for interacting with your program. backtrace Show the call stack that leads to the current execution point. MANI P UL ATI NG YO U REG I STE R S You can see the contents of an individual register by prefixing the name with $ and using the print command: print $r0 You can see the contents of a register after each time you step using the display command display $r0 You can set the contents of an individual register using the set command: set $r9 = 12 3 Additional GDB manipulation commands can be found here: http://darkdust.net/files/GDB%20Cheat%20Sheet.pdf G D B – Y O U R T U R N Debug the sample code you have from last lab (including your add_t est function). Set a breakpoint inside your assembly code, trigger the breakpoint, and experiment with GDB layouts. Use each of the commands shown in the GDB section, and answer for yourself: 1. How can I show the registers, source code, and gdb prompt all at the same time? 2. How can I show the call stack for where I am in the code? 3. If I want to see the contents of r4, what are three ways I can do it? What is the difference between them? 4. How can I see the new value of r0 every time I step in my code? 5. How can I set the value of r0 to 0xFF from the gdb prompt? L E T ’ S W R I T E S O ME C O D E Up until now, you have not had to write any code of your own, but have been asked to experiment with or use existing code. No w, it is time for you to start writing some code of your own . T H E F O U N D A T I O N We will be using the test code from the previous lab, and experimenting with it to see what happens. A S I M P L E D E L A Y F U N C T I O N You will need to use a simple delay in this lab. Here is a basic function that will implement a busy waiting delay for you. (Read the Wikipedia article ab out Busy Waiting to see why we’re calling this a busy waiting delay . While you’re at it, can you identify why busy waiting is a bad idea? ) @ Function Declaration : int busy_ delay(int cycles) @ @ Input: r0 (i.e. r0 holds number of cycles to delay) @ Returns: r0 @ @ Here is the actual function busy _delay: push {r4} mov r4, r0 delay_loop: subs r4, r4, #1 bgt delay_loop mov r0, #0 @ Return zero (always successful) pop {r4} bx lr @ Return (Branch eXchange) to the address in the link register (lr) T A S K : A D D A D E L A Y In your assembly code from last week, add a delay just before the function returns by calling the function add_delay with a constant value of 0xFFFFFF . This should be long enough that when you call the function you can see that the delay is actually working. QUE STI O NS: 1. How do you load a 32 -bit constant v alue in ARM? (See the slides) 2. Is the result of your addition still working? T A S K : M A K E T H E D E L A Y V A R I A B L E The sample code and serial interface we are using allows us to retrieve parameters from our commands. The library function fe tch_uint32_arg will fetch arguments from our commands, in order. We will use this to make our delay variable. uint32_t delay; int fetch_status; fetch_status = fetch_uint32_arg(&delay); if(fetch_status) { // Use a default delay value delay = 0xFFFFFF; } // Whe n we call our function, pass the delay value. Now it’s your turn – our addition function currently takes two values – x and y – you now have to pass the delay argument to the function. QUE STI O NS 1. How do you pass the delay argument to the add_test function? 2. How does your assembly code retrieve that argument? 3. How does your assembly code pass the variable length delay to the busy_delay function? T A S K : T O G G L E T H E L E D Call the BSP_LED_Toggle function just before you call your delay. QUE STI O NS 1. How do you tell the function which LED to toggle? 2. Does your delay still work? Is it the right length? T A S K : B L I N K T H E L E D MU L T I P L E T I M E S Here’s where we start to make things interesting. Write a loop to toggle the LED on and off 10 times. After that, make the nu mber of times it blinks an adjustable parameter as well. QUE STI O NS 1. We are now passing 4 parameters (x, y, delay, count) to our function. What’s the significance of that? What if we want to pas s a fifth? 2. Is it easier to count up or count down when implementing a loop? TA S K : B L I N K T H E L E D S I N S E Q UE N C E Can you make all 8 LEDs blink sequentially using a loop? How is this different than blinking the LEDs using the Tick handler? Which way is the “right” way to do this?