Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

assembly - idiv overflow exception asm

I'm very new to assembly, before only C C++
I was trying to create a simple application that print all the prime number from 2 to given input
When I run, it crashes, and that's normal. Looking at OllyDbg, the guilty piece is this:

move eax, ebx
idiv ecx ; !!! here

It is strange because ecx is not 0
Values are EAX = 0000 0004, ECX = 0000 0002, and it says me that's an Integer_Overflow (exception C000 0095)
How it's possible to have an overflow during a division? Both operands are 32bit

Floowing the code and a screenshot of ollydbg

%include "asm_io.inc"

segment .data
input_msg   db  "Insert a number: ", 0
output_msg  db  "This is prime: ", 0

segment .bss
input       resd    1

segment .text
global _asm_main

; input => input number
; ebx => current number to execute [2 .. input]
; ecx => counter from 2 to current [2 .. current]

_asm_main:
    mov eax, input_msg
    call print_string

    call read_int
    inc eax
    mov [input], eax
    call print_nl

    mov ebx, 2h

    _start_main_loop:
        mov eax, [input] ; if current > input
        cmp eax, ebx
        jz _end_main_loop       

        mov ecx, 2h

        _iteration:
            cmp ebx, ecx
            je _print_number

            mov eax, ebx
            idiv ecx ; unsigned division?

            cmp edx, 0 ; if rem != 0 jmp
            jne _end_iteration
            inc ecx ; else inc ecx and re-divide
            jmp _iteration

        _print_number:
            mov eax, output_msg
            call print_string
            mov eax, ebx
            call print_int
            call print_nl

        _end_iteration:
            inc ebx

            jmp _start_main_loop

    _end_main_loop:

    popa
    mov     eax, 0
    leave
    ret

enter image description here

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...