在masm x86中使用扩展加法方法,实现256位数的加法可以通过如下代码实现:
; ---------------------------------------------
; Adding 256-bit numbers using Extended Addition
; ---------------------------------------------
.486
.model flat, stdcall
option casemap :none
;-----------
; Constants
;-----------
SZ_QWORD EQU 8
SZ_DWORD EQU 4
;------------------
; Function Prototypes
;------------------
PUBLIC Add256
PUBLIC ReversedAdd256
;------------
; Externals
;------------
extern _memcpy : proc
;--------------------------
; Structure - Quadword Pair
;--------------------------
STRUCT PAIR
value QWORD ?
reserve QWORD ?
ENDS
;----------------
; Macro Functions
;----------------
repeat 2 ; (1)
MACRO LOADPAIR dest,src
mov eax, src
mov edx, [eax + SZ_QWORD]
mov eax, [eax]
mov dest.value, eax
mov dest.reserve, edx
ENDM
endm
repeat 2 ; (2)
MACRO SAVEPAIR dest,src
mov eax, dest
mov edx, dest.reserve
mov [eax + SZ_QWORD], edx
mov [eax], dest.value
ENDM
endm
;-----------
; Add256
;-----------
PUBLIC Add256
Add256 PROC pDst:PTR QWORD, pSrc:PTR QWORD
LOCAL Dest:PAIR
LOCAL Src:PAIR
LOCAL Carry:PAIR
LOCAL Sum:PAIR
MOV EDX, pDst
MOV ECX, pSrc
CALL LOADPAIR ; Load dst and src pairs 1
CALL LOADPAIR ; Load dst and src pairs 2
; Zero the High dword
xor ecx,ecx
xor edx,edx
xor ebx,ebx
; Do addition using Extended method.
add ebx, ax ; eax -= ZeroFill(ebx)
adc