首页 > 解决方案 > 打印方三角数不是预期值

问题描述

所以,我想打印一个正方形三角形数字,你可以在 这里看到。

这是我写的代码:

global main
extern printf

section .data
format     db "%u ", 0

section .text

main:
    mov     ecx, 8
square_triangular:
    xor     edx, edx
    mov     eax, 0
    call    print       ; just print zero in early
    mov     ebx, 1
    mov     eax, ebx
    jecxz   .done
.loop:
    call    print
    imul    eax, 34
    sub     eax, edx
    add     eax, 2
    mov     edi, eax
    add     edx, 1
    sub     edx, 1
    xchg    ebx, edx
    mov     ebx, edi
    mov     eax, eax    ; store in eax
    loop    .loop
.done:
    ret

print:
    pushad
    push    eax
    push    format
    call    printf
    add     esp, 8
    popad
    ret

结果正确值是迭代自0 - 7但在迭代时8不是正确值。

0 1 36 1225 41616 1413721 48024900 1631432881 3881085504
                                                  ^
                                                  |
                                         here (this is wrong)

如果可能的话,你可以帮我解决这个问题吗?

标签: assemblyx86

解决方案


imul eax, 34执行 32 位乘法,结果溢出


推荐阅读