Question: [ ORG 0 x 2 0 0 0 ] ; Define starting point jmp start ; Jump to start num 1 : dw 6 ,

[ORG 0x2000] ; Define starting point
jmp start ; Jump to start
num1: dw 6,7,8,9,20,21,22,23,24,5,26,0,13,27,30,1,3 ; Array of numbers to check
prime: dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ; Array to store prime numbers (up to 17 primes)
start:
mov si,0 ; Initialize index (SI) to 0
mov di,0 ; Initialize index for the prime array (DI =0)
mov bx,2 ; Start divisor (2)
mov dx,0
check_next_num:
cmp si,34 ; Compare SI (index) with 34(end of num1 array)
jge Halt_program ; If SI >=34, halt the program
; Load the current number into AX
mov ax,[num1+ si]
push bx ; Save BX (divisor register)
push dx ; Save DX (remainder register)
; Call prime check subroutine
call check_prime
pop dx ; Restore DX
pop bx ; Restore BX
; Increment index (SI)
add si,2 ; Move to next number in num1 array (each number is 2 bytes)
jmp check_next_num ; Continue to the next number
Halt_program:
mov ax,0x4C00 ; DOS interrupt to terminate the program
int 0x21 ; Terminate the program with return code 0
check_prime:
cmp ax,2 ; If the number is less than 2, it's not prime
jl not_prime ; Jump if less than 2(not prime)
mov bx,2 ; Set divisor to 2
prime_loop:
push ax
; Divide AX by BX, result in AX, remainder in DX
div bx ; AX = AX / BX, DX = AX % BX
pop ax
cmp dx,0 ; If remainder is 0, it's not prime
je not_prime ; If remainder is 0, it's not prime
inc bx ; Increment divisor
cmp bx, ax ; If divisor >= the original number, it's prime
jl prime_loop ; If divisor is less than the original number, continue loop
; If we are here, the number is prime
mov [prime + di], ax ; Store the prime number in the prime array (prime[di])
add di,2 ; Increment the prime array index (DI) by 2(since each entry is 2 bytes)
ret
not_prime:
ret ; Return from prime check subroutine
my assembly code is not working properly, i need help fixing it. My teacher says stack must be used in code.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!