040.使用ADC指令完成扩展加法运算

本文介绍了一个使用汇编语言实现的8字节整数加法程序,通过数组存储两个大整数,并利用寄存器进行逐位相加,最终展示计算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

; Extended Addition Example           (ExtAdd.asm)

; This program calculates the sum of two 8-byte integers.
; The integers are stored as arrays, with the least significant 
; byte stored at the lowest address value.

INCLUDE Irvine32.inc

;数据以小端模式存储
.data
	op1 BYTE 34h,12h,98h,74h,06h,0A4h,0B2h,0A2hf	;A2B2A40674981234
	op2 BYTE 02h,45h,23h,00h,00h,87h,10h,80h		;8010870000234502

	sum BYTE 9 dup(0) 								; result= 0122C32B0674BB5736h

.code
main PROC
													; 寄存器用作加数,被加数,以及合数数组的偏移量
	mov	esi,OFFSET op1								; first operand
	mov	edi,OFFSET op2								; second operand
	mov	ebx,OFFSET sum								; sum operand
	mov	ecx,LENGTHOF op1   							; number of bytes
	call	Extended_Add

; Display the sum.
	
	mov  esi,OFFSET sum
	mov  ecx,LENGTHOF sum
	call	Display_Sum
	call Crlf
	call Waitmsg			;
	exit
main ENDP

;--------------------------------------------------------
Extended_Add PROC
;
; Calculates the sum of two extended integers stored 
; as arrays of bytes.
; Receives: ESI and EDI point to the two integers,
; EBX points to a variable that will hold the sum, and
; ECX indicates the number of bytes to be added.
; Storage for the sum must be one byte longer than the
; input operands.
; Returns: nothing
;--------------------------------------------------------
	pushad
	clc                								; clear the Carry flag

L1:	mov	al,[esi]      								; get the first integer
	adc	al,[edi]      								; add the second integer
	pushfd              							; save the Carry flag 保存进位
	mov	[ebx],al      								; store partial sum 将部分结果保存
	add	esi,1         								; advance all 3 pointers
	add	edi,1
	add	ebx,1
	popfd               							; restore the Carry flag
	loop	L1           							; repeat the loop

	mov	byte ptr [ebx],0							; clear high byte of sum
	adc	byte ptr [ebx],0							; add any leftover carry
	popad
	ret
Extended_Add ENDP

;-----------------------------------------------------------
Display_Sum PROC
;
; Displays a large integer that is stored in little endian 
; order (LSB to MSB). The output displays the array in 
; hexadecimal, starting with the most significant byte.
; Receives: ESI points to the array, ECX is the array size
; Returns: nothing
;-----------------------------------------------------------
	pushad
	
	; point to the last array element
	add esi,ecx
	sub esi,TYPE BYTE
	mov ebx,TYPE BYTE
	
L1:	mov  al,[esi]									; get an array byte 数据存储在每一个ax寄存地的低八位
	call WriteHexB									; display it
	sub  esi,TYPE BYTE								; point to previous byte
	loop L1

	popad
	ret
Display_Sum ENDP


END main
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值