flat assembler
Message board for the users of flat assembler.

Index > Windows > run error

Author
Thread Post new topic Reply to topic
rkatsiri



Joined: 08 Feb 2016
Posts: 4
rkatsiri 09 Feb 2016, 12:46
Hi, I get a run error when i try running this program.
Is there a wrong command or a logic error in this code?
I'm new to assembly, so sorry if my question seems dum...

I wrote it in attempt to do this:
Write a program that takes the number n as input. Then it prints all the numbers x below n that have exactly 2 different integral divisors (Besides 1 and x).

Here's the code i wrote:

Code:
start:
    
call    read_hex        ; eax <- n
cmp eax,6h      
jb exit_program ;(if n is below 6, which is the smallest number with only 2 divisors)
        
mov     esi,2h          ; esi <- 2
dec     eax                     ; x <- n-- (x is the biggest number below n)

check_integers:
        
        mov ebx, eax            ;ebx <- x
        mov edi, 0h                     ; 0 divisiors
        div esi                 ; eax <- x/2 , edx <- remainder
        call print_eax  ; print just to check the code
        mov ecx, eax    ; ecx <- x/2
        mov eax, ebx    ; eax <- x
        
count_divisors:
        div ecx         ; eax <- x / (x/2) , edx <- remainder 
        mov eax, ebx    ; eax <- x
        cmp edx, 0h
        jnz skip_inc_divisors  ;(if remainder not equal 0)
        inc edi ; divisiors++

skip_inc_divisors:
        dec ecx         ; ecx--
        cmp ecx, 2h
        jnb count_divisors ;(if ecx not below 2)

        cmp edi, 2h;
        jne skip_print
        call print_eax  ; print x (print x if equals 2)

skip_print:
        dec eax         ; x--
        cmp eax,6h      
        jnb check_integers      ;(if eax not below 6, which is the smallest number with only 2 divisors)

exit_program:
    ; Exit the process:
        push    0
        call    [ExitProcess]

include 'training.inc'
    


Description:
Download
Filename: 0_just2divisors.asm
Filesize: 1.66 KB
Downloaded: 347 Time(s)

Post 09 Feb 2016, 12:46
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1620
Location: Toronto, Canada
AsmGuru62 09 Feb 2016, 15:43
DIV instruction requires EDX to be set to zero, so 64 bits of whatever you dividing will fit into EDX:EAX pair.
I do not see you are doing it.
Can this be an issue?
Post 09 Feb 2016, 15:43
View user's profile Send private message Send e-mail Reply with quote
rkatsiri



Joined: 08 Feb 2016
Posts: 4
rkatsiri 09 Feb 2016, 19:01
AsmGuru62, I've just added the following line in the beginning of the code:

Code:
mov edx, 0h
    


But unfortunately, i still get a run error,
right after the following output:

3
55555557[/code][/b]
Post 09 Feb 2016, 19:01
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1620
Location: Toronto, Canada
AsmGuru62 10 Feb 2016, 04:34
Once DIV is complete - EDX may be set to a remainder - may be not zero.
So, you need to reset it to 0 before every DIV - you have another DIV below in the code.
Let me check the code in debugger.

Update:
I added two lines (marked with AG62) and code seems to run OK:
Code:
        ...

check_integers: 

        mov ebx, eax            ;ebx <- x
        mov edi, 0h                     ; 0 divisiors

        mov edx, 0h     ; AG62 - need EDX = 0 before DIV

        div esi                 ; eax <- x/2 , edx <- remainder
        call print_eax  ; print just to check the code
        mov ecx, eax    ; ecx <- x/2
        mov eax, ebx    ; eax <- x

count_divisors: 
        mov edx, 0h     ; AG62 - need EDX = 0 before DIV

        div ecx         ; eax <- x / (x/2) , edx <- remainder  
        mov eax, ebx    ; eax <- x 
        cmp edx, 0h 
        jnz skip_inc_divisors  ;(if remainder not equal 0) 
        inc edi ; divisiors++ 

        ...
    
Post 10 Feb 2016, 04:34
View user's profile Send private message Send e-mail Reply with quote
Mikl___



Joined: 30 Dec 2014
Posts: 129
Location: Russian Federation, Irkutsk
Mikl___ 10 Feb 2016, 06:00
Code:
mov edx,eax     ; AG62 - need EDX = 0 before DIV
and edx,1 ; edx <- remainder
shr eax,1 ; eax <- x/2 
call print_eax  ; print just to check the code    
Post 10 Feb 2016, 06:00
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.