Assembly is allowing my favorite activity to take place, and leveling me off quite well. Efficiency is something that is highly important to me, so when i am given a lab that is about algorithms i become giddy! Todays lab is about calculating the dot product of a multiple dimensioned vector, lets walk through this.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; PROC dotProduct: calculate the dot product of 2 vectors the 2 vectors can be ; 2D, 3D or 4D ; require: address of the 2 vectors in esi and edi constant DIM is set ; return: resulting dot product in eax dotProduct PROC ; Move Dimensions into counter mov ecx, DIM mov eax, 0 ; Clear out EAX register SCALARPROD: mov bx, WORD PTR [edi] ; Move the first word into BX mov dx, WORD PTR [esi] ; Again for DX MULTIPLICATION: ; otherwise we need to do some other arithmetic shr bx, 1 jnc CLEAR ; If the carry is clear we didnt shift any bits out =( add ax, dx ; but otherwise we need to add our value in CLEAR: shl dx, 1 test bx, 0ffffh ; See if there are any bits on jne MULTIPLICATION add ESI, 2 add EDI, 2 LOOP SCALARPROD ret ; go back to main proc dotProduct ENDP

As you can probably see here, stepping through the algorithm is quite interesting.

**Notes**

- DIM is a predefined value that is controlling the dimensions of the vector.

**Walkthrough**

- Move the dimensions into the counter register –
**ECX** - Clear out the EAX Register
- Begin Processing a dimension
- Move the values of the pointers into our DX and BX registers
- Begin the multiplication loop
- Shift the BX register one bit to the right
- if the shift contained a bit in the lowest address (the carry flag is set) we need to add the value to the AX register
- Check the BX register for any further value (Any bits turned on)
- If it does, shift the DX register to the left and repeat this loop

- We have now finished multiplication, Move the pointers to the next location
- Call the loop, which will decrement the counter

- Return

For those of you who are not as well learned in ASM as I am, this code allows us to do multiplication without using a temporary variable! You may be asking what that means, Why do i need a temp variable to step through a vectors values, multiply them and return them? Sounds like 3 variables and we have three registers. Well! Check this out.

Multiplication in assembly is a single operand instruction, meaning it only accepts a single operand, and it multiplies that operator by AX. The catch being that the results of AX and BX is thrown into DX:AX! Is that not fucking insane? What this means int he scope of our application is that since we are using AX and DX and have to use BX for the input to the multiplication and we need to keep track of the dimensions with ECX we have run out of useful general purpose registers =( You can of course destroy the BX value each time by storing it in DX, but that is not a nice way to develop.

Yay for programming! Super cool!