It first initializes a large 2 dimensional array with some non-zero random values using the random_value() function (don't care about its internals but assume it returns a random unsigned integer value)1000000. It then loops over the array 10 times to smooth it out.
unsigned char a[1000000][1000000];
for (unsigned int j=0; j<1000000; j++) {
for (unsigned int i=0; i<1000000; i++) {
a[i][j] = random_value() % 256;
if (a[i][j] == 0) {
a[i][j]=1;
}
}
}
for (unsigned int k=0; k<10; k++) {
for (unsigned int j=1; j<999999; j++) {
for (unsigned int i=1; i<999999; i++) {
a[i][j] = (a[i-1][j-1] + a[i][j-1] + a[i+1][j-1] +
a[i-1][j ] + a[i][j ] + a[i+1][j ] +
a[i-1][j+1] + a[i][j+1] + a[i+1][j+1]) / 9;
}
}
}
1. Write the program above in assembly form using the kinds of instructions listed below. You can assume the large array a[][] is already allocated at M[R1] and that R1 is initialized for you. Note that if you change R1, you should make sure to have a way to get its value back. Otherwise you will not be able to access the array (a) anymore. Also note that memory is byte addressable and the array is allocated in row-major-order. This means M(address) returns the byte stored at the address and that M(1000) is array element a[0][0], M(1001) is a[0][1], M(1002) is a[0][2], M(1000+1000000) is a[1][0], and so forth.
LOADi R, address // R = M(address) STOREi R, address // M(address) = R LOAD R, Rm // R = M(Rm) STORE R, Rm // M(Rm) = R ADD Rs1, Rs2, Rd // Rd = Rs1 + Rs2 DIV Rs1, Rs2, Rd // Rd = Rs1 / Rs2 MOD Rs1, Rs2, Rd // Rd = Rs1 % Rs2 MOV Rs, Rd // Rd = Rs MOVi Rs, val // Rs = val JLT R, val, LABEL // if (R < val) jump to LABEL JGT R, val, LABEL // if (R > val) jump to LABEL JEQ R, val, LABEL // if (R == val) jump to LABEL JLE R, val, LABEL // if (R <= val) jump to LABEL JGE R, val, LABEL // if (R >= val) jump to LABEL JNE R, val, LABEL // if (R != val) jump to LABEL J LABEL // jump to LABEL JSUB LABEL // jumps to function at LABEL (can return with RET) RET // return to the address after the last JSUB.
You can assume the random_value() function is at label RANDOM_VALUE. After the function returns, the value in register R2 will have a new random value for you to use. Note that the function overwrites register R2. Hint: Use JSUB for this.
Edited by dargueta, 04 November 2010 - 10:51 AM.
Added code tags


Sign In
Create Account

Back to top









