   1              	//
   2              	//  Fibonnaci(recursive) function
   3              	//
   4              	
   5              	/* --------------------------------------------------
   6              	   Define required labels for EGTAPI
   7              	   -------------------------------------------------- */
   8              	        .global main, Stop, CodeEnd, DataStart, DataEnd
   9              		.global fib, k, result
  10              	
  11              	/* --------------------------------------------------
  12              	   Begin of the program instructions
  13              	   -------------------------------------------------- */
  14              		.text
  15              	main:
  16              	
  17              		//  result = fib(k)
  18              	
  19              	        /* -------------------------------------------------
  20              		   Pass parameter k (using stack)
  21              	           ------------------------------------------------- */
  22 0000 000000E3 		movw	r0, #:lower16:k
  23 0004 000040E3 		movt	r0, #:upper16:k
  24 0008 000090E5 		ldr	r0, [r0]
  25 000c 04002DE5 		push	{r0}
  26              	
  27              		/* ----------------------------------------------------------------
  28              		   call result = fib(k)
  29              		   ---------------------------------------------------------------- */
  30 0010 FEFFFFEB 		bl	fib
  31 0014 04D08DE2 		add	sp, sp, #4	// Clean up the parameter k
  32              	
  33              	        /* -----------------------------------------------------------------
  34              		   Save return value (in r0) to variable result 
  35              	           ----------------------------------------------------------------- */
  36 0018 001000E3 		movw	r1, #:lower16:result  // Do NOT use r0 !!!
  37 001c 001040E3 		movt	r1, #:upper16:result  // (Because r0 contains the return value)
  38              	
  39 0020 000081E5 		str	r0, [r1]	     // This will store return value in sum
  40              	
  41              	Stop:
  42 0024 0000A0E1 		nop			   // Stop point of main( )
  43              	
  44              	
  45              	
  46              	
  47              	
  48              	/* ----------------------------------------------------------------
  49              	   Function int fib(int n)
  50              	
  51              	        Stack frame structure:
  52              	
  53              			old FP   <--------------------------------- FP
  54              			old LR (ret addr)
  55              			  n			+8
  56              	
  57              	   Body of fib(n)
  58              	
  59              		if ( n == 0 )
  60              		   return 1;
  61              		if ( n == 1 )
  62              		   return 1;
  63              		else
  64              		   return fib(n-1) + fib(n-2);
  65              	   ---------------------------------------------------------------- */
  66              	
  67              	fib:
  68              	
  69              		// When  fib begins, we will have: n  on the stack
  70              	
  71              	
  72              		/* ==========================================================
  73              		   Function Prelude: complete the stack frame structure
  74              		   ========================================================== */
  75 0028 04E02DE5 		push	{lr} 		// Save LR (return address)
  76 002c 04B02DE5 		push	{fp} 		// Save FP (used by caller)
  77 0030 0DB0A0E1 		mov	fp, sp 		// Mark the stack top location before
  78              					// allocating any local variables
  79 0034 00D04DE2 		sub	sp, sp, #0	// Allocate 0 int variables on the stack
  80              	
  81              	
  82              	
  83              		/* ===============================================
  84              		   We completed the stack frame
  85              		   Now we can write the function body
  86              		   =============================================== */
  87              	        // if ( n == 0 )
  88 0038 08009BE5 		ldr	r0, [fp, #8]	// r0 = n
  89              	
  90 003c 000050E3 		cmp	r0, #0		// Check n == 0
  91 0040 0300001A 		bne	else1		// n != 0 --> Goto "else1" part
  92              	
  93              		// return 1
  94              	
  95              		///// put return value 1 in return location r0
  96 0044 0100A0E3 		mov	r0, #1
  97              	
  98              		/* =============================================================
  99              		   Function Postlude: de-allocate local variable and restore FP
 100              		   ============================================================= */
 101 0048 0BD0A0E1 		mov	sp, fp		// De-allocate local variables
 102 004c 04B09DE4 		pop	{fp}		// Restore fp
 103 0050 04F09DE4 		pop	{pc} 		// Return to the caller
 104              	
 105              	else1:
 106              	        // if ( n == 1 )
 107 0054 08009BE5 		ldr	r0, [fp, #8]	// r0 = n
 108              	
 109 0058 010050E3 		cmp	r0, #1		// Check n == 1
 110 005c 0300001A 		bne	else2		// n != 1 --> Goto "else2" part
 111              	
 112              		// return 1
 113              	
 114              		///// put return value 1 in return location r0
 115 0060 0100A0E3 		mov	r0, #1
 116              	
 117              		/* =============================================================
 118              		   Function Postlude: de-allocate local variable and restore FP
 119              		   ============================================================= */
 120 0064 0BD0A0E1 		mov	sp, fp		// De-allocate local variables
 121 0068 04B09DE4 		pop	{fp}		// Restore fp
 122 006c 04F09DE4 		pop	{pc} 		// Return to the caller
 123              	
 124              	
 125              	else2:
 126              		// Compute: fib(n-1) + fib(n-2)
 127              	
 128              		//// Compute fib(n-1) first
 129 0070 08009BE5 		ldr	r0, [fp, #8]	// r0 = n
 130 0074 010040E2 		sub	r0, r0, #1	// r0 = n-1
 131              	
 132 0078 04002DE5 		push	{r0}		// pass (n-1) to fib on stack
 133 007c FEFFFFEB 		bl	fib
 134 0080 04D08DE2 		add 	sp, sp, #4	// Clean up parameter (n-1) from stack
 135              	
 136              		//// ** Right now, r0 = fib(n-1)  --- we can't lose this value !!!
 137              		//// ** But we can't compute  fib(n-1) + fib(n-2)  without fib(n-2)
 138              		//// ** We will call fib to compute fib(n-2)
 139              		//// ** We MUST save r0 on the STACK !!!
 140              	
 141 0084 04002DE5 		push    {r0}		// Save fib(n-1)  on the stack
 142              	
 143              	        //// Compute fib(n-2)
 144 0088 08009BE5 	        ldr     r0, [fp, #8]    // r0 = n
 145 008c 020040E2 	        sub     r0, r0, #2      // r0 = n-2
 146              	
 147 0090 04002DE5 	        push    {r0}            // pass (n-2) to fib on stack
 148 0094 FEFFFFEB 	        bl      fib
 149 0098 04D08DE2 	        add     sp, sp, #4      // Clean up parameter (n-2) from stack
 150              	
 151              		//// ** Right now, r0 = fib(n-2) - we can now compute fib(n-1)+fib(n-2)
 152              	
 153 009c 04109DE4 		pop     {r1}		// Retrieve fib(n-1)  from the stack
 154              	
 155 00a0 000081E0 		add     r0, r1, r0	// r0 = fib(n-1) + fib(n-2)
 156              					// NOTE: r0 has the correct return value !!!
 157              	
 158              		// return   fib(n-1)+fib(n-2)  in r0
 159              		/* =============================================================
 160              		   Function Postlude: de-allocate local variable and restore FP
 161              		   ============================================================= */
 162 00a4 0BD0A0E1 		mov	sp, fp		// De-allocate local variables
 163 00a8 04B09DE4 		pop	{fp}		// Restore fp
 164 00ac 04F09DE4 		pop	{pc} 		// Return to the caller
 165              	
 166              	
 167              	
 168              	
 169              	CodeEnd:
 170 00b0 0000A0E1 	    	nop
 171              	
 172              	/* --------------------------------------------------
 173              	   Begin of the permanent program variables
 174              	   -------------------------------------------------- */
 175              		.data
 176              	DataStart:
 177              	
 178 0000 03000000 	k:	.4byte	3
 179 0004 00000000 	result: .skip   4
 180              	
 181              	DataEnd:
 182              	
 183              		.end
DEFINED SYMBOLS
/home/cs255001/cs255/demo/8-sub/fib.s:15     .text:0000000000000000 main
/home/cs255001/cs255/demo/8-sub/fib.s:41     .text:0000000000000024 Stop
/home/cs255001/cs255/demo/8-sub/fib.s:169    .text:00000000000000b0 CodeEnd
/home/cs255001/cs255/demo/8-sub/fib.s:176    .data:0000000000000000 DataStart
/home/cs255001/cs255/demo/8-sub/fib.s:181    .data:0000000000000008 DataEnd
/home/cs255001/cs255/demo/8-sub/fib.s:67     .text:0000000000000028 fib
/home/cs255001/cs255/demo/8-sub/fib.s:178    .data:0000000000000000 k
/home/cs255001/cs255/demo/8-sub/fib.s:179    .data:0000000000000004 result
/home/cs255001/cs255/demo/8-sub/fib.s:22     .text:0000000000000000 $a
/home/cs255001/cs255/demo/8-sub/fib.s:105    .text:0000000000000054 else1
/home/cs255001/cs255/demo/8-sub/fib.s:125    .text:0000000000000070 else2
/home/cs255001/cs255/demo/8-sub/fib.s:179    .data:0000000000000004 $d

NO UNDEFINED SYMBOLS
   1              	
   2              		.global	malloc, print
   3              		.global	stdout_start, stdout_end
   4              	
   5              	// **********************************************
   6              	// malloc: simulate memory allocate
   7              	//
   8              	// This function will NOT change ANY registers
   9              	// (I.e.: will preserve ALL registers)
  10              	// **********************************************
  11              	malloc:
  12 0000 0E002DE9 		push 	{r1, r2, r3}		// Save scratch registers
  13              	
  14 0004 001000E3 		movw	r1, #:lower16:maddr
  15 0008 001040E3 		movt	r1, #:upper16:maddr	// r1 = &maddr
  16 000c 003091E5 		ldr	r3, [r1]		// r3 = maddr 
  17              	
  18              		// --------------------------------------
  19              	        // Make new maddr (divisible by 8)
  20              	        // --------------------------------------
  21              	
  22 0010 002083E0 		add	r2, r3, r0		// New maddr
  23 0014 082082E2 		add	r2, r2, #8		// Making sure it increase
  24              	
  25 0018 0700A0E3 		mov	r0, #0x7		// r0 = 0000..00111
  26 001c 0000E0E1 		mvn	r0, r0			// r0 = 1111..11000
  27 0020 002002E0 		and	r2, r2, r0		// Knock off the last 3 bits
  28 0024 002081E5 		str	r2, [r1]		// Update maddr 
  29              	
  30              	
  31              		// --------------------------------------
  32              		// Return to caller
  33              		// --------------------------------------
  34 0028 0300A0E1 		mov	r0, r3			// Return maddr value in r0
  35 002c 0E00BDE8 		pop  	{r1, r2, r3}		// Restore scratch registers
  36 0030 0EF0A0E1 		mov	pc, lr			// Return
  37              	
  38 0034 00000400 	maddr:	.word 0x40000		// Start address of the heap
  39              	
  40              	
  41              	// *******************************************************************
  42              	// print: simulate print output in Egtapi
  43              	//
  44              	//	input: r0 = address of string
  45              	//	       r1 = # characters to write
  46              	//
  47              	// Egtapi will show ASCII data stored starting at stdout_start
  48              	// until stdout_end  in its output area
  49              	//
  50              	// This function will NOT change ANY registers
  51              	// (I.e.: will preserve ALL registers)
  52              	// *******************************************************************
  53              	print:
  54 0038 1C002DE9 		push	{r2, r3, r4}            	// Save scratch registers
  55              	
  56 003c 002000E3 	        movw    r2, #:lower16:stdout_end
  57 0040 002040E3 	        movt    r2, #:upper16:stdout_end	// r2 = &stdout_end
  58 0044 002092E5 	        ldr     r2, [r2]                	// r2 = stdout_end
  59              	
  60 0048 0130A0E1 		mov	r3, r1			// r3 = # char to copy
  61              	
  62              	printLoop:
  63 004c 000053E3 		cmp	r3, #0
  64 0050 0500000A 		beq	printDone
  65              	
  66 0054 0040D0E5 		ldrb	r4, [r0]		// Get next char
  67 0058 0040C2E5 		strb	r4, [r2]		// Write next char
  68              	
  69 005c 010080E2 		add	r0, r0, #1
  70 0060 012082E2 		add	r2, r2, #1
  71              	
  72 0064 013043E2 		sub	r3, r3, #1
  73              	
  74 0068 F7FFFFEA 		b	printLoop
  75              	
  76              	printDone:
  77 006c 2040A0E3 		mov	r4, #32			// Space
  78 0070 0040C2E5 		strb	r4, [r2]
  79 0074 012082E2 		add	r2, r2, #1
  80              	
  81 0078 003000E3 	        movw    r3, #:lower16:stdout_end
  82 007c 003040E3 	        movt    r3, #:upper16:stdout_end	// r2 = &stdout_end
  83 0080 002083E5 		str	r2, [r3]
  84              	
  85 0084 1C00BDE8 		pop  	{r2, r3, r4}		// Restore scratch registers
  86 0088 0EF0A0E1 		mov	pc, lr			// Return
  87              		
  88              	
  89              		
  90              	
  91 008c 00000500 	stdout_start: 	.word 0x50000		// This value remains unchanged
  92 0090 00000500 	stdout_end: 	.word 0x50000
  93              	
  94              	
  95              		.end
DEFINED SYMBOLS
/home/egtapi/lib/cs255lib.s:11     .text:0000000000000000 malloc
/home/egtapi/lib/cs255lib.s:53     .text:0000000000000038 print
/home/egtapi/lib/cs255lib.s:91     .text:000000000000008c stdout_start
/home/egtapi/lib/cs255lib.s:92     .text:0000000000000090 stdout_end
/home/egtapi/lib/cs255lib.s:12     .text:0000000000000000 $a
/home/egtapi/lib/cs255lib.s:38     .text:0000000000000034 maddr
/home/egtapi/lib/cs255lib.s:38     .text:0000000000000034 $d
/home/egtapi/lib/cs255lib.s:54     .text:0000000000000038 $a
/home/egtapi/lib/cs255lib.s:62     .text:000000000000004c printLoop
/home/egtapi/lib/cs255lib.s:76     .text:000000000000006c printDone
/home/egtapi/lib/cs255lib.s:91     .text:000000000000008c $d

NO UNDEFINED SYMBOLS
