Assignment 5: the Model 200 Calculator
You must implement the model 200 version of the original calculator from assignment 4. It must have the following new-and-improved features:
If you are uncertain about how this works, use the provided ~/../public/calc200 binary to see how it should work.int i; for (i = 0; i < N; i++) tbl[i] = i;It assembles into
li $t0, 0 b test loop: sll $t1,$t0,2 sw $t0,tbl($t1) test: blt $t0,$a1, loop # assume a1 has N # slt $at,$t0,$a1 # bne $at,$zero,loopwhich is really
li $t0, 0 b test loop: sll $t1,$t0,2 lui $at, UPPER(tbl) addu $at, $at, $t1 sw $t0,LOWER(tbl)($at) test: slt $at,$t0,$a1 bne $at,$zero,loopThis loop uses 6N + 2 instructions to initialize a table of N entries.
First, assume N is a multiple of 4. We write the code as
int i, *tblp; for (i = 0, tblp = tbl; i < N; tblp += 4) { tblp[0] = i++; tblp[1] = i++; tblp[2] = i++; tblp[3] = i++; }which assembles into
li $t0, 0 la $t1, tbl # lui $t1,UPPER(tbl); ori $t1,$t1,LOWER(tbl) b test loop: sw $t0,0($t1) addi $t0,$t0,1 # C compilers would use addiu since C has sw $t0,4($t1) # no exceptions for overflows addi $t0,$t0,1 sw $t0,8($t1) addi $t0,$t0,1 sw $t0,12($t1) addi $t0,$t0,1 addiu $t1,$t1,16 test: slt $at,$t0,$a1 bne $at,$zero,loop
The second loop runs N/4 times, each iteration costing 11 instructions. Thus the run time is 11 N / 4 + 4 or 2.75 N + 4. For sufficiently large N, this is more than twice as fast.
To handle an input N that is not a known constant that is a multiple of 4, we do the following:
int i, *tblp, N0; i = 0; tblp = tbl; N0 = N >> 2; /* N div 4 */ switch (N&3) { /* N rem 4 */ case 3: *tblp++ = i++; case 2: *tblp++ = i++; case 1: *tblp++ = i++; } for (; i < N; tblp += 4) { tblp[0] = i++; tblp[1] = i++; tblp[2] = i++; tblp[3] = i++; }which assembles into
li $t0, 0 la $t1, tbl # lui $t1,UPPER(tbl); ori $t1,$t1,LOWER(tbl) sra $t2,$a1,2 # assume a1 has N andi $t3,$a1,3 lw $t3,case_tbl($t3) jr $t3 c3: sw $t0,0($t1) addi $t0,$t0,1 addiu $t1,$t1,4 c2: sw $t0,0($t1) addi $t0,$t0,1 addiu $t1,$t1,4 c1: sw $t0,0($t1) addi $t0,$t0,1 addiu $t1,$t1,4 .data case_tbl: .word test,c1,c2,c3 .text b test loop: sw $t0,0($t1) addi $t0,$t0,1 sw $t0,4($t1) addi $t0,$t0,1 sw $t0,8($t1) addi $t0,$t0,1 sw $t0,12($t1) addi $t0,$t0,1 addiu $t1,$t1,16 test: blt $t0,$a1, loopThere is a little more constant overhead to deal with the portion of the array not covered by the multiple-of-4 loop body, but for large N this is negligible.
bsy@cse.ucsd.edu, last updated