🔰 第一章:汇编语言是什么?

✅ 汇编语言是:

  • 靠近计算机底层的语言
  • 每一条汇编指令对应一个机器指令
  • 更容易控制硬件,效率高,但书写复杂

✅ 编写汇编程序需要:

  • 汇编器:MASM(微软宏汇编器)
  • Irvine32 库:老师常用的教学库(简化输入输出等操作)

📁 第二章:汇编程序结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
include irvine32.inc      ; 包含 Irvine32 的函数库

.data ; 数据段,定义变量
msg byte "hello", 0

.code ; 代码段,写程序的地方
main proc ; main 是主过程入口
mov edx, offset msg ; 把字符串地址放入 edx
call writestring ; 调用写字符串的函数
call crlf ; 换行
exit ; 正确退出程序(注意不是 call exit)
main endp

end main ; 程序结束,入口为 main

🧱 第三章:常用寄存器解释(32 位)

寄存器 全名 作用
eax Accumulator 通用计算、返回值、常用
ebx Base 通用变量寄存器
ecx Counter 常用作计数器(循环)
edx Data 数据相关、乘除法使用
esi Source Index 源地址,数组遍历常用
edi Destination 目标地址
ebp Base Pointer 基址指针,访问参数使用
esp Stack Pointer 栈顶指针

🧾 第四章:变量定义和数据段

1
2
3
4
.data
num1 dword 100 ; 32位整数,初值100
arr dword 10, 20, 30 ; 定义数组
msg byte "Hello", 0 ; 定义字符串(0表示结束)
  • dword:定义一个 32 位整数
  • byte:定义一个字节数据
  • offset:用来获取变量的地址

🔧 第五章:基本指令详解

✅ 数据传送类

1
2
3
4
mov eax, 100           ; 把100送入 eax
mov ebx, eax ; 把 eax 的值复制到 ebx
mov eax, [arr] ; 读取 arr[0] 的值
mov [arr+4], ebx ; 把 ebx 存入 arr[1]

✅ 算术运算

1
2
3
4
5
add eax, 5             ; eax = eax + 5
sub ebx, 1 ; ebx = ebx - 1
inc ecx ; ecx++
dec ecx ; ecx--
imul eax, ebx ; eax = eax * ebx

✅ 比较与跳转

1
2
3
4
cmp eax, ebx           ; 比较 eax 和 ebx
je label ; 如果相等跳转
jne label ; 如果不等跳转
jl / jg / jle / jge ; 小于/大于/小于等于/大于等于

🔁 第六章:循环结构

示例:输出 1 到 5

1
2
3
4
5
6
7
mov ecx, 5
mov eax, 1
L1:
call writeint
call crlf
inc eax
loop L1 ; ecx = ecx - 1,如果不为0跳回

📞 第七章:过程调用(函数)

写法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
main proc
push 5
call printNumber
exit
main endp

printNumber proc
push ebp
mov ebp, esp
mov eax, [ebp+8] ; 取参数
call writeint
call crlf
pop ebp
ret 4 ; 参数一个,占 4 字节
printNumber endp

📌 第八章:数组操作基础

1
2
3
4
5
6
7
8
9
10
11
12
13
.data
arr dword 5, 10, 15, 20

.code
mov esi, offset arr ; esi = arr 地址
mov ecx, 4 ; 循环次数 = 元素个数
mov ebx, 0
loop_start:
mov eax, [esi+ebx*4] ; eax = arr[ebx]
call writeint
call crlf
inc ebx
loop loop_start

🔚 第九章:正确退出程序

1
exit         ; 这是宏,写在 main 的最后

🚫 不要写 call exit,那是错误用法!


✅ 第十章:用 Irvine32 输入输出

功能 使用方法
输出整数 mov eax, num + call writeint
输入整数 call readinteax中返回值
输出字符串 mov edx, offset msg + call writestring
换行 call crlf