|
| 1 | +WeChat: cstutorcs |
| 2 | +QQ: 749389476 |
| 3 | +Email: tutorcs@163.com |
| 4 | +; |
| 5 | +; The code given to you here implements the histogram calculation that |
| 6 | +; we developed in class. In programming video lecture, we will discuss |
| 7 | +; how to prints a number in hexadecimal to the monitor. |
| 8 | +; |
| 9 | +; Your assignment for this program is to combine these two pieces of |
| 10 | +; code to print the histogram to the monitor. |
| 11 | +; |
| 12 | +; If you finish your program, |
| 13 | +; ** Submit a working version to Gradescope ** |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + .ORIG x3000 ; starting address is x3000 |
| 18 | + |
| 19 | + |
| 20 | +; |
| 21 | +; Count the occurrences of each letter (A to Z) in an ASCII string |
| 22 | +; terminated by a NUL character. Lower case and upper case should |
| 23 | +; be counted together, and a count also kept of all non-alphabetic |
| 24 | +; characters (not counting the terminal NUL). |
| 25 | +; |
| 26 | +; The string starts at x4000. |
| 27 | +; |
| 28 | +; The resulting histogram (which will NOT be initialized in advance) |
| 29 | +; should be stored starting at x3F00, with the non-alphabetic count |
| 30 | +; at x3F00, and the count for each letter in x3F01 (A) through x3F1A (Z). |
| 31 | +; |
| 32 | +; table of register use in this part of the code |
| 33 | +; R0 holds a pointer to the histogram (x3F00) |
| 34 | +; R1 holds a pointer to the current position in the string |
| 35 | +; and as the loop count during histogram initialization |
| 36 | +; R2 holds the current character being counted |
| 37 | +; and is also used to point to the histogram entry |
| 38 | +; R3 holds the additive inverse of ASCII '@' (xFFC0) |
| 39 | +; R4 holds the difference between ASCII '@' and 'Z' (xFFE6) |
| 40 | +; R5 holds the difference between ASCII '@' and '`' (xFFE0) |
| 41 | +; R6 is used as a temporary register |
| 42 | +; |
| 43 | + |
| 44 | + LD R0,HIST_ADDR ; point R0 to the start of the histogram |
| 45 | + |
| 46 | + ; fill the histogram with zeroes |
| 47 | + AND R6,R6,#0 ; put a zero into R6 |
| 48 | + LD R1,NUM_BINS ; initialize loop count to 27 |
| 49 | + ADD R2,R0,#0 ; copy start of histogram into R2 |
| 50 | + |
| 51 | + ; loop to fill histogram starts here |
| 52 | +HFLOOP STR R6,R2,#0 ; write a zero into histogram |
| 53 | + ADD R2,R2,#1 ; point to next histogram entry |
| 54 | + ADD R1,R1,#-1 ; decrement loop count |
| 55 | + BRp HFLOOP ; continue until loop count reaches zero |
| 56 | + |
| 57 | + ; initialize R1, R3, R4, and R5 from memory |
| 58 | + LD R3,NEG_AT ; set R3 to additive inverse of ASCII '@' |
| 59 | + LD R4,AT_MIN_Z ; set R4 to difference between ASCII '@' and 'Z' |
| 60 | + LD R5,AT_MIN_BQ ; set R5 to difference between ASCII '@' and '`' |
| 61 | + LD R1,STR_START ; point R1 to start of string |
| 62 | + |
| 63 | + ; the counting loop starts here |
| 64 | +COUNTLOOP |
| 65 | + LDR R2,R1,#0 ; read the next character from the string |
| 66 | + BRz PRINT_HIST ; found the end of the string |
| 67 | + |
| 68 | + ADD R2,R2,R3 ; subtract '@' from the character |
| 69 | + BRp AT_LEAST_A ; branch if > '@', i.e., >= 'A' |
| 70 | +NON_ALPHA |
| 71 | + LDR R6,R0,#0 ; load the non-alpha count |
| 72 | + ADD R6,R6,#1 ; add one to it |
| 73 | + STR R6,R0,#0 ; store the new non-alpha count |
| 74 | + BRnzp GET_NEXT ; branch to end of conditional structure |
| 75 | +AT_LEAST_A |
| 76 | + ADD R6,R2,R4 ; compare with 'Z' |
| 77 | + BRp MORE_THAN_Z ; branch if > 'Z' |
| 78 | + |
| 79 | +; note that we no longer need the current character |
| 80 | +; so we can reuse R2 for the pointer to the correct |
| 81 | +; histogram entry for incrementing |
| 82 | +ALPHA ADD R2,R2,R0 ; point to correct histogram entry |
| 83 | + LDR R6,R2,#0 ; load the count |
| 84 | + ADD R6,R6,#1 ; add one to it |
| 85 | + STR R6,R2,#0 ; store the new count |
| 86 | + BRnzp GET_NEXT ; branch to end of conditional structure |
| 87 | + |
| 88 | +; subtracting as below yields the original character minus '`' |
| 89 | +MORE_THAN_Z |
| 90 | + ADD R2,R2,R5 ; subtract '`' - '@' from the character |
| 91 | + BRnz NON_ALPHA ; if <= '`', i.e., < 'a', go increment non-alpha |
| 92 | + ADD R6,R2,R4 ; compare with 'z' |
| 93 | + BRnz ALPHA ; if <= 'z', go increment alpha count |
| 94 | + BRnzp NON_ALPHA ; otherwise, go increment non-alpha |
| 95 | + |
| 96 | +GET_NEXT |
| 97 | + ADD R1,R1,#1 ; point to next character in string |
| 98 | + BRnzp COUNTLOOP ; go to start of counting loop |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +PRINT_HIST |
| 103 | + |
| 104 | +; you will need to insert your code to print the histogram here |
| 105 | + |
| 106 | +; do not forget to write a brief description of the approach/algorithm |
| 107 | +; for your implementation, list registers used in this part of the code, |
| 108 | +; and provide sufficient comments |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +DONE HALT ; done |
| 113 | + |
| 114 | + |
| 115 | +; the data needed by the program |
| 116 | +NUM_BINS .FILL #27 ; 27 loop iterations |
| 117 | +NEG_AT .FILL xFFC0 ; the additive inverse of ASCII '@' |
| 118 | +AT_MIN_Z .FILL xFFE6 ; the difference between ASCII '@' and 'Z' |
| 119 | +AT_MIN_BQ .FILL xFFE0 ; the difference between ASCII '@' and '`' |
| 120 | +HIST_ADDR .FILL x3F00 ; histogram starting address |
| 121 | +STR_START .FILL x4000 ; string starting address |
| 122 | + |
| 123 | +; for testing, you can use the lines below to include the string in this |
| 124 | +; program... |
| 125 | +; STR_START .FILL STRING ; string starting address |
| 126 | +; STRING .STRINGZ "This is a test of the counting frequency code. AbCd...WxYz." |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + ; the directive below tells the assembler that the program is done |
| 131 | + ; (so do not write any code below it!) |
| 132 | + |
| 133 | + .END |
0 commit comments