汇编语言第三章
Chap3 Elements of Assembly Language 10
Exercises 3.1
For each combination of characters below, determine whether or not it is an allowable label (name). If not, give a reason (a) repeat (e) 2much
Chap3 Elements of Assembly Language 24
Run example Debug Windbg example.exe
Chap3 Elements of Assembly Language
25
3.4 汇编器清单文件(.lst)
ML汇编器在汇编过程中会产生一个清 单文件.lst,这个文件显示源代码、转 换的目标代码以及附近信息. 汇编时需 要二维复交转换器 /Fl, 生产清单文件 ml /c /coff / Fl /Zi example.asm
Edit Masm
6.11
Windbg
cmd
Chap3 Elements of Assembly Language 23
DOS prompt
Assemble
ml /c /coff /Zi example.asm /c 只编译不连接 /coff 生成COFF格式的目标文件 /Zi 告诉汇编器在输出时添加调试信息
是一条指令性语句,mov代表“move”, 但是该指令实际上是完成一个复制操作。 mov number1, eax 指令把寄存器中的数值复制到地址为 number1的内存双字中。
Add
将第二个数字加到第一个数字上。 add eax,number2
Chap3 Elements of Assembly Language 20
example.lst
Chap3 Elements of Assembly Language
26
3.5 常数操作数
数据的表示方式 BYTE directive WORD directive DWORD directive
Chap3 Elements of Assembly Language
(b) exit
(c) more
(f) add
(g) if
(d) EndIf
(h) add2
Chap3 Elements of Assembly Language
11
汇编语言的格式
[名字] 指令助记符 [操作数] [;注解]
2.指令助记符:表示不同操作的指令 3.操作数: 指令执行的对象。依指令的 要求,可能有一个、两个或者没有。 4.注解:该项可有可无,是为源程序所 加的注解,用于提高程序的可读性。 ZeroCount:mov eax,0 ;初始化数据 为0
是一个指示性语句。Proto用来产生一个函数, 函数名字是:ExitProcess,用于结束程序的系 统函数。它有一个参数Dword类型的。
Chap3 Elements of Assembly Language
16
Directives (2)
Include io.h
指示汇编器拷贝一文件io.h到程序中
3
3.1 汇编语言
一个汇编语言的源代码文件包含了一 系列语句。
语句要求每行少于80个字符。(MASM6.1
还允许不长于512个字符的句子,在每行的行尾用 斜杠“\”将一条语句拆分成多行)。
注释语句,开头用“;”。 Mov eax,0 ;初始化数据为0 不区分大小写
Chap3 Elements of Assembly Language
Chap3 Elements of Assembly Language
12
3.2一个完整的实例
从键盘输入两个数据,得到两个数据 的和,显示出来。
Chap3 Elements of Assembly Language
13
C program
#include <stdio.h> main() { long a,b,c; printf("Enter first number: "); scanf("%ld",&a); printf("\nEnter second number: "); scanf("%ld",&b); c=a+b; printf("\n\nThe sum is %ld \n",c); }
27
数据的表示方式
Suffix
H B O or Q none
Base
16 2 8 10
Number System
hexadecimal binary octal decimal
Chap3 Elements of Assembly Language
28
Example of the use of suffixes
将双字整数转换为ASCII码
Chap3 Elements of Assembly Language 21
3.3程序的汇编、链接和运行
汇编语言的过程
Object file (Machine code, .obj) List file (Text file, .lst)
Linker (link)
Assembler Source file (masm) (Text file .asm)
Chap3 Elements of Assembly Language 6
指示性语言(directive)
一条指示性语句告诉汇编器做某种操 作。这种操作并不会产生机器指令, 也不会对目标代码有任何影响。 .code
. 386
.nolist Include io.h
Chap3 Elements of Assembly Language
; valueis FF ; valueis A5
Chap3 Elements of Assembly Language 15
Directives(1)
.386
指明指令集
.model flat
程序工作模式, flat为Windows程序使用的模式 (代码和数据使用同一个4GB段)
ExitProcess
proto near32 stdcall , dwExitCode:dword
29
BYTE directive
一条Byte指示性语句为数据与预留了 一个或多个字节的存储空间。如果数 据是数字的,那么可以被看作为有符 号数据或者无符号数。 无符号数据的表示范围0 to 255 有符号数据的表示 –128 to 127
Chap3 Elements of Assembly Language
Link
link /debug /subsystem:console /entry:start /out:example.exe example.obj io.obj kernel32.lib /subsystem:console 连接器的参数,用来指定程序 的入口函数,console是win32 字符模式应用程序
汇编语言的要素
上章回顾
Hardware
Memory & memory address CPU registers
Chap3 Elements of Assembly Language
2
本章内容
汇编语言
程序的汇编、链接和运行
指令中的操作数
I/O.H中宏的输入/输出
Chap3 Elements of Assembly Language
Executable file (Machine code, .exe)
Debuger (windbg)
Text editor (edit)
Chap3 Elements of Assembly Language
22
需要的软件
Text
Editors Assembler Linker Debuger
7
宏(macro)
一个宏语句是一系列语句的缩写,它 们可以是指令性语句、指示性语句或 者宏。
Chap3 Elements of Assembly Language
8
汇编语言的格式
由汇编语言编写的源程序是由许多语句(也 可称为汇编指令)组成的。每个语句由1~4 个部分组成,其格式是: [名字] 指令助记符 [操作数] [;注解]
1.
名字:给指令或某一存储单元地址所起的名字。可由下列 字符组成: 字母:A ~ z ; 数字:0 ~ 9 ; 特殊字符:?、· 、@、 __、$ 。 数字不能作标识符的第一个字符,而圆点不能用作第一个 字符。标识符最长为247个字符。
9
名字域的用处之一是在程序汇编和链 接后,代表指令在内存中的地址。其 他的指令就可以很容易地找到这个被 标识的指令,如果一条加法指令需要 在程序中循环执行,那么就可以这样 写: addloop: add eax,158 Jmp addloop Jmp是跳转指令。
30
Example of byte
byte1 BYTE 255 ; valueis FF
byte2 BYTE 127 ; valueis 7F byte3 BYTE 91 ; valueis 5B
byte4 BYTE 0
byte5 BYTE -1 byte6 BYTE -91
; valueis 00
Macros
Output
显示存储在prompt1地址中的字符串,使用空字 符串(00)字节来结束显示