New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3.5.3 for 循环 的算法有问题 #577
Labels
Comments
修改后: func LoopAdd(cnt, v0, step int) int {
result := v0
next := v0
for i := 1; i < cnt; i++ {
next += step
result += next
}
return result
} 汇编: TEXT ·LoopAdd(SB),$0-32
MOVQ cnt+8*0(FP), AX // cnt
MOVQ v0+8*1(FP), BX // v0 /result
MOVQ step+8*2(FP), CX // step
MOVQ BX, R8 // next
MOVQ $1, DX // i
LOOP_IF:
CMPQ DX, AX // compare i, cnt
JL LOOP_BODY // if i < cnt; goto LOOP_BODY
JMP LOOP_END
LOOP_BODY:
ADDQ CX, R8 // next += step
ADDQ R8, BX // result += next
ADDQ $1, DX // i++
JMP LOOP_IF
LOOP_END:
MOVQ BX, ret+8*3(FP) // return result
RET |
发现 BUG 直接上 PR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
问题描述
按这个算法
LoopAdd(100, 1, 1) = 101 期望:5050
LoopAdd(5, 10, -2) = 0 期望:30
The text was updated successfully, but these errors were encountered: