/*
 * coast_asm.s
 *
 *  Created on: Jun 18, 2025
 *      Author: hasin
 */

//void coast_delay(uint32_t val){
//	for(uint32_t i=0; i<val; i++){
//		for(uint32_t j=0;j<10000;j++){
//		}
//	}
//}

//void coast_delay(uint32_t val){
//  uint32_t i=0;
//	while(i<val){
//      uint32_3 j=0;
//		while(j<10000){
//         j++;
//		}
//      i++;
//	}
//}

// we will use r4 for i and r5 for j

.global coast_asm_delay
coast_asm_delay:

	// save callee-saved registers
	push {r4}
	push {r5}

    //uint32_t i=0;
	MOV r4, #0

//while(i<val) loop
loop1_start:

	//compare i with val
	CMP r4, r0
	//go to finish if i>=val
	BHS loop1_end
	//if i<val, keep going

	//j=0
	MOV r5, #0

	//while(j<10000) loop
	loop2_start:

		// r1 = 10000
		LDR R1, =10000
		//compare j with 10000
		CMP R5, R1
		//go to end of loop2 if j>=10000
		BHS loop2_end
		//j++
		ADD r5, r5, #1
		// go to start of loop 2
		B loop2_start

	loop2_end:

		//i++
	    ADD R4, R4, #1
	    //goto start of loop 1
	    B loop1_start


loop1_end:

	// restore the changed values
	POP {r5}
	POP {r4}

	//return
	BX lr

