Question

In: Computer Science

This problem is adapted from an earlier edition of P&H. Consider the following code used to...

This problem is adapted from an earlier edition of P&H.

Consider the following code used to implement the instruction: foo $s0,$s1,$s2

mask: .word 0xFFFFF83F

start: la $t0,mask

lw $t0,0($t0)

la $s0,shftr

lw $s0,0($s0)

and $s0,$s0,$t0

andi $s2,$s2,0x1f

sll $s2,$s2,6

or $s0,$s0,$s2

la $t5,shftr

sw $s0,0($t5)

shftr: sll $s0,$s1,0

Add meaningful comments to the code. Please explain each line and write a paragraph describing how it works. Why do you suppose that writing “self-modifying code” such as this is a bad idea (and often times not actually allowed)?

Please explain it. thank you.

Note: We have not covered the .word directive yet. This is an assembler directive, which is essentially an instruction to the assembler (NOT an assembly language instruction). In this case, it tells the assembler to reserve a word of memory initialized to 0xfffff83f. Mask is the address of this word (just as start is the address of the word in memory containing the machine code for that lw instruction).

Solutions

Expert Solution

Answer:

mask: .word 0xFFFFF83F

start: la $t0,mask   //$t0 = 0xfffff83f, 1111111...100000111111 ( meaning word is loaded to register t0 )

lw $t0,0($t0) //load word at RAM address ($t0+0) into register $t0

la $s0,shftr //$s0 = sll instruction( shifting to left) means loading the shift instruction.

lw $s0,0($s0)    // clear the shamt

and $s0,$s0,$t0 //clears the shamt field in the sll instruction stored in $s0

andi $s2,$s2,0x1f   //clear all but the last 5 bits in $s2

sll $s2,$s2,6 //move those bits to the position that corresponds to shamt

or $s0,$s0,$s2 //set the shamt field in $s0 as the value from $s2

la $t5,shftr // load all sll instruction in register t5

sw $s0,0($t5)   //store the sll instruction back into memory

shftr: sll $s0,$s1,0 //shifts the contents of $s0 left 0 which is stored in $s0. perform the modified instr.

The last shift instruction is modified so that its shamt field is written by the 5 LSBs from $s2. It's actually modifying the instruction in-memory to perform the shift.

self modifying means we  are changing code generated by the compiler which will make our code machine dependent.because of this it  is bad idea as it complicates the instruction cache implementation, the instruction cache is usually read only cache. Also it makes detecting defects that overwrite code harder to detect.


Related Solutions

Consider the Hamiltonian of a particle in one-dimensional problem defined by: H = 1 2m P...
Consider the Hamiltonian of a particle in one-dimensional problem defined by: H = 1 2m P 2 + V (X) where X and P are the position and linear momentum operators, and they satisfy the commutation relation: [X, P] = i¯h The eigenvectors of H are denoted by |φn >; where n is a discrete index H|φn >= En|φn > (a) Show that < φn|P|φm >= α < φn|X|φm > and find α. Hint: Consider the commutator [X, H] (b)...
Pricing with increasing returns to scale. Consider the following production function (similar to that used earlier...
Pricing with increasing returns to scale. Consider the following production function (similar to that used earlier for ColdAway): Y = 100 * (L - F), where Y is output, L is labor input, and F is a fixed amount of labor that is required before the first unit of output can be produced (like a research cost). We assume that Y = 0 if L < F. Each unit of labor L costs the wage w to hire. (a) How...
Consider the hypothesis test below. H 0:  p 1 -  p 2  0   H a:  p 1 -  p 2 >...
Consider the hypothesis test below. H 0:  p 1 -  p 2  0   H a:  p 1 -  p 2 > 0 The following results are for independent samples taken from the two populations. Sample 1 Sample 2 n1 = 100 n2 = 300 p1 = 0.24 p2 = 0.13 Use pooled estimator of p. What is the value of the test statistic (to 2 decimals)?   What is the  p-value (to 4 decimals)?   With   = .05, what is your hypothesis testing conclusion?
Consider the following hypothesis test: H 0:   50 H a:  > 50 A sample of 70 is used...
Consider the following hypothesis test: H 0:   50 H a:  > 50 A sample of 70 is used and the population standard deviation is 7. Use the critical value approach to state your conclusion for each of the following sample results. Use  = .05. a. With x = 52.5, what is the value of the test statistic (to 2 decimals)? b.  With x = 51, what is the value of the test statistic (to 2 decimals)? c. With x = 51.8, what is the...
Consider the following hypothesis test: H 0:   50 H a:  > 50 A sample of 50 is used...
Consider the following hypothesis test: H 0:   50 H a:  > 50 A sample of 50 is used and the population standard deviation is 7. Use the critical value approach to state your conclusion for each of the following sample results. Use  = .05. a. With x (with line over top of x) = 52.5, what is the value of the test statistic (to 2 decimals)? b. With x = 51, what is the value of the test statistic (to 2 decimals)? c....
Consider the following hypothesis test: H 0:   50 H a:  > 50 A sample of 55 is used...
Consider the following hypothesis test: H 0:   50 H a:  > 50 A sample of 55 is used and the population standard deviation is 7. Use the critical value approach to state your conclusion for each of the following sample results. Use  = .05. a. With  = 52.5, what is the value of the test statistic (to 2 decimals)? Can it be concluded that the population mean is greater than 50? SelectYesNoItem 2 b. With  = 51, what is the value of the test statistic...
Consider the following hypothesis test: H 0:   50 H a:  > 50 A sample of 65 is used...
Consider the following hypothesis test: H 0:   50 H a:  > 50 A sample of 65 is used and the population standard deviation is 7. Use the critical value approach to state your conclusion for each of the following sample results. Use  = .05. a. With  = 52.5, what is the value of the test statistic (to 2 decimals)? Can it be concluded that the population mean is greater than 50? SelectYesNoItem 2 b. With  = 51, what is the value of the test statistic...
Consider the following hypothesis test: H 0:   50 H a:  > 50 A sample of 60 is used...
Consider the following hypothesis test: H 0:   50 H a:  > 50 A sample of 60 is used and the population standard deviation is 7. Use the critical value approach to state your conclusion for each of the following sample results. Use  = .05. a. With  = 52.5, what is the value of the test statistic (to 2 decimals)?    Can it be concluded that the population mean is greater than 50? SelectYesNoItem 2 b. With  = 51, what is the value of the test...
Consider the following hypothesis test: H 0:   50 H a:  > 50 A sample of 55 is used...
Consider the following hypothesis test: H 0:   50 H a:  > 50 A sample of 55 is used and the population standard deviation is 8. Use the critical value approach to state your conclusion for each of the following sample results. Use  = .05. With  = 52.5, what is the value of the test statistic (to 2 decimals)?    Can it be concluded that the population mean is greater than 50? SelectYesNoItem 2 b. With  = 51, what is the value of the test statistic...
Consider the following hypotheses. Ho p<0.16 H, p>0.16 Given that p=0.2 n = 140 and a=005,...
Consider the following hypotheses. Ho p<0.16 H, p>0.16 Given that p=0.2 n = 140 and a=005, answer the following questions a. What conclusion should be drawn? b. Determine the p-value for this test. a. Determine the critical value(s) of the test statistic
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT