Jump to content

need help ASAP - C++ to assembly conversion.

- - - - -

  • Please log in to reply
1 reply to this topic

#1
h_005

h_005

    Newbie

  • Members
  • Pip
  • 1 posts
I need to create a assembly code for the given C++ code with conditions given.
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


#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
We don't do your homework for you. We'll help, though. What do you have so far?
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users