0

I just started learning ARM Assembly for the Cortex M4 architecture. I have the basic concepts understood, such as loops, memory access, and proper use of instructions. I've come across another loop type that I tried implementing, the delay loop. I understand the premise, just a loop with a delay in the execution of the next instruction. So I tried my best using the knowledge I have of NOPs, cycle times, and loops, but haven't gotten anywhere. My goal with the delay loop is to print an integer to the terminal every 2 seconds. My initial approach was to try and set up two nested loops and load the cycle time into the registers, this gave me an infinite loop of just strings. I scrapped that idea and have tried the following, but am encountering errors, but believe I'm on the right track.

PRESERVE8
AREA MyCode, CODE, READONLY
EXPORT start

start
    import print  ;Custom C function that prints a string to screen
    MOV R1, #0  ;Number from which the program will count down

loop1
    CMP R1, #10 ;Compares the register with 0
    BEQ stop    ;If true, will jump to stop
    BL print    ;Calls the C function print to print the number
    
    BLE loop    ;Loop back to loop until complete

loop2
    LDR R2, =num
    STR R1, [R2]
    ADDS R1, R1, #1 ;Increments the count

stop
    B stop

    ALIGN
    AREA MyData, DATA, READWRITE
srcstr DCB "String source", 0
    END

I've kind of run out of ideas about what to do or where to go from here. Could someone please show me what I'm looking for?

Cheers

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
BlueAngel45
  • 21
  • 1
  • 4
  • Not all those branch destinations exist in the code, or the given labels used. – Weather Vane Sep 25 '22 at 22:32
  • 2
    It's been figured out several decades ago that software timing loops are commonly a bad idea. For a 2 second time, reconsider & see if there is a HW timer that you can use instead. Don't use software timing loops unless you really, truly, absolutely have to and there is genuinely no alternative. – Avi Berger Sep 25 '22 at 22:32
  • 2
    If you encounter errors, always say what the error is you encounter! – fuz Sep 25 '22 at 22:39
  • Delay lopps with any accuracy only work on specific platforms like a pic mcu for example. For various reasons the loop will change timing even on the same mcu. The timers are easy to use exp for 2 seconds, just use a timer. Start with the systick as it is from arm and in many of the cortex-ms . – old_timer Sep 26 '22 at 18:42
  • a delay loop using counting is a good starting point for blinking an led when learning a new chip or board, a very good idea, but you quickly want to switch to a timer. – old_timer Sep 26 '22 at 18:43

0 Answers0