How to handle a 2D array in an assembly

I was reading a book on assembly programming and I have a practice problem that I cannot figure out. The problem is that given the function prototype

void multiply2D (unsigned char image[numRows][numCols], int numRows, int numCols,
    int factor)

      

I need to find a way to write a function in an assembly that multiplies the entire array by factor

without using a nested loop.

I understand that the 2D array will decay into one line array in an assembly, however I cannot figure out an algorithm to indicate when to stop (i.e. the end of the array reached). I was thinking about a simple column and row multiplication to get the total size, however it states that the size of the row by column multiplication may not fit into an integer.

+3


source to share


1 answer


In this case, the 2D array is a full linear array with a size numRows * numCols

. So you have to do a 32x32 = 64 multiplication where the 64 bit answer will be in some register somewhere (see the assembler link for the instruction you need).

In psuedo code, you need:

reg_64 rsize = numRows * numCols
reg_64 count = 0

while( count < rsize )
    image[count] *= factor

      



i7 has 64-bit register access, so you have access to 64-bit type.

If you are really struggling, you can of course write it in C and compile it and then disassemble it with objdump to see how the compiler solves the problem! It may not be the most effective, but at least it will be the answer!

+2


source







All Articles