|
|
|
[翻译]Exploit 编写系列教程第九篇Win32 Shellcode编写入门
收藏学习 感谢dragonltx的翻译了 |
|
[分享]发个32位汇编语言教程
是的,你说对了。好好努力吧! |
|
[调查]这本书在深圳已经买不到了吗?
才去深圳购书中心看了,没找到 |
|
[注意]邀请码发放公布(05.06~05.17)
谢谢,注册码已经收到了! |
|
[分享]汇编系列教程笔记—5月14日更新[邀请码已发]
占更新位。。。。广州出差中。。。 |
|
[分享]汇编系列教程笔记—5月14日更新[邀请码已发]
占更新位。。。。广州出差中。。。 |
|
[分享]汇编系列教程笔记—5月14日更新[邀请码已发]
存储器的管理模式 Intel公司的80X86系列的CPU基本上采用内存分段的管理模式。它把内存和程序分成若干个段,每个段的起点用一个段寄存器来记忆,所以,学习汇编语言,必须要清楚地理解存储器的分段含义、存储单元的逻辑地址和其物理地址之间的转换关系。 16位微机的内存管理模式 1、存储器的分段 我们知道:计算机的内存单元是以“字节”为最小单位进行线性编址的。为了标识每个存储单元,就给每个存储单元规定一个编号,此编号就是该存储单元的物理地址。 存储单元的物理地址是一个无符号的二进制数。但为了书写的简化,物理地址通常用十六进制来表示。 16位CPU内部有20根地址线,其编码区间为:00000H~0FFFFFH,所以,它可直接访问的物理空间为1M(2的20次方)字节。而16位CPU内部存放存储单元偏移量的寄存器(如:IP、SP、BP、SI、DI和BX等)都是16位,它们的编码范围仅为:00000H~0FFFFH。这样,如果用16位寄存器来访问内存的话,则只能访问内存的最低端的64K,其它的内存将无法访问。为了能用16位寄存器来有效地访问1M的存储空间,16位CPU采用了内存分段的管理模式,并引用段寄存器的概念。 16位微机把内存空间划分成若干个逻辑段,每个逻辑段的要求如下: 逻辑段的起始地址(通常简称为:段地址)必须是16的倍数,即最低4位二进制必须全为0; 逻辑段的最大容量为64K,这由16位寄存器的寻址空间所决定。 按上述规定,1M内存最多可分成64K个段,即65536个段(段之间相互重叠),至少可分成16个相互不重叠的段。 下图是内存各逻辑段之间的分布情况示意图,其中有相连的段(如:C和D段)、不相连的段(如:A和B段)以及相互重叠的段(如:B和C段)。 这种存储器分段的内存管理方法不仅实现了用两个16位寄存器来访问1M的内存空间,而且对程序的重定位、浮动地址的编码和提高内存的利用率等方面都具有重要的实用价值。 |
|
[分享]汇编系列教程笔记—5月14日更新[邀请码已发]
专用寄存器的作用 16位CPU内部有一个16位的标志寄存器,它包含9个标志位。这些标志位主要用来反映处理器的状态和运算结果的某些特征。各标志位在标志寄存器内的分布如图所示。 上面9个标志位可分为二组:运算结果标志位(有背景色的标志位)和状态控制标志位。前者受算术运算和逻辑运算结果的影响,后者受一些控制指令执行的影响。 一、运算结果标志位 1、进位标志CF(Carry Flag) 进位标志CF主要用来反映运算是否产生进位或借位。如果运算结果的最高位产生了一个进位或借位,那么,其值为1,否则其值为0。 使用该标志位的情况有:多字(字节)数的加减运算,无符号数的大小比较运算,移位操作,字(字节)之间移位,专门改变CF值的指令等。 2、奇偶标志PF(Parity Flag) 奇偶标志PF用于反映运算结果中“1”的个数的奇偶性。如果“1”的个数为偶数,则PF的值为1,否则其值为0。 利用PF可进行奇偶校验检查,或产生奇偶校验位。在数据传送过程中,为了提供传送的可靠性,如果采用奇偶校验的方法,就可使用该标志位。 3、辅助进位标志AF(Auxiliary Carry Flag) 在发生下列情况时,辅助进位标志AF的值被置为1,否则其值为0: (1)、在字操作时,发生低字节向高字节进位或借位时; (2)、在字节操作时,发生低4位向高4位进位或借位时。 对以上6个运算结果标志位,在一般编程情况下,标志位CF、ZF、SF和OF的使用频率较高,而标志位PF和AF的使用频率较低。 4、零标志ZF(Zero Flag) 零标志ZF用来反映运算结果是否为0。如果运算结果为0,则其值为1,否则其值为0。在判断运算结果是否为0时,可使用此标志位。 5、符号标志SF(Sign Flag) 符号标志SF用来反映运算结果的符号位,它与运算结果的最高位相同。在微机系统中,有符号数采用补码表示法,所以,SF也就反映运算结果的正负号。运算结果为正数时,SF的值为0,否则其值为1。 6、溢出标志OF(Overflow Flag) 溢出标志OF用于反映有符号数加减运算所得结果是否溢出。如果运算结果超过当前运算位数所能表示的范围,则称为溢出,OF的值被置为1,否则,OF的值被清为0。 “溢出”和“进位”是两个不同含义的概念,不要混淆。如果不太清楚的话,请BAIDU一下。 二、状态控制标志位 状态控制标志位是用来控制CPU操作的,它们要通过专门的指令才能使之发生改变。 1、追踪标志TF(Trap Flag) 当追踪标志TF被置为1时,CPU进入单步执行方式,即每执行一条指令,产生一个单步中断请求。这种方式主要用于程序的调试。 指令系统中没有专门的指令来改变标志位TF的值,但程序员可用其它办法来改变其值。 2、中断允许标志IF(Interrupt-enable Flag) 中断允许标志IF是用来决定CPU是否响应CPU外部的可屏蔽中断发出的中断请求。但不管该标志为何值,CPU都必须响应CPU外部的不可屏蔽中断所发出的中断请求,以及CPU内部产生的中断请求。具体规定如下: (1)、当IF=1时,CPU可以响应CPU外部的可屏蔽中断发出的中断请求; (2)、当IF=0时,CPU不响应CPU外部的可屏蔽中断发出的中断请求。 CPU的指令系统中也有专门的指令来改变标志位IF的值。 3、方向标志DF(Direction Flag) 方向标志DF用来决定在串操作指令执行时有关指针寄存器发生调整的方向。 在微机的指令系统中,还提供了专门的指令来改变标志位DF的值。 三、32位标志寄存器增加的标志位 1、I/O特权标志IOPL(I/O Privilege Level) I/O特权标志用两位二进制位来表示,也称为I/O特权级字段。该字段指定了要求执行I/O指令的特权级。如果当前的特权级别在数值上小于等于IOPL的值,那么,该I/O指令可执行,否则将发生一个保护异常。 2、嵌套任务标志NT(Nested Task) 嵌套任务标志NT用来控制中断返回指令IRET的执行。具体规定如下: (1)、当NT=0,用堆栈中保存的值恢复EFLAGS、CS和EIP,执行常规的中断返回操作; (2)、当NT=1,通过任务转换实现中断返回。 3、重启动标志RF(Restart Flag) 重启动标志RF用来控制是否接受调试故障。规定:RF=0时,表示“接受”调试故障,否则拒绝之。在成功执行完一条指令后,处理机把RF置为0,当接受到一个非调试故障时,处理机就把它置为1。 4、虚拟8086方式标志VM(Virtual 8086 Mode) 如果该标志的值为1,则表示处理机处于虚拟的8086方式下的工作状态,否则,处理机处于一般保护方式下的工作状态。 有些指令的执行会改变标志位(如:算术运算指令等),不同的指令会影响不同的标志位,有些指令的执行不改变任何标志位(如:MOV指令等),有些指令的执行会受标志位的影响(如:条件转移指令等),也有指令的执行不受其影响。 程序员要想熟练运用这些标志位,就必须掌握每个标志位的含义、每条指令的执行条件和执行结果对标志位的作用。 注意:虽然知道每个标志位在标志寄存器内的具体位置是有好处的,但通常情况下,没有这个必要。在使用“条件转移指令”时,系统会自动引用相应标志位的值来决定是否需要“转移”的,所以,不必过分强调标志位在标志寄存器内的具体位置。 |
|
[分享]汇编系列教程笔记—5月14日更新[邀请码已发]
一、运算结果标志位 1、进位标志CF(Carry Flag) 进位标志CF主要用来反映运算是否产生进位或借位。如果运算结果的最高位产生了一个进位或借位,那么,其值为1,否则其值为0。 使用该标志位的情况有:多字(字节)数的加减运算,无符号数的大小比较运算,移位操作,字(字节)之间移位,专门改变CF值的指令等。 2、奇偶标志PF(Parity Flag) 奇偶标志PF用于反映运算结果中“1”的个数的奇偶性。如果“1”的个数为偶数,则PF的值为1,否则其值为0。 利用PF可进行奇偶校验检查,或产生奇偶校验位。在数据传送过程中,为了提供传送的可靠性,如果采用奇偶校验的方法,就可使用该标志位。 3、辅助进位标志AF(Auxiliary Carry Flag) 在发生下列情况时,辅助进位标志AF的值被置为1,否则其值为0: (1)、在字操作时,发生低字节向高字节进位或借位时; (2)、在字节操作时,发生低4位向高4位进位或借位时。 对以上6个运算结果标志位,在一般编程情况下,标志位CF、ZF、SF和OF的使用频率较高,而标志位PF和AF的使用频率较低。 4、零标志ZF(Zero Flag) 零标志ZF用来反映运算结果是否为0。如果运算结果为0,则其值为1,否则其值为0。在判断运算结果是否为0时,可使用此标志位。 5、符号标志SF(Sign Flag) 符号标志SF用来反映运算结果的符号位,它与运算结果的最高位相同。在微机系统中,有符号数采用补码表示法,所以,SF也就反映运算结果的正负号。运算结果为正数时,SF的值为0,否则其值为1。 6、溢出标志OF(Overflow Flag) 溢出标志OF用于反映有符号数加减运算所得结果是否溢出。如果运算结果超过当前运算位数所能表示的范围,则称为溢出,OF的值被置为1,否则,OF的值被清为0。 “溢出”和“进位”是两个不同含义的概念,不要混淆。如果不太清楚的话,请查阅《计算机组成原理》课程中的有关章节。 二、状态控制标志位 状态控制标志位是用来控制CPU操作的,它们要通过专门的指令才能使之发生改变。 1、追踪标志TF(Trap Flag) 当追踪标志TF被置为1时,CPU进入单步执行方式,即每执行一条指令,产生一个单步中断请求。这种方式主要用于程序的调试。 指令系统中没有专门的指令来改变标志位TF的值,但程序员可用其它办法来改变其值。 2、中断允许标志IF(Interrupt-enable Flag) 中断允许标志IF是用来决定CPU是否响应CPU外部的可屏蔽中断发出的中断请求。但不管该标志为何值,CPU都必须响应CPU外部的不可屏蔽中断所发出的中断请求,以及CPU内部产生的中断请求。具体规定如下: (1)、当IF=1时,CPU可以响应CPU外部的可屏蔽中断发出的中断请求; (2)、当IF=0时,CPU不响应CPU外部的可屏蔽中断发出的中断请求。 CPU的指令系统中也有专门的指令来改变标志位IF的值。 3、方向标志DF(Direction Flag) 方向标志DF用来决定在串操作指令执行时有关指针寄存器发生调整的方向。具体规定在第5.2.11节——字符串操作指令——中给出。在微机的指令系统中,还提供了专门的指令来改变标志位DF的值。 三、32位标志寄存器增加的标志位 1、I/O特权标志IOPL(I/O Privilege Level) I/O特权标志用两位二进制位来表示,也称为I/O特权级字段。该字段指定了要求执行I/O指令的特权级。如果当前的特权级别在数值上小于等于IOPL的值,那么,该I/O指令可执行,否则将发生一个保护异常。 2、嵌套任务标志NT(Nested Task) 嵌套任务标志NT用来控制中断返回指令IRET的执行。具体规定如下: (1)、当NT=0,用堆栈中保存的值恢复EFLAGS、CS和EIP,执行常规的中断返回操作; (2)、当NT=1,通过任务转换实现中断返回。 3、重启动标志RF(Restart Flag) 重启动标志RF用来控制是否接受调试故障。规定:RF=0时,表示“接受”调试故障,否则拒绝之。在成功执行完一条指令后,处理机把RF置为0,当接受到一个非调试故障时,处理机就把它置为1。 4、虚拟8086方式标志VM(Virtual 8086 Mode) 如果该标志的值为1,则表示处理机处于虚拟的8086方式下的工作状态,否则,处理机处于一般保护方式下的工作状态。 |
|
[分享]汇编系列教程笔记—5月14日更新[邀请码已发]
1、数据寄存器 数据寄存器主要用来保存操作数和运算结果等信息,从而节省读取操作数所需占用总线和访问存储器的时间。 32位CPU有4个32位的通用寄存器EAX、EBX、ECX和EDX。对低16位数据的存取,不会影响高16位的数据。这些低16位寄存器分别命名为:AX、BX、CX和DX,它和先前的CPU中的寄存器相一致。 4个16位寄存器又可分割成8个独立的8位寄存器(AX:AH-AL、BX:BH-BL、CX:CH-CL、DX:DH-DL),每个寄存器都有自己的名称,可独立存取。程序员可利用数据寄存器的这种“可分可合”的特性,灵活地处理字/字节的信息。 寄存器AX和AL通常称为累加器(Accumulator),用累加器进行的操作可能需要更少时间。累加器可用于乘、除、输入/输出等操作,它们的使用频率很高; 寄存器BX称为基地址寄存器(Base Register)。它可作为存储器指针来使用; 寄存器CX称为计数寄存器(Count Register)。在循环和字符串操作时,要用它来控制循环次数;在位操作中,当移多位时,要用CL来指明移位的位数; 寄存器DX称为数据寄存器(Data Register)。在进行乘、除运算时,它可作为默认的操作数参与运算,也可用于存放I/O的端口地址。 在16位CPU中,AX、BX、CX和DX不能作为基址和变址寄存器来存放存储单元的地址,但在32位CPU中,其32位寄存器EAX、EBX、ECX和EDX不仅可传送数据、暂存数据保存算术逻辑运算结果,而且也可作为指针寄存器,所以,这些32位寄存器更具有通用性。详细内容请见第3.8节——32位地址的寻址方式。 2、变址寄存器 32位CPU有2个32位通用寄存器ESI和EDI。其低16位对应先前CPU中的SI和DI,对低16位数据的存取,不影响高16位的数据。 寄存器ESI、EDI、SI和DI称为变址寄存器(Index Register),它们主要用于存放存储单元在段内的偏移量,用它们可实现多种存储器操作数的寻址方式(在第3章有详细介绍),为以不同的地址形式访问存储单元提供方便。 变址寄存器不可分割成8位寄存器。作为通用寄存器,也可存储算术逻辑运算的操作数和运算结果。 它们可作一般的存储器指针使用。在字符串操作指令的执行过程中,对它们有特定的要求,而且还具有特殊的功能。具体描述请见第5.2.11节。 3、指针寄存器 32位CPU有2个32位通用寄存器EBP和ESP。其低16位对应先前CPU中的SBP和SP,对低16位数据的存取,不影响高16位的数据。 寄存器EBP、ESP、BP和SP称为指针寄存器(Pointer Register),主要用于存放堆栈内存储单元的偏移量,用它们可实现多种存储器操作数的寻址方式(在第3章有详细介绍),为以不同的地址形式访问存储单元提供方便。 指针寄存器不可分割成8位寄存器。作为通用寄存器,也可存储算术逻辑运算的操作数和运算结果。 它们主要用于访问堆栈内的存储单元,并且规定: BP为基指针(Base Pointer)寄存器,用它可直接存取堆栈中的数据; SP为堆栈指针(Stack Pointer)寄存器,用它只可访问栈顶。 4、段寄存器 段寄存器是根据内存分段的管理模式而设置的。内存单元的物理地址由段寄存器的值和一个偏移量组合而成的,这样可用两个较少位数的值组合成一个可访问较大物理空间的内存地址。 CPU内部的段寄存器: CS——代码段寄存器(Code Segment Register),其值为代码段的段值; DS——数据段寄存器(Data Segment Register),其值为数据段的段值; ES——附加段寄存器(Extra Segment Register),其值为附加数据段的段值; SS——堆栈段寄存器(Stack Segment Register),其值为堆栈段的段值; FS——附加段寄存器(Extra Segment Register),其值为附加数据段的段值; GS——附加段寄存器(Extra Segment Register),其值为附加数据段的段值。 在16位CPU系统中,它只有4个段寄存器,所以,程序在任何时刻至多有4个正在使用的段可直接访问;在32位微机系统中,它有6个段寄存器,所以,在此环境下开发的程序最多可同时访问6个段。 32位CPU有两个不同的工作方式:实方式和保护方式。在每种方式下,段寄存器的作用是不同的。有关规定简单描述如下: 实方式: 前4个段寄存器CS、DS、ES和SS与先前CPU中的所对应的段寄存器的含义完全一致,内存单元的逻辑地址仍为“段值:偏移量”的形式。为访问某内存段内的数据,必须使用该段寄存器和存储单元的偏移量。 保护方式: 在此方式下,情况要复杂得多,装入段寄存器的不再是段值,而是称为“选择子”(Selector)的某个值。段寄存器的具体作用在此不作进一步介绍了,有兴趣的读者可参阅其它科技资料。 5、指令指针寄存器 32位CPU把指令指针扩展到32位,并记作EIP,EIP的低16位与先前CPU中的IP作用相同。 指令指针EIP、IP(Instruction Pointer)是存放下次将要执行的指令在代码段的偏移量。在具有预取指令功能的系统中,下次要执行的指令通常已被预取到指令队列中,除非发生转移情况。所以,在理解它们的功能时,不考虑存在指令队列的情况。 在实方式下,由于每个段的最大范围为64K,所以,EIP中的高16位肯定都为0,此时,相当于只用其低16位的IP来反映程序中指令的执行次序。 |
|
[分享]汇编系列教程笔记—5月14日更新[邀请码已发]
通用寄存器的作用 通用寄存器可用于传送和暂存数据,也可参与算术逻辑运算,并保存运算结果。除此之外,它们还各自具有一些特殊功能。汇编语言程序员必须熟悉每个寄存器的一般用途和特殊用途,只有这样,才能在程序中做到正确、合理地使用它们。 表2.1 通用寄存器的主要用途 |
|
[分享]汇编系列教程笔记—5月14日更新[邀请码已发]
CPU资源和存储器 计算机的硬件资源是用汇编语言编程所必须要了解的重要内容,因为汇编语言允许、也需要程序员直接使用这些硬件资源,只有这样才能编写出高效的目标代码。 在汇编语言中,需要访问的硬件资源主要有:CPU内部资源、存储器和I/O端口。本章将着重讲解CPU内部寄存器的命名、功能及其常见的用途,还要介绍存储器的分段管理模式、存储单元地址的表示法以及其物理地址的形成方式。 寄存器组 寄存器是CPU内部重要的数据存储资源,是汇编程序员能直接使用的硬件资源之一。由于寄存器的存取速度比内存快,所以,在用汇编语言编写程序时,要尽可能充分利用寄存器的存储功能。 寄存器一般用来保存程序的中间结果,为随后的指令快速提供操作数,从而避免把中间结果存入内存,再读取内存的操作。在高级语言(如:C/C++语言)中,也有定义变量为寄存器类型的,这就是提高寄存器利用率的一种可行的方法。 另外,由于寄存器的个数和容量都有限,不可能把所有中间结果都存储在寄存器中,所以,要对寄存器进行适当的调度。根据指令的要求,如何安排适当的寄存器,避免操作数过多的传送操作是一项细致而又周密的工作。有关“寄存器的分配策略”在编译原理中会有详细的介绍。由于和此教程关系不大,感兴趣的坛友请去BAIDU一下。 由于16位/32位CPU是微机CPU的两个重要代表,所以,在此只介绍它们内部寄存器的名称及其主要功能。 寄存器组 1、 16位寄存器组 16位CPU所含有的寄存器有(见图2.1中16位寄存器部分): 4个数据寄存器(AX、BX、CX和DX) 2个变址和指针寄存器(SI和DI) 2个指针寄存器(SP和BP) 4个段寄存器(ES、CS、SS和DS) 1个指令指针寄存器(IP) 1个标志寄存器(Flags) 2、 32位寄存器组 32位CPU除了包含了先前CPU的所有寄存器,并把通用寄存器、指令指针和标志寄存器从16位扩充成32位之外,还增加了2个16位的段寄存器:FS和GS。 32位CPU所含有的寄存器有(见图中的寄存器): CPU寄存器组的示意图 4个数据寄存器(EAX、EBX、ECX和EDX) 2个变址和指针寄存器(ESI和EDI) 2个指针寄存器(ESP和EBP) 6个段寄存器(ES、CS、SS、DS、FS和GS) 1个指令指针寄存器(EIP) 1个标志寄存器(EFlags) |
|
[分享]汇编系列教程笔记—5月14日更新[邀请码已发]
对不起,没有完整的打包文件。 此教程是很多教程组合在一起的有删减、添加和修改并搜索了很多内容后修改而成,目的不是通过汇编编写一个应用软件,而是为以后的反汇编、逆向工程等打基础的。 在线教程还有个好处:可以在线交流、讨论、答疑 |
|
[分享]汇编系列教程笔记—5月14日更新[邀请码已发]
1.2.3 基本的数据类型 汇编语言所用到的基本数据类型为:字节、字、双字等,这些数据类型在以后的章节中都有相应的类型说明符。下面对它们进行最基本的描述。 1、字节 一个字节有8位二进制组成,其最高位是第7位,最低位是第0位,如右图所示。在表示有符号数时,最高位就是符号位。 通常情况下,存储器按字节编址,读写存储器的最小信息单位就是一个字节。 图1.2 字节数据类型示意图 2、字 由2个字节组成一个字,其最高位是第15位,最低位是第0位。高8位称为高字节,低8位称为低字节,如右图所示。 字节和字是汇编语言程序中最常用的两种数据类型,也是最容易出错的数据类型。 图1.3 字数据类型示意图 3、双字 用2个字(4个字节)来组成一个双字,其高16位称为高字,低16位称为低字,如右图所示。 双字有较大的数据表示范围,它通常是为了满足数据的表示范围而选用的数据类型,也可用于存储远指针。 字节、字和双字是汇编语言最常用的三种数据类型,下图表现出它们三者之间的组成关系。 图1.4 双字数据类型示意图 4、四字 由4个字(8个字节)组成一个四字类型,它总共有64个二进制位,当然,也就有更大的数据表示范围,但在汇编语言中很少使用该数据类型。 5、十字节 由10个字节组成一个十字节类型,它总共有80个二进制位。在汇编语言中很少使用该数据类型。 6、字符串 字符串是由若干个字节组成的,字节数不定,通常每个字节存储一个字符。该数据形式是汇编语言程序中经常使用的另一种数据形式。 |
|
[分享]汇编系列教程笔记—5月14日更新[邀请码已发]
1.2.2 非数值数据的表示 计算机除了具有进行数值计算能力之外,还具有进行非数值计算的能力。现在,后者的应用领域已远远超过了前者的应用领域,如:文字处理、图形图象处理、信息检索、日常的办公管理等。所以,对非数值信息的编码就显得越加重要。 1、ASCII码 ASCII码(American Standard Code for Information Interchange)是目前应用极其广泛的一种信息编码,许多计算机系统都是采用它为字符进行编码。它是一种7位二进制编码。 右表是ASCII码的具体编码方案。在该表中,对学习本课程有用的主要信息有: 字符'0'~'9'是连续编码的,其编码的低4位就是该字符在十进制中的数值; 小写字母的编码比大写字母的编码大,对应字母的编码之间相差20H。 当然,从ASCII码表中还可看出其它有用信息,还有扩展的ASCII码等知识,但这些内容对学习本课程的帮助不明显,故不再叙述。有兴趣的读者可BAIDU一下。 表1.3 ASCII码的编码方案 2、汉字编码 ASCII码是针对英文的字母、数字和其它特殊字符进行编码的,它不能用于对汉字的编码。要想用计算机来处理汉字,就必须先对汉字进行适当的编码。我国在1981年5月对6000多个常用的汉字制定了交换码的国家标准,即:GB2312-80。该标准规定了汉字交换用的基本汉字字符和一些图形字符,它们共计7445个,其中汉字有6763个。该标准给定每个字符的二进制编码,即国标码。 有关汉字编码的详细信息,有兴趣的读者可BAIDU一下,在此不再介绍。 |
|
[分享]汇编系列教程笔记—5月14日更新[邀请码已发]
1.2 数据的表示和类型 在用汇编语言进行程序设计时,程序员可以直接访问内存,对数据在存储器内的表示形式要有一个清晰的认识。下面,我们只简单介绍本课程所要用到的数据表示知识,为后面的学习作一点必要的准备。 有关“数据表示”的详细内容请参阅《计算机组成原理》中的相关章节。 1.2.1 数值数据的表示 (1)、二进制 在计算机内,数值是用二进制来表示的,每个二进制数按权相加就可得到其十进制数值。在书写二进制时,为了区别,在数据后面紧跟一个字母B。 二进制的一般表现形式为:bn-1…b1b0B,其代表数值:bn-12n-1+…+b121+b020。 数据的二进制表示形式简单、明了,但它书写起来比较长,所以,通常情况下,我们在程序中不直接用二进制来书写具体的数值,而改用八进制、十进制或十六进制。 (2)、八进制 八进制是一种二进制的变形,三位二进制可变为一位八进制,反之也然。八进制的表示元素是:0、1、…、7。在书写时,为了区别,在数据后面紧跟一个字母Q。如:1234Q、7654Q、54Q等都是八进制。 八进制数在程序中的使用频率不高。 (3)、十进制 十进制是我们最熟悉的一种数据表示形式,它的基本元素是:0、1、…、9。在书写时,为了区别,在数据后面紧跟一个字母D。在程序中经常用十进制来表示数据。 (4)、十六进制 十六进制是另一种二进制的变形,四位二进制可变为一位十六进制,反之也然。十六进制的基本元素是:0、1、…、9、A、B、…、F(字母小写也可以),其中:字母A、B、…、F依次代表10、11、…、15。 在书写时,为了区别,在数据后面紧跟一个字母H。当十六进制数的第一个字符是字母时,在第一个字符之前必须添加一个‘0’。如:100H、56EFH、0FFH、0ABCDH等都是十六进制数。 十六进制在程序中的使用频率很高。 (5)、数值进制的总结和相互转换 表1.1 各种进制及其字符表示 (6)、数的补码表示法 在计算机内,为了表示正负数,并便于进行各种算术运算,对有符号数采用二进制的补码表示形式。 补码的最高位用来表示正负数:0—正数,1—负数。 正数的补码是其自身的二进制形式,负数的补码是把其正数的二进制编码变“反”,再加1而得。 (7)、二进制数的符号扩展 在汇编语言中,我们经常要对字/字节的数据进行操作。当把“字节”转换成“字”,或“字”转换成“双字”时,就需要进行符号扩展。符号扩展的具体操作就是把已知信息的最高位扩展到所有更高位。 例1.1 把8位补码01011010、10101100分别扩展成16位补码。 解:根据符号扩展的含义,“字节→字”的具体扩展结果如下: 例1.2 把16位补码0101101111001010、1010111101011011别扩展成32位补码。 解:根据符号扩展的含义,“字→双字”的具体扩展结果如下: (8)、n位二进制的表示范围 n位二进制所能表示的无符号整数的范围:0≤x≤2n-1。 n位二进制所能表示的有符号整数(补码表示)的范围:-2n-1≤x≤2n-1-1。 在汇编语言中,常用到n为8和16时的数值范围: n=8时,无符号整数的范围:0~255,有符号整数的范围:-128~127; n=16时,无符号整数的范围:0~65535,有符号整数的范围:-32768~32767。 (9)、BCD码 通常,我们习惯用十进制表示的数据,但计算机是用二进制来表示数数据的,这就需要进行数值进制之间的转换。我们把每位十进制数转换二进制的编码,简称为BCD码(Binary Coded Decimal)。 BCD码是用4位二进制编码来表示1位十进制数。这种编码方法有多种,但常用的编码是8421BCD编码,如表1.2所示。这种BCD编码实际上就是0~9的“等值”二进制数。 表1.2 8421BCD编码列表 用BCD码进行进制的转换时,是要求在二种进制的表现形式上快速转换,而不是要求在“数值相等”的含义快速转换。 例1.3 求十进制数2000的BCD编码和其二进制数。 解:2000的BCD编码是把每位上的数2、0、0、0分别转换为其对应的BCD编码:0010、0000、0000和0000,把它们合在一起就是2000的BCD编码:0010 0000 0000 0000。 十进制数2000的二进制数是:11111010000,它们在数值上是相等的。 注意:十六进制、和二进制必须掌 握,否则请不要继续看下面的帖 |
操作理由
RANk
{{ user_info.golds == '' ? 0 : user_info.golds }}
雪币
{{ experience }}
课程经验
{{ score }}
学习收益
{{study_duration_fmt}}
学习时长
基本信息
荣誉称号:
{{ honorary_title }}
能力排名:
No.{{ rank_num }}
等 级:
LV{{ rank_lv-100 }}
活跃值:
在线值:
浏览人数:{{ visits }}
最近活跃:{{ last_active_time }}
注册时间:{{ user_info.create_date_jsonfmt }}
勋章
兑换勋章
证书
证书查询 >
能力值