I'm working on implementing the AddPacked procedure by modifying an example (AddPacked.asm is below) so that it adds two packed decimal integers of arbitrary size (both lengths must be the same). I have to use the following registers to pass information to the procedure:
- ESI: pointer to the first number
- EDI: pointer to the second number
- EDX: pointer to the sum
- ECX: number of bytes to add
I've referred to the example to write my code but I'm having trouble on utilizing edx, the pointer to the sum. The sum is stored in different variables based on the size so how do I move the value of eax outside of the AddPacked procedure? I've tried push/pop eax but neither work.
INCLUDE Irvine32.inc
.data
packed_1a WORD 4536h
packed_1b WORD 7297h
sum_1 DWORD 0
packed_2a DWORD 67345620h
packed_2b DWORD 54496342h
sum_2 DWORD 2 DUP (0)
packed_3a QWORD 6734562000346521h
packed_3b QWORD 5449634205738261h
sum_3 DWORD 3 DUP (0)
.code
main PROC
mov esi, OFFSET packed_1a
mov edi, OFFSET packed_1b
mov edx, OFFSET sum_1
mov ecx, 4
call AddPacked
mov sum_1, eax
call WriteHex
call Crlf
mov esi, OFFSET packed_2a
mov edi, OFFSET packed_2b
mov edx, OFFSET sum_2
mov ecx, 8
call AddPacked
mov sum_2, eax
call WriteHex
call Crlf
mov esi, OFFSET packed_3a
mov edi, OFFSET packed_3b
mov edx, OFFSET sum_3
mov ecx, 16
call AddPacked
mov sum_3, eax
call WriteHex
call Crlf
exit
main ENDP
;--------------------------------------------------------
;AddPacked PROC USES ecx esi edi,
;
; Adds two packed decimal integers of arbitrary size
; (both lengths must be the same)
; Receives:
; esi: pointer to the first number
; edi: pointer to the second number
; edx: pointer to the sum
; ecx: number of bytes to add
; Returns: nothing
;--------------------------------------------------------
AddPacked PROC USES esi, edi, edx, ecx
xor eax, eax ; initialize sum to zero
;add edi, ecx last byte of sum
L1:
mov al, [esi]
adc al, [edi] ; add corresponding byte of second number and carry
daa
mov [edx], al ; store result
inc esi
inc edi
inc edx
dec ecx
jnz L1 ; continue until byte count is zero
; Add final carry, if any
inc esi
;mov al, 0
;adc al, 0
; add carry to most significant byte of sum
mov [edx], al
pop eax
ret
AddPacked ENDP
END main
----------------------------------------------------
; Packed Decimal Examples (AddPacked.asm)
; This program adds two decimal integers.
INCLUDE Irvine32.inc
.data
packed_1 WORD 4536h
packed_2 WORD 7297h
sum DWORD ?
.code
main PROC
; Initialize sum and index.
mov sum,0
mov esi,0
; Add low bytes.
mov al,BYTE PTR packed_1[esi]
add al,BYTE PTR packed_2[esi]
daa
mov BYTE PTR sum[esi],al
; Add high bytes, include carry.
inc esi
mov al,BYTE PTR packed_1[esi]
adc al,BYTE PTR packed_2[esi]
daa
mov BYTE PTR sum[esi],al
; Add final carry, if any.
inc esi
mov al,0
adc al,0
mov BYTE PTR sum[esi],al
; Display the sum in hexadecimal.
mov eax,sum
call WriteHex
call Crlf
exit
main ENDP
END main