0
votes

I'm trying to solve this exercise :

Assume that you are given values in eax,ebx,ecx. Write code that adds the values inside all those registers, and stores the final result inside edx.

My code :

mov eax,3
mov ebx,4
mov ecx,1
add edx,eax
add edx,ebx
add edx,ecx

Do I have to initialize the register edx (mov edx,0) ?

1

1 Answers

7
votes

Do I have to initialize the register edx (mov edx,0) ?

The way your code is written you need to clear edx prior to the first add, either with mov edx, 0 or xor edx, edx. But instead of adding an extra instruction you can just replace the first add with a mov:

mov edx,eax   ; edx = eax
add edx,ebx   ; edx += ebx
add edx,ecx   ; edx += ecx

Or, one instruction less:

lea edx,[eax + ebx]  ; edx = eax + ebx
add edx,ecx          ; edx += ecx