Could you help me to have some idea of how to solve the following exercise:
The following Matlab code allows you to recursively determine the integer part of the square root of a number. Write the equivalent assembly code and determine the approximate value of the root of 78999999, in 10, 50, 100 and 500 iterations. The results should be displayed on the console. Then determine the value N, where the closest integer value to the root of that number is obtained.
Attached code of the function in Matlab:
N = 10
i_number = 78999999;
i_sqrt = i_number;
for i = 1:N
i_sqrt = floor(((i_number / i_sqrt) + i_sqrt) / 2);
end
Thank you
I'm not very into the x86 assembler but I hope I can give you an idea of how to start and set up the problem. The first thing we must do is initialize the variables as indicated by the MatLab code. To do this, we create a label with the name of our variable and assign it the corresponding type
The labels are created in the form:
In this case, the data type corresponds to long , which is used to declare labels (variables) of integer type and size 8 bytes . Next, if we want to set the value of one variable equal to another, we must use the
.zero 4
. This is due to the FLAGS register which are the status registers in Intel x86 microprocessors that contain the current state of the processor. This register is 16 bits wide (which is the same as saying two to the power of 4, since all registers are already given in base 2).The code presents a general structure that is formed:
Therefore, the initialization of corresponding variables would be:
Next, it is mandatory to have a main tag . It is created in the same way as we saw before. That main tag will include all instructions that are not global variable initialization. Therefore, since the MatLab code includes a loop
for
we can use the BX register to count the loops (iterations)After the main tag we can include a new tag called for example
startloop
to store the loopfor
Finally we perform the operation
i_sqrt = floor(((i_number/i_sqrt)+i_sqrt)/2);
For this we make use of the corresponding operations:
fidiv (integer division)
fidd (Sum)