Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

assembly - Printing square triangular numbers not excpected value

So, I want to print a square triangular numbers, you can see at here.

This is code I wrote :

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

The result correct value is iteration from 0 - 7 but at iteration 8 not correct value.

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

So you can help me to fix this if possible ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

imul eax, 34 does a 32-bit multiplication and the result overflows


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...