汇编语言输出100-200之间的素数每行5个

2024-12-31 01:27:47
推荐回答(1个)
回答1:

;用汇编语言,编写程序输出100-200之间所有的素数,每行输出5个数,在线等,急求
assume cs:code, ds:data
data segment
    PRIME db 100 dup ('$')
    FLAG db ?
    COL db ?
    CRLF db 0AH, 0DH, '$'
    DISPLAY db 4 dup ('$')
data ends
code segment
    start:
                    mov ax, data
                    mov ds, ax
                    sub ax, ax
                    mov di, offset PRIME
                                                                                                  
                    mov al, 101
                    mov bl, 200
    lp:         cmp al, bl
                    ja dis
                    mov FLAG, 0
                    mov cl, 2
    lp2:        cmp cl, al
                    jnb done                ;当cl >= al时, 跳转
                    mov dl, al
                    div cl
                    cmp ah, 0
                    je done1
                    sub ah, ah
                    mov al, dl
                    inc cl
                    jmp lp2
                                                                                                  
                                                                                  
    done1:  mov FLAG, 1
    done:       cmp FLAG, 0
                    jne done2
                    mov byte ptr [di], dl
                    inc di
                                                                                                                      
    done2:  mov al, dl
                    add al, 2
                    jmp lp
                                                                                                  
                                                                                                  
    dis:        mov di, offset PRIME - 1
                    mov si, offset DISPLAY
                    mov COL, 0
                    mov cl, 10
    done4:  inc di
                    sub ax, ax
                    mov al, [di]
                    mov bl, 2
                    cmp al, '$'
                    je exit
    done5:  div cl
                    add ah, 30h
                    mov byte ptr [si+bx], ah
                    dec bl
                    cmp al, 0
                    je done6
                    sub ah, ah
                    jmp done5
                                                                                                  
    done6:  mov ah, 9                       ;输出素数
                    mov dx, si
                    int 21h
                                                                                                  
                    mov ah, 2                       ;输出空格
                    mov dl, ' '
                    int 21h
                                                                                                  
                    inc COL
                    cmp COL, 5                  ;看这一行是否有5个素数
                    jne done7                       ;不是的话,那么跳转到done7
                                                                                                  
                                                                                                  
                    mov ah, 9                       ;否则输出空行
                    mov dx, offset CRLF
                    int 21h
                    mov COL, 0                  ;下一行重新置0
                                                                                                  
    done7:  jmp done4  
                                                                                                  
                                                                                                  
    exit:       mov ax, 4c00h
                    int 21h    
code ends
end start




PS:若有不明白的地方, 请追问