Getting back into the code

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


	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

	shl dx, 1

	test bx, 0ffffh	; See if there are any bits on

	add ESI, 2
	add EDI, 2


	ret		; go back to main proc
dotProduct ENDP

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


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


  1. Move the dimensions into the counter register – ECX
  2. Clear out the EAX Register
  3. Begin Processing a dimension
    1. Move the values of the pointers into our DX and BX registers
    2. Begin the multiplication loop
      1. Shift the BX register one bit to the right
      2. 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
      3. Check the BX register for any further value (Any bits turned on)
      4. If it does, shift the DX register to the left and repeat this loop
    3. We have now finished multiplication, Move the pointers to the next location
    4. Call the loop, which will decrement the counter
  4. 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!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.