036.比较利用移位运算和mul指令计算乘法的效率差距

; Comparing Multiplications         (CompareMult.asm)

; This program compares the execution times of two approaches to 
; integer multiplication: Binary shifting versus the MUL instruction.

INCLUDE Irvine32.inc

LOOP_COUNT = 0FFFFFFFFh		;一共计算4,294,967,295次乘法‬

.data
intval DWORD 5
startTime DWORD ?

.code
main PROC

; First approach:
							;使用移位的方式计算乘法
	call	GetMseconds		; get start time
	mov	startTime,eax
	
	mov	eax,intval			; multiply now
	call	mult_by_shifting
	
	call	GetMseconds		; get stop time
	sub	eax,startTime
	call	WriteDec		; display elapsed time
	call	Crlf

; Second approach:
							;使用mul指令来计算乘法
	call	GetMseconds		; get start time
	mov	startTime,eax		; 开始计时
	
	mov	eax,intval
	call	mult_by_MUL

	call	GetMseconds		; get stop time
	sub	eax,startTime		; 计算出计时结束时间与计时开始时间的时间间隔
	call	WriteDec		; display elapsed time 利用十进制格式向控制台显示一个32位无符号整数
	call	Crlf

	exit
main ENDP


;---------------------------------
mult_by_shifting PROC
	;
	; Multiplies EAX by 36 using SHL
	;    LOOP_COUNT times.
	; Receives: EAX
	;---------------------------------

		mov	ecx,LOOP_COUNT
	
	L1:	push	eax			; save original EAX
		mov	ebx,eax
		shl	eax,5			;  x*36=(x<<5)+(x<<2)
		shl	ebx,2
		add	eax,ebx
		pop	eax			    ; restore EAX
		loop	L1
	
		ret
	mult_by_shifting ENDP


;---------------------------------
mult_by_MUL PROC
	;
	; Multiplies EAX by 36 using MUL
	;    LOOP_COUNT times.
	; Receives: EAX
	;---------------------------------

		mov	ecx,LOOP_COUNT
	
	L1:	push	eax			; save original EAX
		mov	ebx,36
		mul	ebx
		pop	eax			; restore EAX
		loop	L1
	
		ret
	mult_by_MUL ENDP

END main

猜你喜欢

转载自blog.csdn.net/dosdiosas_/article/details/106318744