Reputation: 4382
I'm asked to write a program to add a byte sized array elements. First 2 bytes are the number of elements in the array.
The array may contain thousands of numbers so I should put the sum in 32-bit register (e.g. ax:dx
) but the problem is that I don't know how to take a byte from memory then add it to a 32-bit register (or a double variable in memory).
I have tried adding using a 16-bit register and a word variable in memory. Here is the code:
array db 07, 00, 30, 10, 77, 14, 9, 54, 100
sum dw ?
lea ax, data
mov ds, ax
mov es, ax
lea bx, array
mov cx, [bx]
mov bx, 0002h
mov dx, 0000h
Addition:
mov dl, [bx]
mov dh, 00h
add sum, dx
add bx, 1
loop Addition
mov ax, 4c00h
int 21h
It works correctly. But I want to know how to do that with a 32-bit register and a double variable.
I use emu8086
Upvotes: 0
Views: 610
Reputation: 58507
To add an 8-bit value in BL
to the 32-bit sum in DX:AX
:
xor bh,bh ; Clear BH (effectively zero-extends BL into BX)
add ax,bx ; Add BX to the lower half of the sum
adc dx,0 ; If the lower half wrapped around (the above addition resulted
; in a carry), add 1 to the upper half, otherwise add 0.
Upvotes: 3