Advertisement
Guest User

assign3

a guest
Apr 12th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 4.43 KB | None | 0 0
  1. @ Function Declaration : int mg_game_demo(int count, int delay)
  2. @
  3. @ Input: r0, r1, r2 (i.e. r0 holds delay, r1 holds pattern, r2 holds target)
  4. @ Returns: 0
  5. @
  6.  
  7. @ Here is the actual function
  8. mg_game_demo:
  9.  
  10.     push {r4-r9, lr}                 @ Put aside registers we want to restore later
  11.     mov r4, r0                       @ Stores the value of delay into r4
  12.     mov r5, r1                       @ Stores the value of pattern into r5
  13.     mov r6, r2                       @ Stores the value of target into r6
  14.    
  15.     reset_pattern:                   @ Resets the LED counter so you can do led_toggle_loop from scratch
  16.     mov r7, r5                       @ r7 now holds the address of the value of the 1st LED to be toggled
  17.     mov r9, #7                       @ r9 acts like a down counter of times left to loop
  18.    
  19.    
  20.     led_toggle_loop:                 @ Toggle each LED once
  21.    
  22.         ldrb r8, [r7], #1            @ Load LED number(as an 8bit value, so only one char) into r8 add skip to next LED number
  23.         sub r8, #48                  @ Convert ASCII value to decimal number
  24.  
  25.         mov r0, r8                   @ r0 now holds the number of LED to be toggled. Will be passed as a parameter
  26.         bl BSP_LED_Toggle            @ call BSP C function using Branch with Link (bl)
  27.  
  28.         mov r0, r4                   @ r0 now holds the delay time, to be used in check_button_delay
  29.         mov r1, r6                   @ r1 now holds the target LED value, to be used in check_button_delay
  30.         mov r2, r8                   @ r2 now holds the ON LED value, to be used in check_button_delay
  31.         bl check_button_delay        @ Calls busy_delay with r0,r1 and r2 as its arguments
  32.         beq win                      @ If function returns 0, user pressed the button at the RIGHT time
  33.         bmi lose                     @ If function returns -1, user pressed the button at the WRONG time
  34.  
  35.         mov r0,r8                    @ r0 now holds the number of LED to be toggled. Will be passed as a parameter
  36.         bl   BSP_LED_Toggle          @ call BSP C function using Branch with Link (bl)
  37.  
  38.         subs r9, #1                  @ Count down until all the LEDs have been toggled
  39.         bpl led_toggle_loop          @ Loop until all LEDs are toggled    
  40.  
  41.     b reset_pattern                  @ Loop until we user presses the button
  42.  
  43.  
  44.     win:                             @ Toggle all LED at the same time twice
  45.        
  46.         mov r0, r8                   @ r0 grabs the value of the LED currently ON...
  47.         bl BSP_LED_Toggle            @ and here we turn it OFF
  48.  
  49.         mov r5, #3                   @ Store in r5 the number of times to toggle all LEDs
  50.  
  51.     reset_leds:
  52.        
  53.         mov r6, #7                   @ Store in r6 the total number of LEDs to be toggled
  54.  
  55.         toggle_all_leds_loop:
  56.            
  57.             mov r0,r6                @ r0 holds the value of the LED to be toggled
  58.             bl BSP_LED_Toggle        @ Toggle LED
  59.  
  60.             subs r6, #1              @ Move to next LED
  61.             bpl toggle_all_leds_loop
  62.  
  63.            
  64.             mov r0, r4               @ r0 now holds the delay time, to be used in busy_delay
  65.             bl busy_delay              
  66.  
  67.             subs r5, #1              @ Subtract one from the number of times all LEDs need to be toggled
  68.             bpl reset_leds
  69.  
  70.             b end
  71.  
  72.     lose:
  73.  
  74.         mov r0, r8                     @ r0 grabs the value of the LED currently ON...
  75.         bl BSP_LED_Toggle              @ and here we turn it OFF
  76.  
  77.         mov r8, #5                     @ Store in r8 number of times the LED will be toggled
  78.  
  79.         toggle_one_led_loop:
  80.            
  81.             mov r0,r6                  @ r0 holds the value of target LED
  82.             bl BSP_LED_Toggle
  83.  
  84.             mov r0, r4                 @ r0 now holds the delay time, to be used in busy_delay
  85.             bl busy_delay
  86.  
  87.             subs r8, #1                @ Subtract one from the number of times this one LED needs to be toggled
  88.             bpl toggle_one_led_loop
  89.            
  90.                                    
  91.     end:
  92.         mov r0, #0                     @ Return zero (always sucessful)
  93.         pop {r4-r9,lr}                 @ Restore the orginal values of the registers
  94.         bx lr                          @ Return (Branch eXchange) to the address in the link register (lr)  
  95.  
  96.     .size   mg_game_demo, .-mg_game_demo    @@ - symbol size (not req)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement