I am trying to add two 256-bit integers together, but can only get 64-bit ints to work. Any help? Here is what I have:
<CODE>
TITLE Addition (main.asm)
INCLUDE Irvine32.inc
.data
op1 QWORD 0A2B2A40674981234h
op2 QWORD 08010870000234502h
sum DWORD 3 dup(0FFFFFFFFh) ; = 0000000122C32B0674BB5736
.code
main PROC
mov esi,OFFSET op1 ; first operand
mov edi,OFFSET op2 ; second operand
mov ebx,OFFSET sum ; sum operand
mov ecx,2 ; number of doublewords
call Extended_Add
; Display the sum.
mov eax,sum + 8 ; display high-order dword
call WriteHex
mov eax,sum + 4 ; display middle dword
call WriteHex
mov eax,sum ; display low-order dword
call WriteHex
call Crlf
exit
main ENDP
Extended_Add PROC
pushad
clc ; clear the Carry flag
L1: mov eax, ; get the first integer
adc eax, ; add the second integer
pushfd ; save the Carry flag
mov ,eax ; store partial sum
add esi,4 ; advance all 3 pointers
add edi,4
add ebx,4
popfd ; restore the Carry flag
loop L1 ; repeat the loop
mov dword ptr ,0 ; clear high dword of sum
adc dword ptr ,0 ; add any leftover carry
popad
ret
Extended_Add ENDP
END main
</CODE>
<CODE>
TITLE Addition (main.asm)
INCLUDE Irvine32.inc
.data
op1 QWORD 0A2B2A40674981234h
op2 QWORD 08010870000234502h
sum DWORD 3 dup(0FFFFFFFFh) ; = 0000000122C32B0674BB5736
.code
main PROC
mov esi,OFFSET op1 ; first operand
mov edi,OFFSET op2 ; second operand
mov ebx,OFFSET sum ; sum operand
mov ecx,2 ; number of doublewords
call Extended_Add
; Display the sum.
mov eax,sum + 8 ; display high-order dword
call WriteHex
mov eax,sum + 4 ; display middle dword
call WriteHex
mov eax,sum ; display low-order dword
call WriteHex
call Crlf
exit
main ENDP
Extended_Add PROC
pushad
clc ; clear the Carry flag
L1: mov eax, ; get the first integer
adc eax, ; add the second integer
pushfd ; save the Carry flag
mov ,eax ; store partial sum
add esi,4 ; advance all 3 pointers
add edi,4
add ebx,4
popfd ; restore the Carry flag
loop L1 ; repeat the loop
mov dword ptr ,0 ; clear high dword of sum
adc dword ptr ,0 ; add any leftover carry
popad
ret
Extended_Add ENDP
END main
</CODE>
Do you understand how your data is laid out?
Since you are doing addition left to right, lowest dword of your 256 integer should be leftmost, highest rightmost.
op1 dd LOWDWORD, ? , ? , ? , ? , ? , ? , HIGHDWORD
op2 dd LOWDWORD, ? , ? , ? , ? , ? , ? , HIGHDWORD
sum dd LOWDWORD, ? , ? , ? , ? , ? , ? , ? , HIGHDWORD
Besides that i can't see anything wrong with your code.
Since you are doing addition left to right, lowest dword of your 256 integer should be leftmost, highest rightmost.
op1 dd LOWDWORD, ? , ? , ? , ? , ? , ? , HIGHDWORD
op2 dd LOWDWORD, ? , ? , ? , ? , ? , ? , HIGHDWORD
sum dd LOWDWORD, ? , ? , ? , ? , ? , ? , ? , HIGHDWORD
Besides that i can't see anything wrong with your code.
I understand the concept, but I cannot get two 256-bit integers to add and output.
Your code should work fine. I don't see the problem.
mov esi,OFFSET op1 ; first operand
mov edi,OFFSET op2 ; second operand
mov ebx,OFFSET sum ; sum operand
mov ecx,8 ; number of doublewords
call Extended_Add
; Display the sum.
mov eax,sum + 8*4 ; display high-order dword
call WriteHex
mov eax,sum + 7*4 ;
call WriteHex
etc...
mov eax,sum ; display low-order dword
call WriteHex
call Crlf
program_man,
Your code works (you've linked it with /subsystem:console option, haven't you?), though it can be improved.
lea ebx, leaves eflags untouched, you may replace adds with it.
There is a way to address both sources and destination with only one register being increased: load ebx with address of destination, esi with source1 - destination, edi with source2 - destination and address sources with / .
mov / adc dword ptr , 0 to save one-bit carry? sbb eax, eax / neg eax / mov , eax looks better.
Your code works (you've linked it with /subsystem:console option, haven't you?), though it can be improved.
lea ebx, leaves eflags untouched, you may replace adds with it.
There is a way to address both sources and destination with only one register being increased: load ebx with address of destination, esi with source1 - destination, edi with source2 - destination and address sources with / .
mov / adc dword ptr , 0 to save one-bit carry? sbb eax, eax / neg eax / mov , eax looks better.