1 ; File: index1.asm 2 ; 3 ; This program demonstrates the use of an indexed addressing mode 4 ; to access array elements. 5 ; 6 ; This program has no I/O. Use the debugger to examine its effects. 7 ; 8 SECTION .data ; Data section 9 10 00000000 000000000100000002- arr: dd 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ; ten 32-bit words 11 00000009 000000030000000400- 12 00000012 000005000000060000- 13 0000001B 000700000008000000- 14 00000024 09000000 15 base: equ arr - 4 16 17 SECTION .text ; Code section. 18 global _start 19 00000000 90 _start: nop ; Entry point. 20 21 ; Add 5 to each element of the array stored in arr. Simulate: 22 ; 23 ; for (i = 0 ; i < 10 ; i++) { 24 ; arr[i] += 5 ; 25 ; } 26 27 00000001 B900000000 mov ecx, 0 ; ecx simulates i 28 00000006 83F90A loop1: cmp ecx, 10 ; i < 10 ? 29 00000009 7D0E jge done1 30 0000000B 81048D[00000000]05- add [4*ecx+arr], dword 5 ; arr[i] += 5 31 00000013 000000 32 00000016 41 inc ecx ; i++ 33 00000017 EBED jmp loop1 34 done1: 35 36 ; more idiomatic for an assembly language program 37 00000019 B909000000 mov ecx, 9 ; last array element's index 38 0000001E 81048D[00000000]05- loop2: add [4*ecx+arr], dword 5 39 00000026 000000 40 00000029 49 dec ecx 41 0000002A 7DF2 jge loop2 ; again if ecx >= 0 42 43 44 ; another way 45 0000002C BF[FCFFFFFF] mov edi, base ; base computed at load time 46 00000031 B90A000000 mov ecx, 10 ; for(i=10 ; i>0 ; i--) 47 00000036 81048F05000000 loop3: add [edi+4*ecx], dword 5 48 0000003D E2F7 loop loop3 ; loop = dec ecx, jne 49 50 alldone: 51 0000003F BB00000000 mov ebx, 0 ; exit code, 0=normal 52 00000044 B801000000 mov eax, 1 ; Exit. 53 00000049 CD80 int 80H ; Call kernel.