Intel汇编语言程序设计第五章答案

5.1

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.data 
szText BYTE "这是什么颜色?", 0
color_array DWORD yellow, red, blue, green

.code
main PROC
    call DrawColorfulText
    exit
main ENDP

DrawColorfulText PROC USES edx ecx eax esi
    mov edx, OFFSET [szText]
    mov ecx, LENGTHOF color_array
    mov esi, 0
L1:
    mov eax, [color_array + esi]
    call SetTextColor
    call WriteString
    call Crlf
    add esi, TYPE color_array
    loop L1
    ret
DrawColorfulText ENDP
END main

5.2

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.data 
fibs_array DWORD 47 DUP(0)
fibs_array_size DWORD LENGTHOF fibs_array
fibs_elem DWORD 0
szFileName BYTE "file.txt", 0
prompt_create_file_failed BYTE "创建文件失败", 0
file_handle DWORD 0
.code
main PROC
    call Save47Fibs
    exit
main ENDP

Save47Fibs PROC 
    ; 创造47个斐波那契数组元素
    call Calc47Fibs
    mov edx, OFFSET [szFileName]
    ; 创建文件
    call CreateOutputFile
    cmp eax, INVALID_HANDLE_VALUE
    jz CreateFileFailed
    ; 创建文件成功保存句柄, 写入文件
    mov [file_handle], eax
    mov edx, OFFSET [fibs_array]
    mov ecx, SIZEOF fibs_array
    call WriteToFile
    ; 关闭句柄
    mov eax, [file_handle]
    call CloseFile
    jmp SaveFileSuccess
CreateFileFailed:
    mov edx, OFFSET [prompt_create_file_failed]
    call WriteString
SaveFileSuccess:
    ret
Save47Fibs ENDP

Calc47Fibs PROC USES ecx edx eax 
    mov ecx, [fibs_array_size]
    dec ecx
    mov edi, OFFSET [fibs_array]
    mov eax, 1
    xor edx, edx
    mov [fibs_array], eax
    mov esi, TYPE fibs_array
L1:
    ; 临时保存下元素2
    mov edx, eax
    add eax, [fibs_elem]
    ; 更新元素1
    mov [fibs_elem], edx
    mov [fibs_array + esi], eax
    add esi, TYPE fibs_array
    loop L1
    ret

Calc47Fibs ENDP

; 打印fibs_array数组
PrintArray PROC USES ecx esi 
    mov ecx, [fibs_array_size]
    xor esi, esi
L1:
    mov eax, [fibs_array + esi]
    call WriteDec
    call Crlf
    add esi, TYPE fibs_array
    loop L1

    ret
PrintArray ENDP
END main

5.3

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.data 
prompt_input_text BYTE "输入整数: ", 0

int1 SDWORD 0
int2 SDWORD 0

.code
main PROC
    call Clrscr
    call MoveScreenCentre
    ; 输出提示
    mov edx, OFFSET [prompt_input_text]
    call WriteString
    ; 输入整数
    call ReadInt
    mov [int1], eax
    ; 输出提示
    mov edx, OFFSET [prompt_input_text]
    call WriteString
    ; 输入整数
    call ReadInt 
    mov [int2], eax
    ; 计算和并打印
    add eax, [int1]
    call WriteInt
    exit
main ENDP

MoveScreenCentre PROC USES edx
    mov dh, 12
    mov dl, 40
    call Gotoxy
    ret
MoveScreenCentre ENDP 
END main

5.4

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.data 
prompt_input_text BYTE "输入整数: ", 0
int1 SDWORD 0
int2 SDWORD 0

.code
main PROC
    mov ecx, 3
L:
    call CalcTwoNumSum
    loop L
    exit
main ENDP

MoveScreenCentre PROC USES edx
    mov dh, 12
    mov dl, 40
    call Gotoxy
    ret
MoveScreenCentre ENDP 

CalcTwoNumSum PROC USES edx eax
    call Clrscr
    call MoveScreenCentre
    ; 输出提示
    mov edx, OFFSET [prompt_input_text]
    call WriteString
    ; 输入整数
    call ReadInt
    mov [int1], eax
    ; 输出提示
    mov edx, OFFSET [prompt_input_text]
    call WriteString
    ; 输入整数
    call ReadInt 
    mov [int2], eax
    ; 计算和并打印
    add eax, [int1]
    call WriteInt
    call Crlf
    call WaitMsg
    ret
CalcTwoNumSum ENDP
END main

5.5

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.code
main PROC
    call Gen50RandomNums
    exit
main ENDP

Gen50RandomNums PROC USES ecx eax
    call Randomize
    mov ecx, 50
L:
    ; 生成一个-20~20的随机整数
    mov eax, 41
    call RandomRange
    sub eax, 20
    call WriteInt
    call Crlf
    loop L
    ret
Gen50RandomNums ENDP
END main

