The problem I am facing is trying to add all the bytes defined in
data db 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90
.
I want to average them, which means dividing the sum by 10.
%include "io.inc"
SECTION .text
global CMAIN
CMAIN:
mov ebp, esp ; for correct debugging
SUB AX, AX ; clear the register AX
MOV ECX, 0x000A ; set counter to 10
MOV ESI, 0 ; set offset to 0
AW: ; Destination of Loop AW as long as counter != 0
ADD AL,[ds:data + ESI] ; Add data in the data array into AL
ADD ESI, 1 ; Increase ESI and move along the data array
LOOP AW ; Loop to label AW,CX=CX-1 Keep looping until CX= 0
SUB EDX, EDX ; Clear EDX
MOV CX, 0x000A ; Set the divider to 10 data
DIV CX ; Divide AX with CX, AX = AX / 10
MOV [ds:result], EAX ; The result is stored into location pointed by result
PRINT_HEX 4, result ; Display "result" in the output window
ret ; end program
SECTION .data
data db 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90
result db 0x0
I have tried, and this is the closest I can get. Initially I used EAX instead of AL (I took the 8 least significant bits), and the values I got were very huge and not in the least want I expected.
I add all the values together but it overflows and results in a value of 14 instead of the correct value of 0x48. I am wondering if there is a way to add byte data without causing overflow? Or is this just the limitation of the assembly? Or I am dumb?