   1              	//
   2              	//  Illustrate allocating some unused registers as local variables of function
   3              	//
   4              	//
   5              	//
   6              	
   7              	/* --------------------------------------------------
   8              	   Define required labels for EGTAPI
   9              	   -------------------------------------------------- */
  10              	        .global main, Stop, CodeEnd, DataStart, DataEnd
  11              	
  12              		.global Pause, sumRange
  13              	
  14              	/* --------------------------------------------------
  15              	   Begin of the program instructions
  16              	   -------------------------------------------------- */
  17              		.text
  18              	main:
  19              		// sum = sumRange(A, x, y)
  20              	
  21              		// pass A in r0
  22 0000 000000E3 		movw  r0, #:lower16:A
  23 0004 000040E3 		movt  r0, #:upper16:A
  24              	
  25              		// pass x in r1
  26 0008 001000E3 	        movw  r1, #:lower16:x
  27 000c 001040E3 	        movt  r1, #:upper16:x
  28 0010 001091E5 		ldr   r1, [r1]
  29              	
  30              		// pass y in r2
  31 0014 002000E3 	        movw  r2, #:lower16:y
  32 0018 002040E3 	        movt  r2, #:upper16:y
  33 001c 002092E5 	        ldr   r2, [r2]
  34              	
  35 0020 FEFFFFEB 		bl sumRange
  36              	
  37 0024 001000E3 	        movw  r1, #:lower16:sum
  38 0028 001040E3 	        movt  r1, #:upper16:sum
  39 002c 000081E5 		str   r0, [r1]
  40              	
  41              	Pause:
  42              		// sum = sumRange(B, 3, 7)
  43              	
  44 0030 000000E3 		movw  r0, #:lower16:B
  45 0034 000040E3 	        movt  r0, #:upper16:B
  46              	
  47 0038 0310A0E3 		mov   r1, #3
  48 003c 0720A0E3 		mov   r2, #7
  49              	
  50 0040 FEFFFFEB 	        bl sumRange
  51              	
  52 0044 001000E3 	        movw  r1, #:lower16:sum
  53 0048 001040E3 	        movt  r1, #:upper16:sum
  54 004c 000081E5 	        str   r0, [r1]
  55              	
  56              	
  57              	
  58              	
  59              	Stop:
  60 0050 0000A0E1 		nop			   // Stop point of main( )
  61              	
  62              	
  63              	sumRange:
  64              		// A=r0, a=r1, b=r2, i=r3, s=r4
  65              	
  66 0054 0040A0E3 		mov  r4, #0	// s = 0
  67 0058 0130A0E1 		mov  r3, r1	// i = a
  68              	
  69              	while:
  70 005c 020053E1 		cmp  r3, r2
  71 0060 050000AA 		bge  whileEnd
  72              	
  73 0064 035083E0 		add  r5, r3, r3	// r5 = 2*i
  74 0068 055085E0 		add  r5, r5, r5 // r5 = 4*i
  75              	
  76 006c 055090E7 		ldr  r5, [r0, r5] // r5 = A[i]
  77              	
  78 0070 054084E0 		add  r4, r4, r5   // s = s + A[i]
  79              	
  80 0074 013083E2 		add  r3, r3, #1   // i++
  81 0078 F7FFFFEA 		b    while
  82              	
  83              	whileEnd:
  84              		// return(s)
  85              	
  86 007c 0400A0E1 		mov  r0, r4
  87 0080 0EF0A0E1 		mov  pc, lr
  88              	
  89              	CodeEnd:
  90 0084 0000A0E1 	    	nop
  91              	
  92              	/* --------------------------------------------------
  93              	   Begin of the permanent program variables
  94              	   -------------------------------------------------- */
  95              		.data
  96              	DataStart:
  97              	
  98 0000 0B000000 	A:	.4byte	11, 12, 13, 14, 15, 16, 17, 18, 19 , 20
  98      0C000000 
  98      0D000000 
  98      0E000000 
  98      0F000000 
  99 0028 01000000 	B:	.4byte	1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  99      02000000 
  99      03000000 
  99      04000000 
  99      05000000 
 100 0050 01000000 	x:	.4byte	1
 101 0054 03000000 	y:	.4byte	3
 102 0058 FFFFFFFF 	sum:	.4byte  -1
 103              	
 104              	DataEnd:
 105              	
 106              		.end
DEFINED SYMBOLS
/home/cs255001/cs255/demo/8-sub/reg-local1.s:18     .text:0000000000000000 main
/home/cs255001/cs255/demo/8-sub/reg-local1.s:59     .text:0000000000000050 Stop
/home/cs255001/cs255/demo/8-sub/reg-local1.s:89     .text:0000000000000084 CodeEnd
/home/cs255001/cs255/demo/8-sub/reg-local1.s:96     .data:0000000000000000 DataStart
/home/cs255001/cs255/demo/8-sub/reg-local1.s:104    .data:000000000000005c DataEnd
/home/cs255001/cs255/demo/8-sub/reg-local1.s:41     .text:0000000000000030 Pause
/home/cs255001/cs255/demo/8-sub/reg-local1.s:63     .text:0000000000000054 sumRange
/home/cs255001/cs255/demo/8-sub/reg-local1.s:98     .data:0000000000000000 A
/home/cs255001/cs255/demo/8-sub/reg-local1.s:22     .text:0000000000000000 $a
/home/cs255001/cs255/demo/8-sub/reg-local1.s:100    .data:0000000000000050 x
/home/cs255001/cs255/demo/8-sub/reg-local1.s:101    .data:0000000000000054 y
/home/cs255001/cs255/demo/8-sub/reg-local1.s:102    .data:0000000000000058 sum
/home/cs255001/cs255/demo/8-sub/reg-local1.s:99     .data:0000000000000028 B
/home/cs255001/cs255/demo/8-sub/reg-local1.s:69     .text:000000000000005c while
/home/cs255001/cs255/demo/8-sub/reg-local1.s:83     .text:000000000000007c whileEnd

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
