Assemble language // - This function takes in a square sized grayscale image and
ID: 3798229 • Letter: A
Question
Assemble language
// - This function takes in a square sized grayscale image and applies thresholding on each pixel.
// i.e. it should change pixel values according to this formula:
// 0xFF if x >= threshold
// 0x00 if x < threshold
// - The width and height of the image are equal to dim.
// - You are not allowed to define additional variables.
//
void imageThresholding(unsigned char* image, int dim, unsigned char threshold) {
__asm {
mov eax, image
mov edi, 0
BEGIN_FOR_ROW:
cmp edi, dim
jge END_FOR_ROW
mov esi, 0
BEGIN_FOR_COL :
cmp esi, dim
jge END_FOR_COL
mov ebx, 0 //Transfer row to ebx
mov edx, 0 //Multiply row by dim (add row to row dim times)
BEGIN_FOR_MUL :
cmp edx, dim
jge END_FOR_MUL
add ebx, edi
inc edx
jmp BEGIN_FOR_MUL
END_FOR_MUL :
add ebx, esi
xor edx, edx
mov dl, [eax + ebx]
mov cl, threshold
and cl, dl
cmp cl, 0x00
jne IF_HIGHER
xor edx, edx //set to Min
jmp CONT
IF_HIGHER :
or edx, 0xFF
CONT :
mov[eax + ebx], dl
inc esi
jmp BEGIN_FOR_COL
END_FOR_COL :
inc edi
jmp BEGIN_FOR_ROW
END_FOR_ROW :
mov image, eax
}
}
Won't printing out the right result,
Test driver
imageThresholding(&testStr1_1[0][0], 3, 90);
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j) {
if (testStr1_1[i][j] != exptectedStr1_1[i][j]) {
std::cout << "Part 1: Test 1 failed at index: " << i << "," << j << " got: " << int(testStr1_1[i][j]) << " expected: " << int(exptectedStr1_1[i][j]) << std::endl;
failed = true;
}
}
if (!failed)
std::cout << "Part 1: Test 1 passed." << std::endl;
Explanation / Answer
x86 code to utilize imageThresholding function:
void imageThresholding(unsigned char* image, int dim)
{
--asm
{
//compiler command line option
push ebx;
push edi;
push esi;
mov eax,
image mov edi,
BEGIN_FOR_ROW:
cmp edi, dim
jge END_FOR_ROW
mov esi, 0
BEGIN_FOR_COL :
cmp esi, dim
jge END_FOR_COL
mov ebx, 0 //Transfer row to ebx
mov edx, 0 //Multiply row by dim (add row to row dim times)
BEGIN_FOR_MUL :
cmp edx, dim
jge END_FOR_MUL
add ebx, edi
inc edx
jmp BEGIN_FOR_MUL
END_FOR_MUL :
add ebx, esi
xor edx, edx
mov dl, [eax + ebx]
mov cl, threshold
and cl, dl
cmp cl, 0x00
jne IF_HIGHER
xor edx, edx //set to Min
jmp CONT
IF_HIGHER :
or edx, 0xFF
CONT :
mov[eax + ebx], dl
inc esi
jmp BEGIN_FOR_COL
END_FOR_COL :
inc edi
jmp BEGIN_FOR_ROW
END_FOR_ROW :
mov image, eax
}
}
Test driver
imageThresholding(&testStr1_1[0][0], 3, 90);
for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j) {
if (testStr1_1[i][j] != exptectedStr1_1[i][j]) {
std::cout << "Part 1: Test 1 failed at index: " << i << "," << j << " got: " << int(testStr1_1[i][j]) << " expected: " << int(exptectedStr1_1[i][j]) << std::endl;
failed = true;
}
}
if (!failed)
std::cout << "Part 1: Test 1 passed." << std::endl;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.