How multiplication is realized in a simple computer processor?24 Apr 2007, 534 read(s) Tags:
The assembly language can explain us how a computer actually works and thinks. As there is no multiplication function in a computer processor (I'm not sure if there is any in the newer ones, but I speak mainly for 8086 and the x86 family), I decided to find out how multiplication is realized through addition....
Sounds quite simple. Can you do it? Do you know how the computer calculates the simple x*y you write in Pascal or any other high-level language?
Calculating x*y... get mov bx, ax get mov cx, 0 a: add cx, bx sub ax, 1 cmp ax, 0 ja a halt
What does this do and how?
First of all, this code gets two variables (X and Y) and stores them in two different registers on the processor. We are using the registers and not the memory here, because the registers work faster. Then, the code uses a third register to calculate the sum. This is done via a simple loop: the value of X is added Y times to this third register. Differently from high-level languages, we cannot create a for loop here. Instead, each time we add X to the third register, we decrease the value of Y with 1. Then, we check if Y is above zero. If it is, we continue to add. If not, we have the awaited result in the third register.
Comments
|