Loop is executed repeatedly;
From where? This requires "label ";
How many times? Tell the ECX register the number of iterations.
Stupid way to calculate 3x8 = 24
; Test15_1.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data Val DD 3. codemain proc XOR eax, eax add eax, Val add eax, val printdec eax; 24 retmain endpend main
Use loop and label:
; Test15_2.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data Val DD 3. codemain proc XOR eax, eax mov ECx, 8l1:; add eax, Val loop L1, which is named "L1"; repeated to the specified number; each repetition ECX will be reduced by 1, printdec eax; 24 retmain endpend main is not executed until ECx is 0
Example of array summation:
; Test15_3.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data dwarr DD 1, 2, 4, 5. codemain proc Lea EDI, dwarr; give the array start address to a register mov ECx, lengthof dwarr; give the number of array elements (the number of times to be repeated) to ECx XOR eax, eaxl1: Add eax, [EDI]; the addresses in EDI will change constantly. Get the element values add EDI and type dwarr through [EDI]; get the address of the next element loop L1 printdec eax; 15 retmain endpend main
Example of copying a string:
; ‑Flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude Debug. incincludelib kernel32.libincludelib masm32.libincludelib Debug. Lib. Data szsource dB 'Hello world! ', 0; defines the source string szdest dB sizeof szsource DUP (0); defines the target string of the same size. codemain proc mov ESI, 0; here we use ESI to index mov ECx and sizeof szsource; this is the number of cycles L1: mov Al, szsource [esi]; moV's operands cannot both be variables. Use al to convert mov szdest [esi], Al; inc esi; to adjust the index loop L1 printstring szdest; Hello world! Retmain endpend main
If you just copy a string, you can use the szcopy function declared in masm32.inc:
; ‑Flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude Debug. incincludelib kernel32.libincludelib masm32.libincludelib Debug. Lib. Data szsource dB 'Hello world! ', 0 szdest dB sizeof szsource DUP (0). codemain proc invoke szcopy, ADDR szsource, ADDR szdest printstring szdest; Hello world! Retmain endpend main
@, @ B, @ F:
If you are too reluctant to name a label, you can use @ for the label; @ B for the last label and @ F for the last label ;; use @ to modify the preceding 3*8 = 24 example:; test15_6.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data Val DD 3. codemain proc XOR eax, eax mov ECx, 8: Add eax, Val loop @ B printdec eax; 24 retmain endpend main
About global labels:
In the above example, labels are all local labels. If a label is defined outside the sub-process, it is a global label. Can we define a global label in the sub-process? Add two to the end: Yes .; test15_7.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data Val DD 3. codemyproc proclabel1:; Is label1: instead of label1: printtext 'myproc' retmyproc endpmain proc printtext'main' JMP label1; JMP is the unconditional jump command retmain endpend main