|
| 1 | +# ------------------------------------------------------------------- |
| 2 | +# [KNU COMP411 Computer Architecture] Test code for the 1st project (calculator) |
| 3 | +# ------------------------------------------------------------------- |
| 4 | + |
| 5 | +.data |
| 6 | +mul_str: .string "multiplication " |
| 7 | +add_str: .string "addition " |
| 8 | +sub_str: .string "subtraction " |
| 9 | +div_str: .string "division " |
| 10 | +success_str: .string "success counts:" |
| 11 | +total_testnum_str: .string "/100" |
| 12 | +.align 2 |
| 13 | + |
| 14 | +int_input1: .space 400 #reserve memory space to store the input values for the test |
| 15 | +int_input2: .space 400 |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +.text |
| 20 | +##---------------------------------- |
| 21 | +#name: init_test |
| 22 | +#func: initialize input values that will be used for test |
| 23 | +#---------------------------------- |
| 24 | +init_test: |
| 25 | + addi t0, zero, 221 #1st operand |
| 26 | + addi t1, zero, 10 #2nd operand |
| 27 | + addi t2, zero, 100 #iteration count |
| 28 | + la t3, int_input1 #base address for 1st operand |
| 29 | + la t4, int_input2 #base address for 2nd operand |
| 30 | + init_test_loop: |
| 31 | + sw t0, 0(t3) |
| 32 | + sw t1, 0(t4) |
| 33 | + addi t0, t0, 1 |
| 34 | + addi t1, t1, 1 |
| 35 | + addi t3, t3, 4 |
| 36 | + addi t4, t4, 4 |
| 37 | + addi t2, t2, -1 |
| 38 | + bne t2, zero, init_test_loop |
| 39 | + jalr zero, 0(ra) |
| 40 | + |
| 41 | + |
| 42 | +##---------------------------------- |
| 43 | +#name: test_core |
| 44 | +#func: core procedure of basic functionality test |
| 45 | +#x10(a0): result |
| 46 | +#x11(a1): arithmetic operation (0: addition, 1: subtraction, 2: multiplication, 3: division) |
| 47 | + |
| 48 | +#---------------------------------- |
| 49 | +test_core: |
| 50 | + addi sp, sp, -88 #adjust stack pointer |
| 51 | + sw ra, 0(sp) #push register values to stack |
| 52 | + sw a0, 8(sp) |
| 53 | + sw a1, 16(sp) |
| 54 | + sw a2, 24(sp) |
| 55 | + sw a3, 32(sp) |
| 56 | + sw a4, 40(sp) |
| 57 | + sw a5, 48(sp) |
| 58 | + sw s0, 56(sp) |
| 59 | + sw s1, 64(sp) |
| 60 | + sw s2, 72(sp) |
| 61 | + sw s3, 80(sp) |
| 62 | + |
| 63 | + #test for integer arithmetic |
| 64 | + addi s3, zero, 0 #test success count |
| 65 | + addi s0, zero, 100 #set the number of test |
| 66 | + la s1, int_input1 #store the base oddress array input1 in x6 |
| 67 | + la s2, int_input2 #store the base oddress array input1 in x7 |
| 68 | + |
| 69 | + test_loop: |
| 70 | + lw a2, 0(s1) #load input1 to x12 (a2) |
| 71 | + lw a3, 0(s2) #load input2 to x13 (a3) |
| 72 | + |
| 73 | + |
| 74 | + addi t0, zero, 0 |
| 75 | + bne a1, t0, get_answer_sub |
| 76 | + get_answer_add: |
| 77 | + add t1, a2, a3 |
| 78 | + beq zero,zero, get_answer_done |
| 79 | + get_answer_sub: |
| 80 | + addi t0, zero, 1 |
| 81 | + bne a1, t0, get_answer_mul |
| 82 | + sub t1, a2, a3 |
| 83 | + beq zero,zero, get_answer_done |
| 84 | + get_answer_mul: |
| 85 | + addi t0, zero, 2 |
| 86 | + bne a1, t0, get_answer_div |
| 87 | + mul t1, a2, a3 |
| 88 | + beq zero,zero, get_answer_done |
| 89 | + get_answer_div: |
| 90 | + addi t0, zero, 3 |
| 91 | + bne a1, t0, get_answer_done |
| 92 | + div t1, a2, a3 |
| 93 | + rem t2, a2, a3 |
| 94 | + |
| 95 | + get_answer_done: |
| 96 | + jal ra, calc #call calc() |
| 97 | + bne a0, t1, test_done #check if the result of calc is correct |
| 98 | + addi s3, s3, 1 #increase the success count if the result is correct |
| 99 | + addi t0, zero, 3 #if the operation is division, check the remainer as well |
| 100 | + bne a1, t0, test_done |
| 101 | + beq a4, t2, test_done #decrease the success count if the remainder is not correct |
| 102 | + addi s3, s3, -1 |
| 103 | + test_done: |
| 104 | + add a5, zero, s3 #store an argument (success count) in a5 |
| 105 | + jal x1, print_test_message #call print_test_message |
| 106 | + addi x6, x6, 4 #increase base address of the array input1 by 4 |
| 107 | + addi x7, x7, 4 #increase base address of the array input2 by 4 |
| 108 | + addi s0, s0, -1 #decrease loop count |
| 109 | + addi s1, s1, 4 #increase the index for input1 |
| 110 | + addi s2, s2, 4 #increase the index for input2 |
| 111 | + bne s0, x0, test_loop |
| 112 | + |
| 113 | + |
| 114 | + lw ra, 0(sp) #pop register values from stack |
| 115 | + lw a0, 8(sp) |
| 116 | + lw a1, 16(sp) |
| 117 | + lw a2, 24(sp) |
| 118 | + lw a3, 32(sp) |
| 119 | + lw a4, 40(sp) |
| 120 | + lw a5, 48(sp) |
| 121 | + lw s0, 56(sp) |
| 122 | + lw s1, 64(sp) |
| 123 | + lw s2, 72(sp) |
| 124 | + lw s3, 80(sp) |
| 125 | + addi sp, sp, 88 #adjust stack pointer |
| 126 | + jalr zero, 0(ra) |
| 127 | + |
| 128 | +##---------------------------------- |
| 129 | +#name: test |
| 130 | +#func: functional test |
| 131 | +#x11: arithmetic operation (0:addition, 1: subtraction, 2: multiplication, 3: division) |
| 132 | +#x12: result calculated with the calc |
| 133 | +#---------------------------------- |
| 134 | +test: |
| 135 | + addi sp, sp, -16 |
| 136 | + sw a1, 0(sp) |
| 137 | + sw ra, 8(sp) |
| 138 | + |
| 139 | + jal ra, init_test #initialize test inputs |
| 140 | + addi a1, zero, 0 |
| 141 | + jal ra, test_core |
| 142 | + addi a1, zero, 1 |
| 143 | + jal ra, test_core |
| 144 | + addi a1, zero, 2 |
| 145 | + jal ra, test_core |
| 146 | + addi a1, zero, 3 |
| 147 | + jal ra, test_core |
| 148 | + |
| 149 | + lw a1, 0(sp) |
| 150 | + lw ra, 8(sp) |
| 151 | + addi sp, sp, 16 |
| 152 | + |
| 153 | + jalr zero, 0(ra) |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +#---------------------------------- |
| 159 | +#name: print_test_message |
| 160 | +#func: print a test message |
| 161 | +#x10(a0): result |
| 162 | +#x11(a1): arithmetic operation (0:int addition, 1: int subtraction, 2: int multiplication, 4: division) |
| 163 | +#x12(a2): 1st operand |
| 164 | +#x13(a3): 2nd operand |
| 165 | +#x14(a4): remainder |
| 166 | +#x15(a5): success count |
| 167 | +#---------------------------------- |
| 168 | +print_test_message: |
| 169 | + addi sp, sp, -24 |
| 170 | + sw a0, 0(sp) |
| 171 | + sw a1, 8(sp) |
| 172 | + sw a7, 16(sp) |
| 173 | + |
| 174 | + add t2, a0, zero #store result in t2 |
| 175 | + print_test_message_int_add: |
| 176 | + bne a1, zero, print_test_message_int_sub |
| 177 | + la a0, add_str |
| 178 | + addi t3, zero, 43 #store ascii code of operator in t3 |
| 179 | + beq zero, zero, print_test_message_op |
| 180 | + print_test_message_int_sub: |
| 181 | + addi t0, zero, 1 |
| 182 | + bne a1, t0, print_test_message_int_mul |
| 183 | + la a0, sub_str |
| 184 | + addi t3, zero, 45 #store ascii code of operator in t3 |
| 185 | + beq zero,zero, print_test_message_op |
| 186 | + print_test_message_int_mul: |
| 187 | + addi t0, zero, 2 |
| 188 | + bne a1, t0, print_test_message_int_div |
| 189 | + la a0, mul_str |
| 190 | + addi t3, zero, 42 #store ascii code of operator in t3 |
| 191 | + beq zero,zero, print_test_message_op |
| 192 | + print_test_message_int_div: |
| 193 | + la a0, div_str |
| 194 | + addi t3, zero, 47 #store ascii code of operator in t3 |
| 195 | + print_test_message_op: |
| 196 | + li a7, 4 |
| 197 | + ecall |
| 198 | + |
| 199 | + #print operation, operands, result |
| 200 | + li a7, 11 |
| 201 | + li a0, 91 #display '[' |
| 202 | + ecall |
| 203 | + li a7, 1 |
| 204 | + addi a0, a2, 0 #display 1st operand |
| 205 | + ecall |
| 206 | + li a7, 11 |
| 207 | + addi a0, t3, 0 #display operator |
| 208 | + ecall |
| 209 | + li a7, 1 |
| 210 | + addi a0, a3, 0 #display 2nd operand |
| 211 | + ecall |
| 212 | + li a7, 11 |
| 213 | + addi a0, zero, 61 #display '=' |
| 214 | + ecall |
| 215 | + li a7, 1 |
| 216 | + addi a0, t2, 0 #display result |
| 217 | + ecall |
| 218 | + addi t0, zero, 3 |
| 219 | + bne a1, t0, print_success_cnt |
| 220 | + li a7, 11 |
| 221 | + li a0, 44 #display ',' |
| 222 | + ecall |
| 223 | + li a7, 1 |
| 224 | + addi a0, a4, 0 #display remainder if the operation is division |
| 225 | + ecall |
| 226 | + print_success_cnt: |
| 227 | + li a7, 11 |
| 228 | + li a0, 93 #display ']' |
| 229 | + ecall |
| 230 | + li a0, 44 #display ',' |
| 231 | + ecall |
| 232 | + li a7, 1 |
| 233 | + addi a0, t2, 0 |
| 234 | + ecall |
| 235 | + la a0, success_str |
| 236 | + li a7, 4 |
| 237 | + ecall |
| 238 | + li a7, 1 |
| 239 | + add a0, zero, a5 #display the success count |
| 240 | + ecall |
| 241 | + li a7, 4 |
| 242 | + la a0, total_testnum_str |
| 243 | + ecall |
| 244 | + |
| 245 | + li a7, 11 |
| 246 | + addi a0, zero, 10 #next line |
| 247 | + ecall |
| 248 | + |
| 249 | + |
| 250 | + lw a0, 0(sp) |
| 251 | + lw a1, 8(sp) |
| 252 | + lw a7, 16(sp) |
| 253 | + addi sp, sp, 24 |
| 254 | + |
| 255 | + jalr x0, 0(ra) |
0 commit comments