Matthias Herrmann
Matthias Herrmann

Reputation: 2790

Assembler: Understanding how to calculate an average

The comment of the Code above says: Calculation of N numbers from Memory adress A. The Code is only for educational purpose...

I'm trying to understand how the calculation of an average is working in assembler. The Code comments are written by me. If I'm understanding something wrong pls add it to your answer. The part which I'm not understanding is especially ADD EBX,4.

The purpose of this Code is to get the average of all the numbers in the memory address A. Please read the comments carefully.

MOV ECX, [N]      ;Loading count of numbers in ECX
JZ done           ; If the count of numbers is 0 go to done
MOV EBX, [A]      ; Load all the numbers which are in memory space [A] in EBX
MOV EAX,0         ; EAX will be the result
Loop: ADD EAX, [EBX] ;Add first number of EBX to EAX?
ADD EBX, 4           ;What is this doing and how does it work, why would you add the number 4?
DEC ECX              ;Numbers to add - 1
JNZ Loop             ;Jump if not zero, and repeat the loop
DIV [N]              ;Divide the result of the calculation
Done: ;value of average in EAX

The questions are in the Code comments. Provide me with Information what [A]and [N] is and how they are working and especially what ADD EBX,4 is doing? Thanks in regard.

Upvotes: 1

Views: 1675

Answers (2)

Ped7g
Ped7g

Reputation: 16606

MOV ECX, [N]      ;Loading count of numbers in ECX
JZ done           ; If the count of numbers is 0 go to done

The second comment is wrong. The mov will not change ZF, so the conditional jump will happen upon value of ZF from caller.

To have a code working as the comments suggest, you would have to test the fetched value like this:

MOV ECX, [N]      ;Loading count of numbers in ECX
TEST ecx,ecx      ; do virtual "AND ecx,ecx" and set only flags
JZ done           ; If the count of numbers is 0 go to done

Or CMP ecx,0, or the JZ can be replaced by JECXZ without setting ZF. (or probably million other cryptic ways, how to set some flag when ecx is zero, without changing it content... did anyone mention OR ecx,ecx ...)

Upvotes: 2

Let's start with the definition of the variables "N" and "A", they look like this:

.data
  N     dd 5
  ARRAY dd 1,2,3,4,5
  A     dd ?

We now "N" is type "dd" because in your code "N" is been assigned to register "ECX", which size is 32 bit :

MOV ECX, [N]      ;Loading count of numbers in ECX

"A" is a pointer to an array of numbers (as @MichaelPetch pointed out), because register "EBX" is set to "[A]" and its value is added to EAX on each loop:

mov [A], offset ARRAY      ;A IS THE ADDRES OF THE ARRAY OF NUMBERS.
MOV EBX, [A]               ;IN IDEAL MODE VARIABLES VALUE IS ACCESSED WITH BRACKETS.
...
Loop: ADD EAX, [EBX] ;Add first number of EBX to EAX?

By the way, the label "Loop:" might be a problem, because that's the name of a instruction, let's change the name by "Loopy:" :

Loopy: ADD EAX, [EBX] ;Add first number of EBX to EAX?
...
JNZ Loopy             ;Jump if not zero, and repeat the loop

Register "EBX" works as a pointer to the array of numbers "ARRAY":

mov [A], offset ARRAY  ;A IS THE ADDRES OF THE ARRAY OF NUMBERS.
MOV EBX, [A]           ; Load all the numbers which are in memory space [A] in EBX

This pointer must be moved forward, the first time it points to "1" (the first number in the array "ARRAY" pointed by "A"), to make it point to the rest of numbers, we have to add "4" to it on each loop, because the array "ARRAY" (pointed by "A") is type "dd", which means each element's size is 4 bytes :

ADD EBX, 4           ;What is this doing and how does it work, why would you add the number 4?

Finally, the division by "[N]" :

DIV [N]              ;Divide the result of the calculation

The "DIV" instruction, when the value is "dd" uses "EDX" register also, so it would be a good idead to clear "EDX" to avoid a wrong result :

MOV EDX, 0
DIV [N]              ;Divide the result of the calculation

Now that "EDX" is 0, the division will divide "EAX" only, and get the average.

Upvotes: 2

Related Questions