Write an assembly language subroutine that checks whether a number, passed in R1
ID: 3755433 • Letter: W
Question
Write an assembly language subroutine that checks whether a number, passed in R1, is a prime number or not. If it is prime, 1 is returned in R0. If not prime, 0 is returned. Write a main program which calls this subroutine and counts the number of primes between 10 and 2000.Write an assembly language subroutine that checks whether a number, passed in R1, is a prime number or not. If it is prime, 1 is returned in R0. If not prime, 0 is returned. Write a main program which calls this subroutine and counts the number of primes between 10 and 2000.
Write an assembly language subroutine that checks whether a number, passed in R1, is a prime number or not. If it is prime, 1 is returned in R0. If not prime, 0 is returned. Write a main program which calls this subroutine and counts the number of primes between 10 and 2000.
Explanation / Answer
org 100h ;start address mov cx, 512 ;1024 bytes mov di, primes ;clear table mov bx, di ;bx is used to fill table xor ax, ax rep stosw ;clear table mov al, 1 ;not a prime mov dx,2 ;prime number call fillprimes ;sieve the table mov dx,3 ;prime call fillprimes ;sieve mov dx,5 call fillprimes mov dx,7 call fillprimes mov dx,11 call fillprimes mov dx,13 call fillprimes mov dx,17 call fillprimes mov dx,19 call fillprimes mov dx,23 call fillprimes mov dx,29 call fillprimes mov dx,31 ;prime (sqrt 1024 is 32, so last one) call fillprimes ;sieve mov primes, al ;0 is not prime mov primes + 1, al ;neither is 1 mov cx, 1024 ;size of primes mov si, primes ;address of primes mov bx, 12 ;11 per line (+1 on first line) printloop: lodsb ;get byte from table and al, al ;was it prime jne notprime ;if set, then it wasn't mov ax, si ;get the index sub ax, primes + 1 ;+1 to account for autoincrement call print_ax ;display prime mov dx, comma ;get the text to be displayed dec bx ;more values on line jne endline ;reached the end of line mov dx, crlf ;display end of line mov bx, 11 ;11 values per line endline: mov ah, 9 ;the print command int 21h ;print string notprime: loop printloop ;check the next value ret fillprimes: mov si, dx ;index register si fillprimes1: add si, dx ;mark multiples of dx as non prime mov cx, si ;check for end of table cmp cx, 1024 ;reached the end jge fillprimes2 ;stop the end mov [bx + si], al ;mark as non prime jmp fillprimes1 ;loop until complete fillprimes2: ret print_ax proc cmp ax, 0 ;check for 0 jne print_ax_recursive push ax ;save ax mov ax, 0e30h ;ah = 0eh al = '0' int 10h ;print single character pop ax ;restore ax ret ;exit print_ax_recursive: pusha ;push all the registers xor dx, dx ;dx = 0 cmp ax, dx ;is ax = 0? je print_ax_exit ;yep, so don't print mov bx, 10 ;divide by 10 div bx call print_ax_recursive ;call recursive mov al, dl ;get the digit add al, '0' ;convert to ascii mov ah, 0eh int 10h ;display the character print_ax_exit: popa ;restore all the registers ret endp ;end the procedure crlf: db 10, 13, '$' comma: db ", $" primes: db 255 dup ? ;255 is max for dup db 255 dup ? db 255 dup ? db 255 dup ? db 4 dup ? ;1024 bytes total
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.