题目:
Write a program that randomly chooses between threedifferent colors for displaying text on the screen. Use a loop todisplay twenty lines of text, each with a randomly chosen color.The probabilities for each color are to be as follows: white = 30%,blue = 10%, green = 60%. Hint: generate a random integer between0 and 9. If the resulting integer is in the range 0-2, choose white.If the integer equals 3, choose blue. If the integer is in therange 4-9, choose green.(tip: callSettextcolor)
编写一个程序,随机选择三种不同的颜色在屏幕上显示文本。使用循环显示二十行文本,每行随机选择一种颜色。每种颜色的概率如下:白色=30%,蓝色=10%,绿色=60%。提示:生成一个介于0和9之间的随机整数。如果生成的整数在0到2之间,请选择白色。如果整数等于3,请选择蓝色。如果整数在4-9之间,选择绿色。(提示:callSettextcolor)
代码:
; author: BoPang
; email: 1275189619@qq.com
; 2021/7/11
INCLUDE Irvine32.inc
.data
COUNT = 20 ; produce 20 random numbers
text byte "Hello World :) ",0
.code
main proc
mov edx, OFFSET text
mov ebx,0
mov eax,10
mov ecx,COUNT
L1: push eax
push ebx
push edx
call RandomNumber
push eax
call randomColor
call SetTextColor
call WriteString
pop eax
call WriteInt
call Crlf
pop edx
pop ebx
pop eax
loop L1
invoke ExitProcess,0
main endp
;-------------------------------------------
randomColor proc
;
; return the degree of the score
; Receives: EBX used to compare
; EAX contains the score.
; Returns: a single capital letter in the AL register
;-------------------------------------------
push ebx
mov ebx, 4
; A-------4-9 green
cmp eax, ebx
jae A1
mov ebx, 3
jmp judgeB
A1:
mov eax, green+(black*16)
jmp endrandomColor
; B-------3 blue
judgeB:
cmp eax, ebx
jae B1
mov ebx, 0
jmp judgeC
B1:
mov eax, blue+(black*16)
jmp endrandomColor
; C-------0-2 white
judgeC:
cmp eax, ebx
jae C1
jmp endrandomColor
C1:
mov eax, white+(black*16)
endrandomColor:
pop ebx
ret
randomColor endp
;-------------------------------------------
RandomNumber proc
;
; Generates a random integer between 50 and 99.
; Receives:
; EBX =0
; EAX = 10
; Returns:
; EAX = a random number
;-------------------------------------------------
sub eax,ebx ; calc difference between ranges
call RandomRange
add eax,ebx ; scale to desired range
ret
RandomNumber endp
END main
初学汇编语言,如有错误请多指正!