汇编语言 在控制台console显示一段随机颜色的文本 x86 汇编语言 asm文件 irvine32 intel 汇编语言

题目:

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

初学汇编语言,如有错误请多指正!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖虎技安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值