; loopint2.asm code loopint2.c for nasm ; /* loopint2.c another simple loop that will be coded for nasm */ ; #include ; int main() ; { ; int dd1[100]; ; int i; ; dd1[0]=5; /* be sure loop stays 1..98 */ ; dd1[99]=9; ; for(i=98; i>0; i--) dd1[i]=7; /* ONLY CHANGE !! */ ; printf("dd1[0]=%d, dd1[1]=%d, dd1[98]=%d, dd1[99]=%d\n", ; dd1[0], dd1[1], dd1[98],dd1[99]); ; return 0; ;} section .bss dd1: resd 100 i: resd 1 ; actually unused, kept in register section .text global main main: mov dword [dd1],5 ; dd1[0]=5; mov dword [dd1+99*4],9 ; dd1[99]=9; mov ecx,98 ; i=98; ecx is special for 'loop' instruction loop1: mov dword [dd1+4*ecx],7 ; dd1[i]=7; loop loop1 ; loop while i>0, decrements ecx extern printf ; the C function, to be called section .data ; Data section, initialized variables fmt: db "dd1[0]=%d, dd1[1]=%d, dd1[98]=%d, dd1[99]=%d",10,0 section .text ; Code section, continued push dword [dd1+99*4] ; dd1[99] push dword [dd1+98*4] ; dd1[98] push dword [dd1+4] ; dd1[1] push dword [dd1] ; dd1[0] push dword fmt ; address of format string call printf ; Call C function add esp, 20 ; pop stack 5 push times 4 bytes mov eax,0 ; normal, no error, return value ret ; return 0; ; no registers needed to be saved