I'm trying to get this c code:
main()
{int x, y, count ;
count = 0 ;
x = 10 ;
y = 2 ;
while (y < x)
{ x = x + 1 ;
y = y + 2 ;
count = count + 1 ;
}
printf(“ It took %d iterations to complete loop. That seems like a lot\n”,count) ;
}
to its NASM equivalent which I have this so far:
segment .data
out1 db "It took ", 0
out2 db "%i ", 0
out3 db "iterations to complete the loop. That seems like a lot.", 10, 0
segment .bss
segment .text
global main
extern printf
main:
mov eax, 0 ;count
mov ebx, 10 ;x
mov ecx, 2 ;y
jmp lp
mov eax, 0
ret
lp:
cmp ecx, ebx ;compare y to x
jge end ;jump to end if y >= x
add eax, 1
add ebx, 1
add ecx, 2
jmp lp
end:
push out1
call printf
push eax
push out2
call printf
push out3
call printf
I keep getting a segmentation fault and I don't understand why it keeps happening. I've tried adding in print statements everywhere and cannot find where the fault is located. Any advice would be great! Thank you!
ret
instruction after theprintf
s? – Stollings