"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."
Full description on GitHub: xorpd
The code I've written:
format PE console entry start include 'win32a.inc' ; =============================================== section '.text' code readable executable start: mov eax, 0x01020304 xor ebp, ebp process_eax: movzx ebx, al add ecx, ebx movzx ebx, ah add ecx, ebx cmp ebp, 0x1 je print_result xor edx, edx mov ebx, 0xffff div ebx mov ebp, 0x1 jmp process_eax print_result: mov eax, ecx call print_eax ; Provided by the teacher. Prints eax to the console. exitProgram: ; Exit the process: push 0 call [ExitProcess] include 'training.inc'
I think it works. I've tried it with different values and the sums were correct.
Screenshot with the output of the code above (with 0x01020304 as the hardcoded value).
But it's surely not the most efficient way to solve the exercise.