This piece of code comes from Intel assembly language programming (version 4)
;----------------------------------------------------------------------------
Arraysum proc
;
; Calculates the sum of an array of 32-bit integers.
; Es: ESI = the array offset
; ECx = number of elements in the array
; Returns: eax = sum of the array elements
;----------------------------------------------------------------------------
Push ESI; save ESI, ECx
Push ECx
MoV eax, 0; set the sum to zero
L1:
Add eax, [esi]; add each integer to sum
Add ESI, 4; point to next integer
Loop L1; repeat for array size
Pop ECx; restore ECx, ESI
Pop ESI
RET; sum is in eax
Arraysum endp
The above is the function code of the array. Let's take a sentence for analysis.
First, we define a function and use the following statement
Arraysum proc
Then, we push the values of ESI and ECx into the stack to save them. In this way, after the end of the entire function, you can use Pop to get the original values of ESI and ECx:
Push ESI; save ESI, ECx
Push ECx
Then, because we will keep the sum of the obtained arrays in the eax register, use the following statement to clear the eax register:
MoV eax, 0; set the sum to zero
Then we added a L1 label for loop. Then we started the loop and looked at the Code:
L1:
Add eax, [esi]; add each integer to sum
Add ESI, 4; point to next integer
Loop L1; repeat for array size
First, the ESI register stores the offset address of the element of the current array, and then uses square brackets to obtain the array value of this address, add it to eax, and then, add 4 to the ESI address (because we calculate the sum of 32-bit integers and 32-bit integers, we need 4 bytes. Therefore, here we add 4 to ESI, instead of other values, so that we can get the offset address of the next array element), and then the next sentence jumps back to L1.
Speaking of this, some may ask, how do we know when the loop ends? In fact, during the loop, the register ECx stores the number of times the loop will be executed (here is the number of array elements). Let's look at the comments of the function:
;----------------------------------------------------------------------------
Arraysum proc
;
; Calculates the sum of an array of 32-bit integers.
; Es: ESI = the array offset
; ECx = number of elements in the array
; Returns: eax = sum of the array elements
;----------------------------------------------------------------------------
This sentence:
ECX = number of elements in the array
It indicates that the ECX register retains the number of elements in the array (the number of ECx registers retains the number of cycles, and the number of elements in the loop (each loop is executed) is reduced by 1 until it is changed to 0 ), therefore, before calling the arraysum function, we need to calculate the number of array elements and keep them in the ECX register.
At this point, the functions of the function are completed, but we need to finish the work and look at the following code:
Pop ECx; restore ECx, ESI
Pop ESI
RET; sum is in eax
We need to assign the values of ECx and ESI registers back. We only need to pop up ECx and ESI.
The last RET is required for all non-primary functions. It is used to point the ESP instruction pointer register to the address before the function call, so that the main function can be executed downward.
Then let's take a look at how to use this array summation function.
. Data
Array DWORD 10000 h, 20000 H, 30000 H, 40000 H, 50000 H
Thesum DWORD
. Code
Main proc
MoV ESI, offset array; ESI points to array
MoV ECx, lengthof array; ECx = array count
Call arraysum; Calculate the sum
MoV thesum, eax; Return in eax
Main endp
End main
Here, we can see that the main function indeed puts the appropriate value into the ESI and ECx registers before the arraysum function is executed.
In addition, we can use the pseudo command uses instead of the above error-prone method to save the register value. The following is the summation code after uses is modified:
Arraysum proc uses ESI ECx
MoV eax, 0
L1:
Add eax, [esi]
Add ESI, 4
Loop L1
RET
Arraysum endp
The compiler generates the following code:
Arraysum proc
Push ESI
Push ECx
MoV eax, 0
L1:
Add eax, [esi]
Add ESI, 4
Loop L1
Pop ECx
Pop ESI
RET
Arraysum endp
(Note: The registers used for the returned values of the process cannot be pushed to the stack or pop-up; otherwise, the values will be lost, for example, eax in this example)