066.矩阵行求和

       在本程序中使用了eltType参数,用于为程序指定用户所传参数的类型。其中movzx的作用是使得计算的结果在内存中对其,以加速CPU的执行效率。值得注意的是,带参宏中使用了通用寄存器,为了不导致因为宏内部的逻辑使得外部的寄存器发生变化,这里将其保存在栈中。宏中所涉及到的参数最终都保存在通用寄存器中以供调用。在宏里面,属于局部的变量,标号需要事先声明才可以使用,所以,用于循环的标号L1在宏的开始位置被声明为LOCAL变量。
title Row Sum Calculation	(RowSumMacro.asm)

Comment!
	Tests the mCal_row_sum macro.
!

include irvine32.inc

;-------------------------------------------------------

mCalc_row_sum MACRO index,arrayOffset,rowSize,eltType

;Calculates the sum of a row in a 2-dim array
;Receives:row index,offset of the array,number of bytes
;in each table row ,and the array type(byte word or dword)
;returns:EAX=sum
;--------------------------------------------------------
local L1
	push	ebx					;save changed regs
	push	ecx
	push	esi

								;set up the required register
	mov		eax,	index
	mov		ebx,	arrayOffset
	mov		ecx,	rowSize

								;calculate the row offset
	mul		ecx
	add		ebx,	eax
		
								;prepare the loop counter
	;byte=0	word=1 dword=2
	shr		ecx,	(TYPE eltType/2)

	;initialize the accumulator and column indexes
	mov		eax,	0			;accumulator
	mov		esi,	0			;column index

L1:
	IFIDNI	<eltType>,<DWORD>
			mov		edx,	eltType PTR[ebx+esi*(TYPE eltType)]
	ELSE
			movzx	edx,	eltType PTR[ebx+esi*(TYPE eltType)]
	ENDIF

	add		eax,	edx			;累加操作
	inc		esi
	loop	L1


	pop		esi
	pop		ecx
	pop		ebx

ENDM


.data
	tableB BYTE 10h, 20h, 30h, 40h, 50h
	RowSizeB = ($ - tableB)

	tableW WORD 10h, 20h, 30h, 40h, 50h
	RowSizeW = ($ - tableW)

	tableD DWORD 10h, 20h, 30h, 40h, 50h
	RowSizeD = ($ - tableD)

	index DWORD ?


.code
	main PROC
		
		;Demostrate Base-Index mode:
			
			mCalc_row_sum index,OFFSET tableB,RowSizeB,BYTE
			call WriteHex
			call Crlf

			mCalc_row_sum index,OFFSET tableW,RowSizeW,WORD
			call WriteHex
			call Crlf

			mCalc_row_sum index,OFFSET tableD,RowSizeD,DWORD
			call WriteHex
			call Crlf

			call WaitMsg

			exit
main ENDP
End main
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值