060.计算二维数组某一行的和

这是一个通用的写法,如果想要计算word型或者其他类型的大小的数据,可以在movzx edx,BYTE PTR[ebx + esi*type BYTE] 中更换 type BYTE为 type <your type> 这样可使得程序更加通用。
; Row Sum Calculation					(RowSum.asm)

; This program demonstrates the use of Base-Index addressing 
; with a two-dimensional table array of bytes (a byte matrix).

INCLUDE Irvine32.inc

.data
tableB  BYTE  10h,  20h,  30h,  40h,  50h
        BYTE  60h,  70h,  80h,  90h,  0A0h
        BYTE  0B0h, 0C0h, 0D0h, 0E0h, 0F0h
RowSize = 5
msg1	BYTE "Enter row number: ",0
msg2 BYTE "The sum is: ",0

.code
main PROC

; Demonstrate Base-Index mode:

	mov	  edx,OFFSET msg1			; "Enter row number:"
	call  WriteString
	call  Readint					; EAX = row number

	mov	  ebx,OFFSET tableB
	mov	  ecx,RowSize
	call  calc_row_sum				; EAX = sum
   
	mov	  edx,OFFSET msg2			; "The sum is:"
	call  WriteString
	call  WriteHex					; write sum in EAX
	call  Crlf
	call WaitMsg

	exit
main ENDP


;------------------------------------------------------------
calc_row_sum PROC uses ebx ecx edx esi
;
; Calculates the sum of a row in a byte matrix.
; Receives: EBX = table offset, EAX = row index, 
;		    ECX = row size, in bytes.
; Returns:  EAX holds the sum.
;------------------------------------------------------------

	mul	 ecx			; row index * row size
	add	 ebx,eax		; row offset
	mov	 eax,0			; accumulator
	mov	 esi,0			; column index
	
L1:	movzx edx,BYTE PTR[ebx + esi*type BYTE]		; get a byte
	add	 eax,edx								; add to accumulator
	inc	 esi									; next byte in row
	loop L1

	ret
calc_row_sum ENDP

END main
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值