Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need help rotating an image using assembly language plz help // - This function

ID: 3793222 • Letter: N

Question

need help rotating an image using assembly language plz help

// - This function rotates a square sized color image 90 degress counterclockwise.
// - The width and height of the image are both equal to dim.
// - Four variables (i0, i90, i180, i270) are defined that you may use in your implementation for
//   temporary data storage. You are not allowed to define additional variables.
//
void imageRotation(unsigned int* image, int dim) {

unsigned int i0, i90, i180, i270;

__asm {

  // YOUR CODE STARTS HERE

  // Iterate over the red triangle (row by row starting from top left)

  // compute index of pixel p0, find the corresponding memory address and store it in i0

  // compute index of pixel 90, find the corresponding memory address and store it in i270

  // compute index of pixel p180, find the corresponding memory address and store it in i180

  // compute index of pixel 270, find the corresponding memory address and store it in i90

  // rotationaly swap pixel values: p0 -> p90 -> p180 -> p270 -> p0

  // YOUR CODE ENDS HERE
}
}

Explanation / Answer

void imageRotation(unsigned int* image, int dim) { unsigned int i0, i90, i180, i270; __asm { push ebx; push edi; push esi; // YOUR CODE STARTS HERE //Load Variables mov ecx, image mov ebx, 0 //n //Begin Loops mov esi, 0 BEGIN_ROW: //From 0 -> (dim / 2) + 1 mov edx, dim //dim / 2 + 1 shr edx, 1 inc edx cmp esi, edx jge END_ROW mov edi, ebx BEGIN_COL: //From n -> dim - n - 1 mov edx, dim sub edx, ebx dec edx cmp edi, edx jge END_COL //Find Addresses //i0: base + 4 * (row * dim + col) mov eax, esi mov edx, dim mul edx add eax, edi shl eax, 2 add eax, ecx mov i0, eax //i90: base + 4 * (col * dim + (dim - row - 1)) = base + 4 * (col * dim + dim - row - 1) mov eax, edi mov edx, dim mul edx add eax, dim sub eax, esi dec eax shl eax, 2 add eax, ecx mov i90, eax //i180: base + 4 * ((dim - row - 1) * dim + (dim - col - 1)) = base + 4 * (dim * (dim - row) - col - 1) mov eax, dim sub eax, esi mov edx, dim mul edx sub eax, edi dec eax shl eax, 2 add eax, ecx mov i180, eax //i270: base + 4 * ((dim - col - 1) * dim + row) = base + 4 * (dim * (dim - col) - dim + row) mov eax, dim sub eax, edi mov edx, dim mul edx sub eax, dim add eax, esi shl eax, 2 add eax, ecx mov i270, eax //Rotato mov edx, i0 mov edx, [edx] mov eax, i0 mov ecx, i270 mov ecx, [ecx] mov [eax], ecx mov eax, i270 mov ecx, i180 mov ecx, [ecx] mov [eax], ecx mov eax, i180 mov ecx, i90 mov ecx, [ecx] mov [eax], ecx mov eax, i90 mov [eax], edx mov ecx, image inc edi jmp BEGIN_COL END_COL: inc esi inc ebx jmp BEGIN_ROW END_ROW: mov image, ecx // YOUR CODE ENDS HERE pop esi; pop edi; pop ebx; } }