.global main, Stop, CodeEnd, DataStart, DataEnd .global head, ptr, Insert main: // head = Insert( head, ptr ); // Pass ptr to Insert movw r0, #:lower16:ptr movt r0, #:upper16:ptr // r0 = addr(ptr) ldr r0, [r0] // r0 = ptr push {r0} // Pass ptr using stack // Pass head to Insert movw r0, #:lower16:head movt r0, #:upper16:head // r0 = addr(head) ldr r0, [r0] // r0 = head push {r0} // Pass head using stack bl Insert add sp, sp, #8 // Clean up parameters // Store return value in head movw r1, #:lower16:head movt r1, #:upper16:head // r1 = addr(head) str r0, [r1] Stop: nop /* ----------------------------------------------------------------------- Insert at tail - recursive function Algorithm: List Insert( List h, List e ) { if ( h == null ) { e.next = null; return e; } else { h.next = Insert( h.next, e ); return h; } } Stack frame of the Insert( ) method: SP FP -----> +---------------------+ | old Frame Pointer | +---------------------+ | Return Address | +---------------------+ | h | addr(n) = FP + 8 +---------------------+ | e | addr(n) = FP + 12 +---------------------+ ----------------------------------------------------------------------- */ Insert: /* ============================= Prelude: build stack frame ============================= */ push {lr} push {fp} mov fp, sp sub sp, sp, #0 // No local variables // if ( h == null ) ldr r0, [fp, #8] cmp r0, #0 bne else // e.next = null ldr r0, [fp, #12] mov r1, #0 // null str r1, [r0, #4] // e.next = null // return e ldr r0, [fp, #12] // r0 = e (return value) // Go to postlude - clean up and return to caller b postLude else: // h.next = Insert( h.next, e ); //// pass e ldr r0, [fp, #12] // r0 = e push {r0} // Pass e with stack //// pass h.next ldr r0, [fp, #8] // r0 = h ldr r0, [r0, #4] // r0 = h.next push {r0} // Pass h.next with stack bl Insert add sp, sp, #8 // Clean up 2 parameters //// Store return value (in r0) in h.next ldr r1, [fp, #8] // r1 = h str r0, [r1, #4] // h.next = return value from Insert(h.next,e) // retunr( h ); ldr r0, [fp, #8] // r0 = h (return value) // Postlude - clean up and return to caller postLude: mov sp, fp pop {fp} pop {pc} CodeEnd: nop /* ************************************************************** Permanent variables ************************************************************** */ .data DataStart: head: .word p0 // head contains the address of the first list elem // head->[11]->[22]->[33]->[44]->[55] ptr: .word p9 // list structure is: [int value, next] p0: .4byte 11, p3 // p0 contains [11, p3] p1: .4byte 33, p2 // p1 contains [33, p2] p2: .4byte 44, p4 // p2 contains [44, p4] p3: .4byte 22, p1 // p3 contains [22, p1] p4: .4byte 55, 0 // p4 contains [55, 0] p9: .4byte 99, 0 // p9 contains [99, 0] DataEnd: .end