Assignment 9, due Oct 31Solutions
Part of
the homework for CS:2630 (22C:60), Fall 2014
|
Some of these symbols are absolute, some are relocatable. In order to answer the following, questions, you will need to work out which is which.EXT A INT B B: W 5 C = 5 D = . COMMON E,4
For each of the following expressions, is it legal? Yes or no. If it is legal and absolute, what is the value of the expression? If it is legal and relocatable, what is its value relative to what relocation base? If it is illegal, what is the problem? (0.2 points each)
a) A + 1
Legal, the value is 1 relative to the relocation base A.
b) B + A
Illegal, A and B are both relocatable.
c) C - B
Illegal, absolute C minus relocatable A.
d) D + C
Legal, the value is 9 relative to the default relocation base. Note that D is 4 relative to the default relocation base, and C is absolute 5.
e) E - D
Illegal, E and D are relocatable relative to different relocaiton bases.
Note: This topic is discussed in Chapters 5 and 10 of the notes, and in Sections 2.3 and 4 of the SMAL manual.
; bigint.h ; interface specification for bigint.a, high precision integer arithmetic BIGINTSZ = 16 ; size in bytes of an bigint object ; note: this is set for 128 bit objects, ; but this can be changed for other versions EXT BIGADD ; bigadd(a,b,c): a = b + c EXT BIGSUB ; bigsub(a,b,c): a = b - c EXT BIGMUL ; bigmul(a,b,c): a = b * c EXT BIGDIV ; bigdiv(a,b,c): a = b / c ; ; uses R3 = pointer to operand a ; ; uses R4 = pointer to operand b ; ; uses R5 = pointer to operand c
Note that one thing you probably want to be able to do is write applications that use the bigint package and then, later, change BIGINTSZ when you discover that the size you were using wasn't big enough (or perhaps it was too big).
a) Write SMAL code to declare a global variable called X that is a bigint. (0.5 points)
COMMON X,BIGINTSZ
b) Here is the SMAL code for an activation record of a subroutine:
;RETAD = 0 ; the return address INDEX = 4 ; a 32 bit integer ARSIZE = 8
Rewrite the activation record to include two local variables that hold bigints, one called Y and the other called Z. Your code should be resiliant enough that the activation record automatically rearranges itself if someone changes BIGINTSZ. (0.5 points)
Here is a solution that works but is a bit ugly because the size of each item is given on the next line:
;RETAD = 0 ; the return address INDEX = 4 ; a 4-byte integer Y = INDEX + 4 ; a bigint Z = Y + BIGINTSZ ; a bigint ARSIZE = Z + BIGINTSZHere is a solution that is long winded but keeps the items untangled from each other; now, each bigint declaration takes two lines:
;RETAD = 0 ; the return address INDEX = 4 ; a 4-byte integer ARSIZE = 8 Y = ARSIZE ; a bigint ARSIZE = ARSIZE + BIGINTSZ Z = ARSIZE ; a bigint ARSIZE = ARSIZE + BIGINTSZ
c) Write SMAL Hawk code to call bigsub(x,y,z). Assume that R2 usage has not been optimized. The variables x, y and z are the variables you defined in parts a and b above. (0.5 points)
LIL R3,X LEA R4,R2,Y LEA R5,R2,Z ADDI R2,R2,ARSIZE LIL R1,BIGSUB JSRS R1,R1 ADDI R2,R2,-kARSIZE
;RETAD = 0 ; the return address INDEX = 4 ; a 32 bit integer ARSIZE = 8
With a bit of cleverness, we could solve problem 2b above by writing activation records like this, instead:
NEWAR LOCAL INDEX,INTSIZE LOCAL Y,BIGINTSZ LOCAL Z,BIGINTSZ
The net effect of these macros should be that ARSIZE is set correctly and the identifiers INDEX, Y and Z are defined just as they were by your answer to problem 2b.
A Problem: Write the code for the macros NEWAR and LOCAL. (0.5 points)
MACRO NEWAR ARSIZE = 4 ENDMAC MACRO LOCAL name,=size name = ARSIZE ARSIZE = ARSIZE + size ENDMACNote above that the second macro parameter is passed by value (indicated by the equals sign). This is not strictly needed, although if the parameter is passed as literal text, it really ought to be rewritten as follows:
MACRO LOCAL name,size name = ARSIZE ARSIZE = ARSIZE + (size) ENDMACThe parentheses around the use of the parameter guarantee that the entire parameter will be examined and treated as a single value.