; loopint.asm code loopint.c for nasm ; /* loopint.c a very 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=1; i<99; i++) dd1[i]=7; ; printf("dd1[0]=%d, dd1[1]=%d, dd1[98]=%d, dd1[99]=%d\n", ; dd1[0], dd1[1], dd1[98],dd1[99]); ; return 0; ;} ; execution output is dd1[0]=5, dd1[1]=7, dd1[98]=7, dd1[99]=9 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 edi,4 ; i=1; /* 4 bytes */ loop1: mov dword [dd1+edi],7 ; dd1[i]=7; add edi,4 ; i++; /* 4 bytes */ cmp edi,4*99 ; i<99 jne loop1 ; loop until i=99 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