汇编课设暴力版
要求
求 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。
效果: