;0.0 Write a program that takes a double word (4 bytes) as an argument, and
;      then adds all the 4 bytes. It returns the sum as output. Note that all the
 ;     bytes are considered to be of unsigned value.
;
 ;     Example: For the number 03ff0103 the program will calculate 0x03 + 0xff +
  ;    0x01 + 0x3 = 0x106, and the output will be 0x106
;
 ;     HINT: Use division to get to the values of the highest two bytes.
;reserve eax and edx for the div instructions


format PE console

entry start
include 'win32a.inc'

section '.text' code readable executable


start:

	call read_hex			;get the number from console
	
	xor ecx,ecx
	xor ebx,ebx
	
	mov bl,al					;save ax's low 8bit to bl
	mov bh,ah					;save ax's high 8bit to bh
	
	sub eax,ebx					;to zero out the bottom 16 bits of eax so we can concentrate on getting the top 16
	mov esi,eax					;hold onto the original value of eax in esi since it'll be changing everytime we use div

while_loop:

	mov eax,esi					;reset eax to it's starting value
	inc ecx						;keep checking ecx until they match one another
	xor edx,edx						;DONT FORGET TO CLEAR EDX EVERYTIME THE LOOP OCCURS OR WE'LL NEVER GET A PROPER RESULTTTT
	div ecx						;here I was hoping that ECX would be keeping it's values in the lower 16 bits before utilizing any higher ones and i'd therefore be able to access them with cl and ch
	
	cmp edx,0					;the rest of this is just trying to get the exact number of eax into ecx but again, hoping that it would be held in the CL/CH/CX container...
	jnz while_loop
	
	cmp eax,1
	jz end_while
	
	jmp while_loop
	
end_while:

	mov eax,ecx						;i just move this to eax so i can print it out with the built-in call that the instructor provided
	call print_eax					;right now if i input    03ff0103 i get 03FF0000 as a result and if i do movsx eax,cx i get 0 ... I was hoping it would give me the top bytes of the original EAX input then I can easily add     
									;in the needed add instructions. I just can't access those top 16-bits using the instructions we know.
	push 0
	call [ExitProcess]
	
include 'training.inc'