1. 输出 Hello World

这是最基础的字符串输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
include Irvine32.inc

.data
hello db "Hello World", 0

.code
main PROC
mov edx, offset hello
call WriteString
call Crlf ; 换行
exit
main ENDP

end main

2. 比较 3 个数找最大值

利用 cmp 指令和条件跳转来比较三个数的大小。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
include Irvine32.inc

.data
num1 dd 10
num2 dd 20
num3 dd 30

msgMax db "最大值是: ", 0

.code
main PROC
mov eax, [num1]
mov ebx, [num2]
cmp eax, ebx
jge check_num3 ; 如果 num1 >= num2 跳到检查 num3

mov eax, ebx ; 否则 num2 是更大值
check_num3:
mov ebx, [num3]
cmp eax, ebx
jge done
mov eax, ebx ; 如果 num3 大,则更新 eax

done:
mov edx, offset msgMax
call WriteString
call WriteInt
call Crlf
exit
main ENDP

end main

3. 数组求和

循环遍历数组并累加求和。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
include Irvine32.inc

.data
arr dd 1, 2, 3, 4, 5
arrSize dd 5
msgSum db "数组和是: ", 0

.code
main PROC
mov esi, offset arr
mov ecx, [arrSize]
xor eax, eax ; 清空 eax 用于存储和

sum_loop:
cmp ecx, 0
je sum_done
add eax, [esi] ; 累加数组元素
add esi, 4 ; 移动到下一个元素
dec ecx
jmp sum_loop

sum_done:
mov edx, offset msgSum
call WriteString
call WriteInt
call Crlf
exit
main ENDP

end main

4. 判断偶数或奇数

使用 andtest 指令判断一个数是偶数还是奇数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
include Irvine32.inc

.data
num dd 42 ; 输入数字
msgEven db "是偶数", 0
msgOdd db "是奇数", 0

.code
main PROC
mov eax, num
test eax, 1 ; 判断最低位是否为 1
jz eve ; 如果为 0 是偶数

mov edx, offset msgOdd
call WriteString
call Crlf
jmp done

eve:
mov edx, offset msgEven
call WriteString
call Crlf

done:
exit
main ENDP

end main

5. 输入 3 个整数并找出最小值

使用 ReadInt 读取输入并比较找出最小值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
include Irvine32.inc

.data
prompt1 db "请输入第一个整数: ", 0
prompt2 db "请输入第二个整数: ", 0
prompt3 db "请输入第三个整数: ", 0
msgMin db "最小值是: ", 0

num1 dd 0 ; 存储第一个整数
num2 dd 0 ; 存储第二个整数
num3 dd 0 ; 存储第三个整数

.code

main PROC
; 提示输入第一个整数
mov edx, offset prompt1
call WriteString
call ReadInt
mov num1, eax ; 存储第一个整数

; 提示输入第二个整数
mov edx, offset prompt2
call WriteString
call ReadInt
mov num2, eax ; 存储第二个整数

; 提示输入第三个整数
mov edx, offset prompt3
call WriteString
call ReadInt
mov num3, eax ; 存储第三个整数

; 比较并找出最小值
mov eax, num1 ; 将第一个整数加载到 eax
mov ebx, num2 ; 将第二个整数加载到 ebx
cmp eax, ebx ; 比较 num1 和 num2
jle next1 ; 如果 num1 <= num2,跳到 next1
mov eax, ebx ; 否则,eax = num2

next1:
mov ecx, num3 ; 将第三个整数加载到 ecx
cmp eax, ecx ; 比较当前的最小值和 num3
jle done ; 如果当前最小值 <= num3,跳到 done
mov eax, ecx ; 否则,eax = num3

done:
; 输出最小值
mov edx, offset msgMin
call WriteString
call WriteInt ; 输出最小值
call Crlf
exit
main ENDP

end main