News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

64bit addition

Started by ossama, December 27, 2007, 07:36:49 AM

Previous topic - Next topic

ossama

i tried to write a code that do the addtion of two 64bit unsigned integers,
any optimizations are welcome.


.686
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data

data1 dd 0aabbccddh,0eeffa9b8h ;data1=0eeffa9b8aabbccddh
data2 dd 011223344h,055667788h ;data2=05566778811223344h

szResult db 1024 dup(?) ;this buffer will be use to show the result
szFormat db "%lX%lX%lX",0 ;lX means showing numbers in hexadecimal format
;note that using formats other then hexadecimal will show wrong results

.code
start:
;adding low dword of data1 to low dword of data2
mov eax,[data1] ;mov first dword into eax
mov ecx,[data2] ;mov first dword into ecx
add eax,ecx

;save result to later use
push eax

;adding high dword of data1 to high dword of data2 with cary
mov eax,[data1+4] ;mov second dword into eax,we used the offset +4 bytes to go to the second dword (first dword has offsets 0,1,2,3),but in the case of adding two 32bit integers it is another story
mov ecx,[data2+4]
adc eax,ecx

;save result to later use
push eax

;get the cary of second addition
mov eax,0
adc eax,0 ;this will add 1 if CF is set,so using this method will save the carry of the last addtion in eax

;constructing the result of the addtion of two 64bit numbers
pop ecx ;ecx==high dword of the result
pop edx ;edx==low dword of the result
;now the result of (data1+data2) is : eax:ecx:edx i think you know this notation
;you can do whatever you want with this result in real program,but for demonstration purpose we show the result in messagebox
invoke wsprintf,addr szResult,addr szFormat,eax,ecx,edx
invoke MessageBox,NULL,addr szResult,NULL,MB_OK

;exit the program
invoke ExitProcess,0

;note1: the addition of two 64bit integers is always (64+1)bits
;and in general the addition of two (n-bits) integers is always {(n+1)bits}
;note2: in this example i used only three register, eax,ecx,edx

end start


regards ossama.

woonsan

#1
I think that in normal P6 Family CPU, using MMX instructions is better.

PADDQ instruction can be used to add two 64-bit unsigned integers.
for example, you can write code like below.

MOVQ MM0, $DATA1
PADDQ MM0, $DATA2
MOVQ $RESULT, MM0