NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: spoomyfly on January 03, 2017, 09:04:02 PM
-
org 100h
mov ax, 780 ;testing number
mov bx, 1 ;first prime number to divide
petla:
cmp ax, 1 ;if testing number less than 1 break
jbe Break
inc bx
mov cx, ax ;saving number in cx
mov dx, 0
div bx
mov ax, cx
test dx, dx ;is there any remainder
jnz petla ;if no - change divisor
push ax ;
mov ax, bx
call Show_AX
pop ax
push ax
push dx
mov ah, 09h
lea dx, [CrLf]
int 21h
pop dx
pop ax
;do "petla" until we get a remainder
Divide:
mov cx, ax
mov dx, 0
div bx
jmp petla
Break:
mov ax, 4c00h
int 20h
Show_AX:
push ax
push bx
push cx
push dx
push di
mov cx, 10
xor di, di
or ax, ax
jns Conv
push ax
mov dx, '-'
mov ah, 2
int 21h
pop ax
neg ax
Conv:
xor dx, dx
div cx
add dl, '0'
inc di
push dx
or ax, ax
jnz Conv
Show:
pop dx
mov ah, 2
int 21h
dec di
jnz Show
pop di
pop dx
pop cx
pop bx
pop ax
ret
; X dw 15 ;testing number as a variable
CrLf db 0Dh, 0Ah, '$'
Algorythm: divide number on prime numbers beginning with 2(so we will not miss any prime)/ Problem is that this code shows all divisors 1 time max
(i mean 4=2*2, my code shows only one 2; but for 30 it shows 2,3,5). Any advices will be appreciated.
""Changed Divide, but still cant gett few equal divisors
PS I love this Captcha
-
Hi Spoomyfly,
I distinctly remember answering this the other day. Perhaps I shouldn't have made the nasty remark about "Captcha".
org 100h
mov ax, 780 ;testing number
mov bx, 1 ;first prime number to divide
petla:
cmp ax, 1 ;if testing number less than 1 break
jbe Break
; I'm thinking we don't want to increment bx in
; some circumstances.
; If ax is not 1, we may want to try the same bx.
; but we do want to increment bx if bx was 1.
; Do we want to start with bx=2?
ja again ; if ax is not yet 1, try the same bx?
inc bx
again:
mov cx, ax ;saving number in cx
mov dx, 0
div bx
mov ax, cx
test dx, dx ;is there any remainder
jnz petla ;if no - change divisor
push ax ;
mov ax, bx
call Show_AX
pop ax
push ax
push dx
mov ah, 09h
lea dx, [CrLf]
int 21h
pop dx
pop ax
;do "petla" until we get a remainder
Divide:
mov cx, ax
mov dx, 0
div bx
jmp petla
Break:
mov ax, 4c00h
int 20h
; strictly speaking, if we're going to use int 20h
; we don't need 4ch in ah.
; perhaps better to use int 21h here.
; this isn't actually a problem
Show_AX:
push ax
push bx
push cx
push dx
push di
mov cx, 10
xor di, di
or ax, ax
jns Conv
push ax
mov dx, '-'
mov ah, 2
int 21h
pop ax
neg ax
Conv:
xor dx, dx
div cx
add dl, '0'
inc di
push dx
or ax, ax
jnz Conv
Show:
pop dx
mov ah, 2
int 21h
dec di
jnz Show
pop di
pop dx
pop cx
pop bx
pop ax
ret
; X dw 15 ;testing number as a variable
CrLf db 0Dh, 0Ah, '$'
Unfortunately, I'm not in a position to test this conveniently at the moment. I may have steered you into an infinite loop, or worse. I don't think my first answer was any better. I think I suggested letting it only print a factor once, but that isn't really what you want (?). In any case, I'm pretty sure if you skip incrementing bx "sometimes" it'll do what you want.
Sorry for the untested code.
Best,
Frank
-
Hi Frank,
You beat me with your reply :)
@spoomyfly,
Please find below my version.
I tested it in 8086 emulator.
org 100h
mov ax, 1024 ;testing number
mov bx, 1 ;first prime number to divide
petla:
cmp ax, bx ;if testing number less than divisor break
jbe Break
inc bx
divideagain:
mov cx, ax ;saving number in cx
mov dx, 0
div bx
test dx, dx ;is there any remainder
jz showdivisor ;No Remainder - show the divisor
mov ax, cx
jmp petla ; else Do Petla again with new divisor.
showdivisor:
push ax
mov ax, bx
call Show_AX
pop ax
push ax ;; new line
push dx
mov ah, 09h
lea dx, [CrLf]
int 21h
pop dx
pop ax
jmp divideagain ;; divide again with the same divisor until it gives a remainder.
Break:
mov ax, 4c00h
int 20h
Show_AX:
push ax
push bx
push cx
push dx
push di
mov cx, 10
xor di, di
or ax, ax
jns Conv
push ax
mov dx, '-'
mov ah, 2
int 21h
pop ax
neg ax
Conv:
xor dx, dx
div cx
add dl, '0'
inc di
push dx
or ax, ax
jnz Conv
Show:
pop dx
mov ah, 2
int 21h
dec di
jnz Show
pop di
pop dx
pop cx
pop bx
pop ax
ret
; X dw 15 ;testing number as a variable
CrLf db 0Dh, 0Ah, '$'
-
Thank you, Mathi!
I wasn't happy with my answer, so I attempted to "port" it to Linux while keeping Spoomyfly's logic. It hasn't been going well! Sure enough, I fall into an infinite loop, and haven't managed to fix it yet. I like your approach much better.
Again - thank you, thank you, thank you!
Best,
Frank
-
Thank you guys.I will also add my own version.
org 100h
mov ax, 65500
mov bx, 1
petla:
cmp ax, 1
jbe Break
inc bx
mov cx, ax
mov dx, 0
div bx
mov ax, cx
test dx, dx
jnz petla
push ax
mov ax, bx
call Show_AX
pop ax
push ax
push dx
mov ah, 09h
lea dx, [CrLf]
int 21h
pop dx
pop ax
Divide:
mov cx, ax
mov dx, 0
div bx
test dx, dx
jz LWBX
ja RENEW
Break:
mov ax, 4c00h
int 20h
LWBX: dec bx
jmp petla
RENEW: mov ax, cx
jmp petla
Show_AX:
push ax
push bx
push cx
push dx
push di
mov cx, 10
xor di, di
or ax, ax
jns Conv
push ax
mov dx, '-'
mov ah, 2
int 21h
pop ax
neg ax
Conv:
xor dx, dx
div cx
add dl, '0'
inc di
push dx
or ax, ax
jnz Conv
Show:
pop dx
mov ah, 2
int 21h
dec di
jnz Show
pop di
pop dx
pop cx
pop bx
pop ax
ret
CrLf db 0Dh, 0Ah, '$'
Sry for no replies