Assignment 6, due Feb 27Solutions
Part of
the homework for CS:2630, Spring 2015
|
A = #12345
Suppose B is a constant, declared with a SMAL statement like:
B: W #1234567
Suppose C is a global variable, declared with a SMAL statement like:
COMMON C,4 ; an integer
Suppose D is a local variable, declared with a SMAL statement like:
D = 8 ; an integer
A problem: Write SMAL Hawk code to compute the sum of the values of A, B, C and D. If you were writing this in C or Python, the solution might be as simple as A+B+C+D because the language keeps track of the types of the operands. In assembly language, you might well need to use a different addressing mode for each operand. (1.2 points)
LIL R3,A ; now R3 = A LOAD R4,B ADD R3,R3,R4 ; now R3 = A+B LIL R4,C LOADS R4,R4 ADD R3,R3,R4 ; now R3 = A+B+C LOAD R4,R2,D ADD R3,R3,R4 ; now R3 = A+B+C+D
a) Identify the sub-optimal code. You may do this by quoting the code, along with the lines immediately before and after the less than optimal part. (0.5 points)
BGTU ELSE ; if (i <= 1) { ; ------- then clause ------- BR ENDIF ; } else {
b) Optimize the suboptimal code. Show your result by giving the text you improved along with the lines immediately before and after it. (0.5 points)
BLEU ENDIF ; if (i > 1) {
As it turns out, the keys to this problem are in Chapter 7, which you should start reading before the due date for this assignment. To help motivate that, consider the following question, which would have made a nice part f) for Homework 5, problem 1.
a) Given that R10 points to one record, that is, R10 holds the value of array[i], write SMAL Hawk code to load the value of the X field of that record (array[i]->x) into R3. (0.4 points)
First, here is the solution that follows naturally from Chapter 7:
LEA R4,R10,X ; get the address of X LOADS R3,R4 ; load the word holding X EXTH R3,R3,R4 ; get XWe can optimize this because we know that X is zero:
LOADS R3,R10 ; load XY into R3 EXTH R3,R3,R10 ; get the low 16 bitsAs is always the case, there are other solutions, consider this:
LOADS R3,R10 ; load XY into R3 TRUNC R3,16 ; get the low 16 bits
b) Given that R10 points to one record, as above, write SMAL Hawk code to load the value of the Y field of that record (array[i]->y) into R4. (0.4 points)
Here is the solution that follows naturally from Chapter 7:
LEA R5,R10,Y ; get the address of Y LOADS R4,R5 ; load the word holding Y EXTH R4,R4,R5 ; get YOptimized solutions are left to the reader.
Having completed both of the above, you could now write a call to putat() in order to set things up to output the string at the right place on the screen.