; ASM implementation of a classic 'for' loop command ; ; sum = 0 ; for (i = 0; i <= 10; i++) ; sum += i; ; ; include directives global asm_main %include "../lib/asm_io.inc" segment .data ; DX directives (read only, static data) segment .bss ; RESX directives (writable data) segment .text asm_main: ; entry point (called from driver.c) ; instructions enter 0,0 ; Initialize register EBP pusha ; Push register contents to the stack ; mov eax, 0 ; eax is the sum mov ebx, 0 ; ebx is i mov ecx, 10 ; ecx is the loop condition loop_start: cmp ebx, ecx ; compare i to ECX jg loop_end ; loop condition ; loop body add eax, ebx ; sum += i inc ebx ; ebx++ call print_int ; print for feedback call print_nl jmp loop_start ; continue loop loop_end: ; popa ; Restore register data from the stack mov eax, 0 ; flush EAX of data leave ; restore the stack ret ; Return from main back into C library wrapper