flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
rkatsiri
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'
|
|||||||||||
![]() |
|
AsmGuru62
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? |
|||
![]() |
|
AsmGuru62
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++ ... |
|||
![]() |
|
Mikl___
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 |
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.