5.6

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.data
aString BYTE 10 DUP(0), 0
aStringSize DWORD ($ - aString - 1)
; 大写: 65-90
.code
main PROC
    call Generate20RandomString
    exit
main ENDP

; 生成一串10个大写字母组成的字符串
GenerateRandomString PROC USES ecx eax esi
    mov ecx, [aStringSize]
    xor esi, esi
L1:
    ; 生成0-25的随机数
    mov eax, 26 
    call RandomRange
    ; 范围65-90的随机数
    add eax, 041h
    mov [aString + esi], al
    add esi, TYPE aString
    loop L1
    ret
GenerateRandomString ENDP 

; 打印20串10个大写字母组成的字符串
Generate20RandomString PROC 
    mov ecx, 20
    call Randomize
L:
    call GenerateRandomString
    mov edx, OFFSET [aString]
    call WriteString
    call Crlf
    loop L
    ret
Generate20RandomString ENDP
END main

5.7

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.data
cols BYTE ?
rows BYTE ?
color_array DWORD red, magenta, yellow, green, blue, white, cyan, brown
color_array_size DWORD ($ - color_array) / TYPE DWORD
CONSOLE_WIDTH = 80
CONSOLE_HEIGHT = 25
ALPHABET = 'A'

.code
main PROC
    call Clrscr
    mov ecx, 100
    call Randomize
L:
    ; 获取随机位置并将光标跳转
    call GetRandomPos
    mov dh, [rows]
    mov dl, [cols]
    call Gotoxy
    ; 生成随机颜色
    mov eax, [color_array_size]
    call RandomRange
    mov eax, [color_array + eax * TYPE color_array]
    call SetTextColor
    ; 打印字母
    mov al, ALPHABET
    call WriteChar
    loop L
    
    exit
main ENDP

; 由于GetMaxXY调用失败, 直接使用(25, 80)
GetRandomPos PROC USES eax
    mov eax, CONSOLE_WIDTH
    call RandomRange
    mov [cols], al
    mov eax, CONSOLE_HEIGHT
    call RandomRange
    mov [rows], al
    ret
GetRandomPos ENDP 
END main

5.8

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.data
BgColor DWORD 0
FgColor DWORD 0
Color   DWORD 0
strText BYTE "0123456789ABCDEF", 0

.code
main PROC
    call PrintColorfulMatrix
    exit
main ENDP

PrintColorfulMatrix PROC 
    mov ecx, 16
L1:
    push ecx
    mov ecx, 16
    xor edx, edx
L2:
    ; 计算颜色值
    call CalcColorNum
    mov eax, [Color]
    ; 设置文字颜色
    call SetTextColor
    mov al, [strText + edx]
    call WriteChar
    inc edx
    inc [FgColor]
    loop L2
    mov eax, 0
    mov [FgColor], eax
    call Crlf
    pop ecx
    inc [BgColor]
    loop L1
    ret
PrintColorfulMatrix ENDP

; 计算最终前景色和背景色组合
CalcColorNum PROC USES ecx eax
    mov ecx, 16
    xor eax, eax
L:
    add eax, [BgColor]
    loop L
    add eax, [FgColor]
    mov [Color], eax
    ret
CalcColorNum ENDP 
END main

5.9

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.data
user_input_nums DWORD ?
ARRAY_SIZE = 5
iArray DWORD ARRAY_SIZE DUP(?)

input_int DWORD ?
prompt_input_int BYTE "How many integers will be added? ", 0
prompt_error_info BYTE "The array cannot be larger than ", 0
prompt_input BYTE "Input: ", 0
prompt_result BYTE "Result: ", 0
.code
main PROC
    ; 输出提示信息
    mov edx, OFFSET [prompt_input_int]
    call WriteString
    ; 读取输入数量
    call ReadDec
    ; 判定数量是否超出限制, 超出则报错
    mov [user_input_nums], eax
    cmp eax, ARRAY_SIZE
    jg SHOW_ERROR_PROMPT
    ; 没超出就求和
    call PromptForIntegers
    exit
SHOW_ERROR_PROMPT:
    call PromptErrorInfo
    exit
main ENDP

PromptForIntegers PROC USES ecx edx ebx eax
    mov ecx, [user_input_nums]
    mov edx, OFFSET [prompt_input]
    xor ebx, ebx
    xor esi, esi
L1:
    call WriteString
    call ReadDec
    mov [iArray + esi], eax
    add ebx, [iArray + esi]
    add esi, TYPE iArray
    loop L1
    mov edx, OFFSET [prompt_result]
    call WriteString
    mov eax, ebx
    call WriteDec
    ret
PromptForIntegers ENDP 

PromptErrorInfo PROC USES edx eax
    mov edx, OFFSET [prompt_error_info]
    call WriteString
    mov eax, ARRAY_SIZE
    call WriteDec
    ret
PromptErrorInfo ENDP
END main

(完)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值