The array samples contains 7 8-bit unsigned binary numbers (integers) as shown b
ID: 3564516 • Letter: T
Question
The array samples contains 7 8-bit unsigned binary numbers (integers) as shown below. Write a program which computes the mean and variance of the numbers in samples and stores them in the variables mean and variance, respectively. In the computation, you may ignore any fraction, but minimize the numerical error. You must use loops. Your program should work for any numbers stored in samples. RAMStart=$0800 and ROMStart=$4000.
samples DC.B $10,$25,$90,$CC,$56,$3F,$E1 mean DS.B 1
variance DS.W 1
Assemble the program and execute it in the single-step mode with the contents of the array and variables displayed in the memory window. Make sure that the starting address of the memory window is $0800 (i.e. RAMStart). Do screen-captures (the source, register and memory windows) right before the first instruction is executed, right after the 4th number is processed (in each loop), and right after the final results are stored into the variables mean and variance.
Submit the list file of your code with the screen-captures attached.
Explanation / Answer
************************************************************* 12 00000400 ORG $400 13 00000400 61000026 BSR CTOTAL 14 00000404 61000042 BSR CMEAN 15 00000408 61000054 BSR CDIFF 16 0000040C 61000090 BSR CVAR 17 00000410 610000B2 BSR PNAME 18 00000414 610000D4 BSR PMEAN 19 00000418 6100015C BSR NL 20 0000041C 61000112 BSR PVAR 21 00000420 61000154 BSR NL 22 00000424 4E722700 EXIT: STOP #$2700 23 24 ************************************************************* 25 * SUB-ROUTINES * 26 ************************************************************* 27 28 ***/ CALCULATE TOTAL /*** 29 00000428 4282 CTOTAL: CLR.L D2 30 0000042A 4283 CLR.L D3 31 0000042C 41F900001022 LEA DATA,A0 32 00000432 3418 LOOP1: MOVE.W (A0)+,D2 ;LOOP (D2 = Data) 33 00000434 0C420000 CMP.W #0,D2 ;IF (D2 == 0) 34 00000438 67000006 BEQ ECTOTAL ;GOTO ECTotal 35 0000043C D642 ADD.W D2,D3 ;D3 := D3 + D2 36 0000043E 60F2 BRA LOOP1 ;GOTO LOOP 37 00000440 33C300001042 ECTOTAL: MOVE.W D3,TOTAL ;Total := D3 38 00000446 4E75 RTS 39 40 ***/ CALCULATE MEAN /*** 41 00000448 4282 CMEAN: CLR.L D2 42 0000044A 41F900001042 LEA TOTAL,A0 43 00000450 3410 MOVE.W (A0),D2 ;Load Total into D4 44 00000452 84FC000F DIVU #N,D2 ;D2 := Total / N [ Remainder | Quotient ] 45 00000456 23C200001044 MOVE.L D2,MEAN ;Mean := D2 46 0000045C 4E75 RTS 47 48 ***/ CALCULATE DIFFERENCES /*** 49 0000045E 4282 CDIFF: CLR.L D2 50 00000460 4283 CLR.L D3 51 00000462 4284 CLR.L D4 52 00000464 47F900001044 LEA MEAN,A3 53 0000046A 2813 MOVE.L (A3),D4 54 0000046C 43F900001022 LEA DATA,A1 55 00000472 45F90000104C LEA DIFF,A2 56 00000478 3419 LOOP2: MOVE.W (A1)+,D2 ;LOOP (D2 = Data) 57 0000047A 0C420000 CMP.W #0,D2 ;IF (D2 == 0) 58 0000047E 67000016 BEQ ECDIFF ;GOTO ECDiff 59 00000482 3602 MOVE D2,D3 60 00000484 9644 SUB.W D4,D3 ;D3 := D3 - D4 (D3 := Data - Mean) 61 00000486 6B000006 BMI OPP ;IF (D2 < 0) GOTO Opp 62 0000048A 60000004 BRA MULT ;Else GOTO Mult 63 0000048E 4443 OPP: NEG D3 ;D3 := 0 - D3 (2's compliment) 64 00000490 C6C3 MULT: MULU D3,D3 ;D3 := D3^2 65 00000492 24C3 MOVE.L D3,(A2)+ ;Move D3 into next longword in Diff 66 00000494 60E2 BRA LOOP2 ;GOTO LOOP 67 00000496 24BC00000000 ECDIFF: MOVE.L #0,(A2) ;Set the last longword in Diff to 0 68 0000049C 4E75 RTS 69 70 ***/ CALCULATE VARIANCE /*** 71 0000049E 4282 CVAR: CLR.L D2 72 000004A0 4283 CLR.L D3 73 000004A2 43F90000104C LEA DIFF,A1 74 000004A8 2419 NEXT: MOVE.L (A1)+,D2 ;LOOP WHILE (DIFF != 0) 75 000004AA 0C8200000000 CMP.L #0,D2 ;If (D2 == 0) 76 000004B0 67000006 BEQ DIV ;GOTO Div 77 000004B4 D682 ADD.L D2,D3 ;D3 := D3 + D2 78 000004B6 60F0 BRA NEXT ;GOTO Next 79 000004B8 86FC000F DIV: DIVU #N,D3 ;D3 := D3 / N [ Remainder | Quotient ] 80 000004BC 23C300001048 MOVE.L D3,VAR 81 000004C2 4E75 RTS 82 83 ***/ PRINT NAME /*** 84 000004C4 4280 PNAME: CLR.L D0 85 000004C6 4281 CLR.L D1 86 000004C8 43F900001000 LEA NAMEH,A1 87 000004CE 323C0006 MOVE.W #6,D1 88 000004D2 103C0001 MOVE.B #1,D0 89 000004D6 4E4F TRAP #15 ;Print 'Name: ' 90 000004D8 43F900001006 LEA NAME,A1 91 000004DE 323C000C MOVE.W #12,D1 92 000004E2 103C0000 MOVE.B #0,D0 93 000004E6 4E4F TRAP #15 ;Print Name 94 000004E8 4E75 RTS 95 96 ***/ PRINT MEAN /*** 97 000004EA 4280 PMEAN: CLR.L D0 98 000004EC 4281 CLR.L D1 99 000004EE 4282 CLR.L D2 100 000004F0 43F900001012 LEA MEANH,A1 101 000004F6 323C0006 MOVE.W #6,D1 102 000004FA 103C0001 MOVE.B #1,D0 103 000004FE 4E4F TRAP #15 ;Print 'Mean: ' 104 00000500 41F900001044 LEA MEAN,A0 105 00000506 2410 MOVE.L (A0),D2 106 00000508 3202 MOVE.W D2,D1 107 0000050A 103C0003 MOVE.B #3,D0 108 0000050E 4E4F TRAP #15 ;Print quotient 109 00000510 323C0020 MOVE.W #$20,D1 110 00000514 103C0006 MOVE.B #6,D0 111 00000518 4E4F TRAP #15 ;Print space 112 0000051A 323C0072 MOVE.W #$72,D1 113 0000051E 103C0006 MOVE.B #6,D0 114 00000522 4E4F TRAP #15 ;Print r (for remainder) 115 00000524 4842 SWAP D2 116 00000526 3202 MOVE.W D2,D1 117 00000528 103C0003 MOVE.B #3,D0 118 0000052C 4E4F TRAP #15 ;Print remainder 119 0000052E 4E75 RTS 120 121 ***/ PRINT VARIANCE /*** 122 00000530 4280 PVAR: CLR.L D0 123 00000532 4281 CLR.L D1 124 00000534 4282 CLR.L D2 125 00000536 43F900001018 LEA VARH,A1 126 0000053C 323C000A MOVE.W #10,D1 127 00000540 103C0001 MOVE.B #1,D0 128 00000544 4E4F TRAP #15 ;Print 'Variance: ' 129 00000546 41F900001048 LEA VAR,A0 130 0000054C 2410 MOVE.L (A0),D2 131 0000054E 3202 MOVE.W D2,D1 132 00000550 103C0003 MOVE.B #3,D0 133 00000554 4E4F TRAP #15 ;Print quotient 134 00000556 323C0020 MOVE.W #$20,D1 135 0000055A 103C0006 MOVE.B #6,D0 136 0000055E 4E4F TRAP #15 ;Print space 137 00000560 323C0072 MOVE.W #$72,D1 138 00000564 103C0006 MOVE.B #6,D0 139 00000568 4E4F TRAP #15 ;Print r (for remainder) 140 0000056A 4842 SWAP D2 141 0000056C 3202 MOVE.W D2,D1 142 0000056E 103C0003 MOVE.B #3,D0 143 00000572 4E4F TRAP #15 ;Print remainder 144 00000574 4E75 RTS 145 146 ***/ PRINT NEW LINE /*** 147 00000576 4280 NL: CLR.L D0 148 00000578 4281 CLR.L D1 149 0000057A 123C000A MOVE.B #$0A,D1 ;Print New Line 150 0000057E 103C0006 MOVE.B #$6,D0 151 00000582 4E4F TRAP #15 152 00000584 123C000D MOVE.B #$0D,D1 ;Print Carriage Return 153 00000588 103C0006 MOVE.B #$6,D0 154 0000058C 4E4F TRAP #15 155 0000058E 4E75 RTS 156 157 ************************************************************* 158 * DATA * 159 ************************************************************* 160 0000000F N: EQU 15 161 00001000 ORG $1000 162 00001000 4E616D653A20 NAMEH: DC.B 'Name: ' 163 00001006 4A616D657320 NAME: DC.B 'Rahul Agarwal' 536865657473 164 00001012 4D65616E3A20 MEANH: DC.B 'Mean: ' 165 00001018 56617269616E VARH: DC.B 'Variance: ' 63653A20 166 00001022 000A000C0008 DATA: DC.W 10,12,8,17,9,22,18,11,23,7,30,22,19,6,7,0 001100090016 0012000B0017 0007001E0016 001300060007 0000 167 00001042 00000002 TOTAL: DS.W 1 168 00001044 00000004 MEAN: DS.L 1 169 00001048 00000004 VAR: DS.L 1 170 0000104C 00000040 DIFF: DS.L 16 171 00000400 END $400Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.