【发布时间】:2015-07-09 10:21:30
【问题描述】:
我正在尝试获取此 c 代码:
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) ;
}
到我目前拥有的 NASM 等价物:
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
我不断收到分段错误,但我不明白为什么它会一直发生。我尝试在任何地方添加打印语句,但找不到故障所在。任何建议都会很棒!谢谢!
【问题讨论】:
-
我觉得你的 asm 没问题。
-
在
printfs 之后有ret指令吗? -
是的,我厌倦了它没有解决分段问题:(