masm汇编找100以内素数并求和红色输出

本文介绍了一款使用MASM汇编语言实现的暴力求素数程序,通过筛选法找出100以内的所有素数,并在屏幕上动态展示求素数的过程。程序还计算并以红色显示素数的平均值,利用子程序进行数据处理,实现清晰的用户交互界面。

汇编课设暴力版

要求

求 100 以内的素数。(1)用筛法求出这些素数。(2)在屏幕上显示出求素数的动态过程(在屏幕上先显示出 100 以内的所有数, 再动态地删去不符合要求的数,删除的过程要明显)。(3)计算这些素数的平均值(取整,四舍五入),以十进制形式输出,并让该值以红色显示。(4)数据的输入和结果的输出都要有必要的提示,且提示独占一行。(5)要使用到子程序。

暴力找因数版代码masm

DATAS SEGMENT
    strsum db 'xxxx$'
	sum dw ?
    temp db ?
    ans db 300 dup(?)
    outsum db 00h
DATAS ENDS
 
STACKS SEGMENT
    
STACKS ENDS
 
CODES SEGMENT
    ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
    mov ax, datas
    mov ds, ax
    mov ax, 0b800h
    mov es, ax ; 保存显存的段地址
    mov si, 0000H ; 数据段偏移地址
    mov di, 21*160 ; 显存偏移地址
    call MAIN
MAIN proc near
	XOR bx,bx
    XOR ax,ax
	mov sum,BX
    mov BL, 2        ; save i(that is the dividend)
    mov BH, 2        ; save j(that is the divider)
    mov CL, 1        ; save flag(1 represent isPrime, 0 represent isNotPrime)
ISPRIME:
    cmp BL,100       ; check if need to end the program
    je STOP          ; end the program
    cmp BL,BH        ; check if divider reach to dividend iteself
    je ISCANPRINT    ; judge if can print
    XOR AX,AX        ; empty AX
    MOV AL,BL        ; move dividend to AL
    DIV BH           ; AL%BH=AH remainder stored in AH
    cmp AH,0         ; Is there any exception
    je NOTPRIME      ; if AH stores 0, that means the value stored in BL is not prime
    inc BH           ; increase the divider
    jmp ISPRIME      ; turn to the beginning and loop
ISCANPRINT:
    cmp CL,1         ; check flag
    je PRINT         ; print the value
    mov BH,2         ; if not prime, divider begin again at 2 
    mov CL,1         ; reset flag
    jmp ISPRIME      ; turn to the beginning and loop
PRINT: 
    XOR BH,BH        ; empty BH
    add sum,BX
    mov AX,BX        ; move value to AX
    call PRINTNUMBER ; print the number
 
NOTPRIME:
    inc BL           ; increase dividend
    mov CL, 0        ; set flag
    jmp ISCANPRINT   ; turn to judge
STOP:
    mov di,22*160    ; println
    call PRINTSTR
    mov di,23*160
    mov outsum,01h
    mov AX,sum
    call PRINTNUMBER
    mov ah,01h
    int 21h
    MOV  ah,4CH
    int  21H
    ret
MAIN endp

OUTNORM proc near	; print color character
    push ax
    mov al,temp
    cmp outsum,01h
    je RED
    mov ah,00000111B
    jmp continue
RED:
    mov ah,00100100B
continue:
    mov es:[di],ax
    add di,02h
    pop ax
    ret
OUTNORM endp

PRINTSTR proc near  ; print'sum'
    push ax
    push bx
    push cx
    push dx
    XOR dx,dx
    XOR ax,ax

    mov dl,'s'
    mov temp,dl
    call OUTNORM
    ;mov ah,2            
    ;int 21h
    mov dl,'u'
    mov temp,dl
    call OUTNORM
    ;mov ah,2            
    ;int 21h 
    mov dl,'m'
    mov temp,dl
    call OUTNORM
    ;mov ah,2            
    ;int 21h   
    pop dx
    pop cx
    pop bx
    pop ax
    ret
PRINTSTR endp

PRINTNUMBER proc near
    push ax
    push bx
    push cx
    push dx
 
    
    mov bx,10
    mov cx,0
 
PUSHTOSTACK:
    mov dx,0
    div bx
    
    push dx
    inc cx
    
    cmp ax,0
    jz POPFROMSTACK
    
    jmp PUSHTOSTACK
    
POPFROMSTACK:
    pop dx
    add dl,30h         ; change ASCII to real number
    mov temp,dl
    call OUTNORM
    ;mov ah,2            
    ;int 21h
           
    loop POPFROMSTACK
    
    pop dx
    pop cx
    pop bx
    pop ax
    mov temp,' '        ; print an empty space
    call OUTNORM
    ;mov AH,2
    ;mov DL,0
    ;int 21h
    ret
PRINTNUMBER endp


CODES ENDS
    END START


采用往显存里塞入字符和属性的办法输出带颜色字符。具体输出原理参见https://www.jianshu.com/p/450594ab3ca8。
效果:
在这里插入图片描述

以下是一个用汇编语言(MASM编写的计算输出100以内素数的程序示例: ```asm .MODEL SMALL .STACK 100H .DATA Prime DB 100 DUP(?) ; 用于存储素数的数组 Pcounter DB 0 ; 素数的个数 newline DB 13, 10, '$' ; 换行符 .CODE MAIN PROC MOV AX, @DATA MOV DS, AX MOV CX, 2 ; 从2开始,因为1不是素数 CHECK_LOOP: MOV AL, CL ; 被除数 MOV BL, 2 ; 除数 DIVIDE_LOOP: MOV AH, 0 ; 清余数 DIV BL ; 进行除法运算 CMP AH, 0 ; 检查余数是否为0 JE NOT_PRIME ; 如果余数为0,不是素数 INC BL ; 除数加1 CMP BL, AL ; 检查除数是否达到被除数 JL DIVIDE_LOOP ; 如果未达到,继续除法循环 ; 是素数,存入数组 MOV SI, Pcounter MOV Prime[SI], CL INC Pcounter ; 素数个数加1 NOT_PRIME: INC CX ; 被除数加1 CMP CX, 100 ; 检查是否达到100 JLE CHECK_LOOP ; 如果未达到,继续检查 ; 输出素数 MOV SI, 0 PRINT_LOOP: MOV DL, Prime[SI] ADD DL, 30H ; 转换为ASCII码 MOV AH, 02H INT 21H ; 输出字符 ; 输出空格 MOV DL, ' ' MOV AH, 02H INT 21H INC SI CMP SI, Pcounter JL PRINT_LOOP ; 输出换行 LEA DX, newline MOV AH, 09H INT 21H ; 退出程序 MOV AH, 4CH INT 21H MAIN ENDP END MAIN ``` 这个程序的逻辑是: 1. 从2开始遍历到100,对每个数进行素数判断。 2. 对于每个数,从2开始作为除数进行除法运算,直到除数等于该数本身。如果在这个过程中余数为0,则该数不是素数;否则,该数是素数,将其存入`Prime`数组,增加`Pcounter`的值。 3. 最后,将存储在`Prime`数组中的素数依次输出在每个素数输出一个空格,最后输出换行符。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值