1 ; loopint.asm code loopint.c for nasm 2 ; /* loopint.c a very simple loop that will be coded for nasm */ 3 ; #include 4 ; int main() 5 ; { 6 ; int dd1[100]; 7 ; int i; 8 ; dd1[0]=5; /* be sure loop stays 1..98 */ 9 ; dd1[99]=9; 10 ; for(i=1; i<99; i++) dd1[i]=7; 11 ; printf("dd1[0]=%d, dd1[1]=%d, dd1[98]=%d, dd1[99]=%d\n", 12 ; dd1[0], dd1[1], dd1[98],dd1[99]); 13 ; return 0; 14 ;} 15 ; execution output is dd1[0]=5, dd1[1]=7, dd1[98]=7, dd1[99]=9 16 17 section .bss 18 00000000 dd1: resd 100 19 00000190 i: resd 1 ; actually unused, kept in register 20 21 section .text 22 global main 23 00000000 C705[00000000]0500- main: mov dword [dd1],5 ; dd1[0]=5; 24 00000008 0000 25 0000000A C705[8C010000]0900- mov dword [dd1+99*4],9 ; dd1[99]=9; 26 00000012 0000 27 28 00000014 BF04000000 mov edi,4 ; i=1; /* 4 bytes */ 29 00000019 C787[00000000]0700- loop1: mov dword [dd1+edi],7 ; dd1[i]=7; 30 00000021 0000 31 00000023 83C704 add edi,4 ; i++; /* 4 bytes */ 32 00000026 81FF8C010000 cmp edi,4*99 ; i<99 33 0000002C 75EB jne loop1 ; loop until i=99 34 35 extern printf ; the C function, to be called 36 section .data ; Data section, initialized variables 37 00000000 6464315B305D3D2564- fmt: db "dd1[0]=%d, dd1[1]=%d, dd1[98]=%d, dd1[99]=%d",10,0 38 00000009 2C206464315B315D3D- 39 00000012 25642C206464315B39- 40 0000001B 385D3D25642C206464- 41 00000024 315B39395D3D25640A- 42 0000002D 00 43 44 section .text ; Code section, continued 45 0000002E FF35[8C010000] push dword [dd1+99*4] ; dd1[99] 46 00000034 FF35[88010000] push dword [dd1+98*4] ; dd1[98] 47 0000003A FF35[04000000] push dword [dd1+4] ; dd1[1] 48 00000040 FF35[00000000] push dword [dd1] ; dd1[0] 49 00000046 68[00000000] push dword fmt ; address of format string 50 0000004B E8(00000000) call printf ; Call C function 51 00000050 83C414 add esp, 20 ; pop stack 5 push times 4 bytes 52 53 00000053 B800000000 mov eax,0 ; normal, no error, return value 54 00000058 C3 ret ; return 0; 55 ; no registers needed to be saved