作者 |
[美] 基普·R.欧文(Kip R.Irvine) |
丛书名 |
经典原版书库 |
出版社 |
机械工业出版社 |
ISBN |
9787111672111 |
简要 |
简介 |
内容简介书籍计算机书籍 本课程通过自顶向下的程序设计演示和解释,教授学生如何在机器层面上编写和调试程序,并将有效的设计技巧应用于多种程序设计课程。这种方法简化并消除了学生在学习更高级的计算机体系结构和操作系统课程之前需要掌握的概念。 |
目录 |
第1章 基本概念 1 1.1 欢迎来到汇编语言的世界 1 1.1.1 读者可能会问的问题 2 1.1.2 汇编语言的应用 5 1.1.3 本节回顾 6 1.2 虚拟机概念 7 1.2.1 本节回顾 9 1.3 数据表示 10 1.3.1 二进制整数 10 1.3.2 二进制加法 12 1.3.3 整数存储大小 13 1.3.4 十六进制整数 14 1.3.5 十六进制加法 16 1.3.6 有符号的二进制整数 16 1.3.7 二进制减法 19 1.3.8 字符存储 20 1.3.9 二进制编码的十进制(BCD)数 22 1.3.10 本节回顾 22 1.4 布尔表达式 23 1.4.1 布尔函数的真值表 26 1.4.2 本节回顾 27 1.5 本章小结 27 1.6 关键术语 28 1.7 复习题和练习 29 1.7.1 简答题 29 1.7.2 算法题 31 第2章 x86处理器架构 33 2.1 一般概念 34 2.1.1 基本微机设计 34 2.1.2 指令执行周期 35 2.1.3 读取内存 36 2.1.4 加载并执行程序 37 2.1.5 本节回顾 38 2.2 32位x86处理器 39 2.2.1 操作模式 39 2.2.2 基本执行环境 39 2.2.3 x86内存管理 43 2.2.4 本节回顾 44 2.3 64位x86-64处理器 44 2.3.1 64位操作模式 45 2.3.2 基本的64位执行环境 45 2.3.3 本节回顾 46 2.4 典型x86计算机的组件 47 2.4.1 母板 47 2.4.2 内存 49 2.4.3 本节回顾 49 2.5 输入–输出系统 50 2.5.1 I/O访问层次 50 2.5.2 本节回顾 52 2.6 本章小结 53 2.7 关键术语 54 2.8 复习题 55 第3章 汇编语言基础 57 3.1 基本语言元素 58 3.1.1 第一个汇编语言程序 58 3.1.2 整数字面量 59 3.1.3 常整数表达式 60 3.1.4 实数字面量 61 3.1.5 字符字面量 61 3.1.6 字符串字面量 62 3.1.7 保留字 62 3.1.8 标识符 62 3.1.9 伪指令 63 3.1.10 指令 63 3.1.11 本节回顾 67 3.2 示例:整数加减法 67 3.2.1 AddTwo程序 67 3.2.2 运行和调试AddTwo程序 69 3.2.3 程序模板 73 3.2.4 本节回顾 74 3.3 汇编、链接和运行程序 75 3.3.1 汇编–链接–执行周期 75 3.3.2 列表文件 76 3.3.3 本节回顾 78 3.4 定义数据 78 3.4.1 内部数据类型 78 3.4.2 数据定义语句 79 3.4.3 向AddTwo程序添加一个变量 80 3.4.4 定义BYTE和SBYTE数据 81 3.4.5 定义WORD和SWORD数据 83 3.4.6 定义DWORD和SDWORD数据 84 3.4.7 定义QWORD数据 84 3.4.8 定义压缩的BCD(TBYTE)数据 85 3.4.9 定义浮点类型 85 3.4.10 变量相加程序 86 3.4.11 小端序 87 3.4.12 声明未初始化数据 88 3.4.13 本节回顾 88 3.5 符号常量 90 3.5.1 等号伪指令 90 3.5.2 计算数组和字符串的大小 91 3.5.3 EQU伪指令 92 3.5.4 TEXTEQU伪指令 93 3.5.5 本节回顾 94 3.6 64位编程介绍 95 3.7 本章小结 96 3.8 关键术语 98 3.8.1 术语 98 3.8.2 指令、操作符及伪指令 98 3.9 复习题和练习 99 3.9.1 简答题 99 3.9.2 算法题 100 3.10 编程练习 100 第4章 数据传送、寻址及算术运算 102 4.1 数据传送指令 103 4.1.1 引言 103 4.1.2 操作数类型 103 4.1.3 直接内存操作数 103 4.1.4 MOV指令 105 4.1.5 整数的零/符号扩展 106 4.1.6 LAHF和SAHF指令 108 4.1.7 XCHG指令 109 4.1.8 直接–偏移量操作数 109 4.1.9 传送数据的示例 110 4.1.10 本节回顾 113 4.2 加法和减法 114 4.2.1 INC和DEC指令 114 4.2.2 ADD指令 114 4.2.3 SUB指令 115 4.2.4 NEG指令 115 4.2.5 实现算术表达式 115 4.2.6 加减法影响的标志 116 4.2.7 示例程序(AddSubTest) 120 4.2.8 本节回顾 121 4.3 与数据相关的操作符和伪指令 122 4.3.1 OFFSET操作符 122 4.3.2 ALIGN伪指令 123 4.3.3 PTR操作符 124 4.3.4 TYPE操作符 125 4.3.5 LENGTHOF操作符 125 4.3.6 SIZEOF操作符 126 4.3.7 LABEL伪指令 126 4.3.8 本节回顾 127 4.4 间接寻址 127 4.4.1 间接操作数 127 4.4.2 数组 128 4.4.3 变址操作数 129 4.4.4 指针 131 4.4.5 本节回顾 132 4.5 JMP和LOOP指令 134 4.5.1 JMP指令 134 4.5.2 LOOP指令 135 4.5.3 在Visual Studio调试器中显示数组 136 4.5.4 整数数组求和 137 4.5.5 复制字符串 138 4.5.6 本节回顾 139 4.6 64位编程 140 4.6.1 MOV指令 140 4.6.2 64位版本的SumArray程序 141 4.6.3 加法和减法 142 4.6.4 本节回顾 143 4.7 本章小结 144 4.8 关键术语 145 4.8.1 术语 145 4.8.2 指令、操作符及伪指令 145 4.9 复习题和练习 146 4.9.1 简答题 146 4.9.2 算法题 148 4.10 编程练习 149 第5章 过程 151 5.1 堆栈操作 152 5.1.1 运行时堆栈(32位模式) 152 5.1.2 PUSH和POP指令 154 5.1.3 本节回顾 157 5.2 定义和使用过程 158 5.2.1 PROC伪指令 158 5.2.2 CALL和RET指令 160 5.2.3 嵌套过程调用 161 5.2.4 向过程传递寄存器参数 163 5.2.5 示例:整数数组求和 163 5.2.6 保存和恢复寄存器 165 5.2.7 本节回顾 166 5.3 链接到外部库 167 5.3.1 背景信息 167 5.3.2 本节回顾 168 5.4 Irvine32库 169 5.4.1 创建库的动机 169 5.4.2 Win32控制台窗口 171 5.4.3 各个过程的描述 172 5.4.4 库测试程序 184 5.4.5 本节回顾 192 5.5 64位汇编编程 193 5.5.1 Irvine64库 193 5.5.2 调用64位子例程 194 5.5.3 x64调用规约 195 5.5.4 调用过程的示例程序 195 5.5.5 本节回顾 197 5.6 本章小结 198 5.7 关键术语 199 5.7.1 术语 199 5.7.2 指令、操作符及伪指令 199 5.8 复习题和练习 199 5.8.1 简答题 199 5.8.2 算法题 202 5.9 编程练习 203 第6章 条件处理 205 6.1 布尔和比较指令 206 6.1.1 CPU状态标志 206 6.1.2 AND指令 207 6.1.3 OR指令 208 6.1.4 位映射集 209 6.1.5 XOR指令 211 6.1.6 NOT指令 212 6.1.7 TEST指令 212 6.1.8 CMP指令 213 6.1.9 置位和清零各个CPU标志 214 6.1.10 64位模式下的布尔指令 214 6.1.11 本节回顾 215 6.2 条件跳转 216 6.2.1 条件结构 216 6.2.2 Jcond指令 217 6.2.3 条件跳转指令的类型 217 6.2.4 条件跳转应用 221 6.2.5 本节回顾 225 6.3 条件循环指令 226 6.3.1 LOOPZ和LOOPE指令 226 6.3.2 LOOPNZ和LOOPNE指令 226 6.3.3 本节回顾 227 6.4 条件结构 228 6.4.1 块结构的IF语句 228 6.4.2 复合表达式 233 6.4.3 WHILE循环 234 6.4.4 表驱动的选择 237 6.4.5 本节回顾 239 6.5 应用:有限状态机 240 6.5.1 验证输入字符串 240 6.5.2 验证有符号整数 241 6.5.3 本节回顾 245 6.6 条件控制流伪指令(可选主题) 246 6.6.1 创建IF语句 246 6.6.2 有符号数和无符号数的比较 249 6.6.3 复合表达式 250 6.6.4 用.REPEAT和.WHILE创建循环 253 6.7 本章小结 254 6.8 关键术语 255 6.8.1 术语 255 6.8.2 指令、操作符及伪指令 255 6.9 复习题和练习 256 6.9.1 简答题 256 6.9.2 算法题 258 6.10 编程练习 259 6.10.1 对代码测试的建议 259 6.10.2 习题描述 260 第7章 整数算术运算 263 7.1 移位和循环移位指令 264 7.1.1 逻辑移位和算术移位 264 7.1.2 SHL指令 265 7.1.3 SHR指令 266 7.1.4 SAL和SAR指令 267 7.1.5 ROL指令 268 7.1.6 ROR指令 269 7.1.7 RCL和RCR指令 269 7.1.8 有符号数溢出 270 7.1.9 SHLD/SHRD指令 270 7.1.10 本节回顾 270 7.2 移位和循环移位的应用 274 7.2.1 多个双字的移位 274 7.2.2 通过移位做乘法 275 7.2.3 显示二进制位 276 7.2.4 提取文件日期字段 276 7.2.5 本节回顾 277 7.3 乘法和除法指令 279 7.3.1 无符号整数乘法(MUL) 279 7.3.2 有符号整数乘法(IMUL) 281 7.3.3 测量程序执行时间 284 7.3.4 无符号整数除法(DIV) 286 7.3.5 有符号整数除法(IDIV) 288 7.3.6 实现算术表达式 292 7.3.7 本节回顾 294 7.4 扩展的加减法 295 7.4.1 ADC指令 295 7.4.2 扩展加法的示例 296 7.4.3 SBB指令 298 7.4.4 本节回顾 299 7.5 ASCII和非压缩十进制算术运算 299 7.5.1 AAA指令 300 7.5.2 AAS指令 302 7.5.3 AAM指令 303 7.5.4 AAD指令 303 7.5.5 本节回顾 303 7.6 压缩十进制算术运算 304 7.6.1 DAA指令 305 7.6.2 DAS指令 306 7.6.3 本节回顾 306 7.7 本章小结 307 7.8 关键术语 308 7.8.1 术语 308 7.8.2 指令、操作符及伪指令 308 7.9 复习题和练习 308 7.9.1 简答题 308 7.9.2 算法题 310 7.10 编程练习 311 第8章 高级过程 314 8.1 引言 315 8.2 堆栈帧 315 8.2.1 堆栈参数 315 8.2.2 寄存器参数的缺点 316 8.2.3 访问堆栈参数 318 8.2.4 32位调用规约 321 8.2.5 局部变量 323 8.2.6 引用参数 324 8.2.7 LEA指令 326 8.2.8 ENTER和LEAVE指令 326 8.2.9 LOCAL伪指令 328 8.2.10 Microsoft x64调用规约 329 8.2.11 本节回顾 330 8.3 递归 331 8.3.1 递归求和 331 8.3.2 计算阶乘 333 8.3.3 本节回顾 339 8.4 INVOKE、ADDR、PROC及PROTO 340 8.4.1 INVOKE伪指令 340 8.4.2 ADDR操作符 341 8.4.3 PROC伪指令 342 8.4.4 PROTO伪指令 345 8.4.5 参数分类 348 8.4.6 示例:交换两个整数 349 8.4.7 调试提示 350 8.4.8 WriteStackFrame过程 351 8.4.9 本节回顾 352 8.5 创建多模块程序 352 8.5.1 隐藏和导出过程名 352 8.5.2 调用外部过程 353 8.5.3 跨模块使用变量和符号 354 8.5.4 示例:ArraySum程序 355 8.5.5 用Extern创建模块 356 8.5.6 用INVOKE和PROTO创建模块 359 8.5.7 本节回顾 362 8.6 参数的高级用法(可选主题) 363 8.6.1 受USES操作符影响的堆栈 363 8.6.2 向堆栈传递8位和16位参数 364 8.6.3 传递64位参数 366 8.6.4 非双字局部变量 366 8.7 Java字节码(可选主题) 369 8.7.1 Java虚拟机 369 8.7.2 指令集 370 8.7.3 Java反汇编示例 371 8.7.4 示例:条件分支 374 8.8 本章小结 376 8.9 关键术语 377 8.9.1 术语 377 8.9.2 指令、操作符及伪指令 377 8.10 复习题和练习 377 8.10.1 简答题 377 8.10.2 算法题 378 8.11 编程练习 378 第9章 字符串和数组 381 9.1 引言 381 9.2 字符串原语指令 382 9.2.1 MOVSB、MOVSW及MOVSD 383 9.2.2 CMPSB、CMPSW及CMPSD 384 9.2.3 SCASB、SCASW及SCASD 385 9.2.4 STOSB、STOSW及STOSD 385 9.2.5 LODSB、LODSW及LODSD 385 9.2.6 本节回顾 386 9.3 若干字符串过程 387 9.3.1 Str_compare过程 388 9.3.2 Str_length过程 389 9.3.3 Str_copy过程 389 9.3.4 Str_trim过程 390 9.3.5 Str_ucase过程 393 9.3.6 字符串库演示程序 393 9.3.7 Irvine64库中的字符串过程 395 9.3.8 本节回顾 398 9.4 二维数组 399 9.4.1 行列顺序 399 9.4.2 基址–变址操作数 399 9.4.3 基址–变址–位移操作数 401 9.4.4 64位模式下的基址–变址操作数 402 9.4.5 本节回顾 403 9.5 整数数组的查找和排序 404 9.5.1 冒泡排序 404 9.5.2 对半查找 406 9.5.3 本节回顾 412 9.6 Java字节码:字符串处理(可选主题) 413 9.7 本章小结 414 9.8 关键术语和指令 415 9.9 复习题和练习 415 9.9.1 简答题 415 9.9.2 算法题 416 9.10 编程练习 416 第10章 结构和宏 421 10.1 结构 421 10.1.1 定义结构 422 10.1.2 声明结构对象 424 10.1.3 引用结构对象 425 10.1.4 示例:显示系统时间 428 10.1.5 结构包含结构 430 10.1.6 示例:醉汉行走 430 10.1.7 声明和使用联合 434 10.1.8 本节回顾 436 10.2 宏 437 10.2.1 概述 437 10.2.2 定义宏 438 10.2.3 调用宏 439 10.2.4 其他宏特性 440 10.2.5 使用本书的宏库(仅32位模式) 444 10.2.6 示例程序:封装器 451 10.2.7 本节回顾 452 10.3 条件汇编伪指令 453 10.3.1 检查缺失的参数 454 10.3.2 默认的参数初始化值 455 10.3.3 布尔表达式 456 10.3.4 IF、ELSE及ENDIF伪指令 456 10.3.5 IFIDN和IFIDNI伪指令 457 10.3.6 示例:矩阵行求和 458 10.3.7 特殊操作符 461 10.3.8 宏函数 464 10.3.9 本节回顾 466 10.4 定义重复语句块 467 10.4.1 WHILE伪指令 467 10.4.2 REPEAT伪指令 468 10.4.3 FOR伪指令 468 10.4.4 FORC伪指令 469 10.4.5 示例:链表 470 10.4.6 本节回顾 471 10.5 本章小结 473 10.6 关键术语 474 10.6.1 术语 474 10.6.2 操作符及伪指令 474 10.7 复习题和练习 475 10.7.1 简答题 475 10.7.2 算法题 475 10.8 编程练习 477 第11章 MS-Windows编程 480 11.1 Win32控制台编程 480 11.1.1 背景信息 481 11.1.2 Win32控制台函数 485 11.1.3 显示消息框 487 11.1.4 控制台输入 490 11.1.5 控制台输出 496 11.1.6 读写文件 498 11.1.7 Irvine32库中的文件I/O 502 11.1.8 测试文件I/O过程 504 11.1.9 控制台窗口操作 507 11.1.10 控制光标 510 11.1.11 控制文本颜色 511 11.1.12 时间和日期函数 513 11.1.13 使用64位Windows API 517 11.1.14 本节回顾 518 11.2 编写图形化的Windows应用程序 519 11.2.1 必要的结构 519 11.2.2 MessageBox函数 521 11.2.3 WinMain过程 521 11.2.4 WinProc过程 522 11.2.5 ErrorHandler过程 523 11.2.6 程序清单 523 11.2.7 本节回顾 527 11.3 动态内存分配 528 11.3.1 HeapTest程序 531 11.3.2 本节回顾 535 11.4 32位x86存储管理 535 11.4.1 线性地址 536 11.4.2 页转换 539 11.4.3 本节回顾 541 11.5 本章小结 541 11.6 关键术语 543 11.7 复习题和练习 543 11.7.1 简答题 543 11.7.2 算法题 544 11.8 编程练习 544 第12章 浮点数处理和指令编码 547 12.1 浮点数的二进制表示 547 12.1.1 IEEE二进制浮点数表示 548 12.1.2 阶数 550 12.1.3 规格化二进制浮点数 550 12.1.4 创建IEEE表示 550 12.1.5 将十进制分数转换为二进制实数 552 12.1.6 本节回顾 554 12.2 浮点单元 555 12.2.1 FPU寄存器栈 555 12.2.2 舍入 558 12.2.3 浮点异常 560 12.2.4 浮点指令集 560 12.2.5 算术运算指令 563 12.2.6 比较浮点数值 567 12.2.7 读写浮点数值 570 12.2.8 异常同步 571 12.2.9 代码示例 572 12.2.10 混合模式算术运算 574 12.2.11 屏蔽和非屏蔽异常 575 12.2.12 本节回顾 576 12.3 x86指令编码 577 12.3.1 指令格式 577 12.3.2 单字节指令 578 12.3.3 将立即数送入寄存器 579 12.3.4 寄存器模式指令 580 12.3.5 处理器操作数大小前缀 581 12.3.6 内存模式指令 582 12.3.7 本节回顾 585 12.4 本章小结 585 12.5 关键术语 587 12.6 复习题和练习 587 12.6.1 简答题 587 12.6.2 算法题 588 12.7 编程练习 589 第13章 高级语言接口 593 13.1 引言 593 13.1.1 通用规约 593 13.1.2 .MODEL伪指令 595 13.1.3 检查编译器生成的代码 597 13.1.4 本节回顾 602 13.2 内联汇编代码 602 13.2.1 Visual C++中的__asm伪指令 602 13.2.2 文件加密示例 605 13.2.3 本节回顾 608 13.3 将32位汇编语言代码链接到C/C++ 609 13.3.1 IndexOf示例 610 13.3.2 调用C和C++函数 613 13.3.3 乘法表的示例 615 13.3.4 本节回顾 618 13.4 本章小结 619 13.5 关键术语 620 13.6 复习题 620 13.7 编程练习 620 第14章 16位MS-DOS编程 622 14.1 MS-DOS和IBM-PC 622 14.1.1 内存组织 623 14.1.2 重定向输入–输出 624 14.1.3 软件中断 625 14.1.4 INT指令 626 14.1.5 16位程序的代码编写 627 14.1.6 本节回顾 628 14.2 MS-DOS功能调用(INT 21h) 628 14.2.1 若干输出功能 630 14.2.2 Hello World程序示例 632 14.2.3 若干输入功能 633 14.2.4 日期/时间功能 637 14.2.5 本节回顾 641 14.3 标准的MS-DOS文件I/O服务 641 14.3.1 创建或打开文件(716Ch) 643 14.3.2 关闭文件句柄(3Eh) 644 14.3.3 移动文件指针(42h) 644 14.3.4 获取文件创建日期和时间 645 14.3.5 若干库过程 646 14.3.6 示例:读取和复制一个文本文件 647 14.3.7 读取MS-DOS命令的尾部 649 14.3.8 示例: 创建二进制文件 651 14.3.9 本节回顾 654 14.4 本章小结 655 14.5 关键术语 657 14.6 编程练习 657 第15章 磁盘基础知识 659 15.1 磁盘存储系统 659 15.1.1 磁道、柱面及扇区 660 15.1.2 磁盘分区(卷) 662 15.1.3 本节回顾 663 15.2 文件系统 663 15.2.1 FAT12 664 15.2.2 FAT16 664 15.2.3 FAT32 665 15.2.4 NTFS 665 15.2.5 主磁盘区 666 15.2.6 本节回顾 667 15.3 磁盘 667 15.3.1 MS-DOS结构 668 15.3.2 MS-Windows中的长文件名 671 15.3.3 文件分配表(FAT) 673 15.3.4 本节回顾 673 15.4 读写磁盘扇区 674 15.4.1 扇区显示程序 675 15.4.2 本节回顾 679 15.5 系统级文件功能 680 15.5.1 获取磁盘空闲空间(7303h) 680 15.5.2 创建子(39h) 683 15.5.3 删除子(3Ah) 684 15.5.4 设置当前(3Bh) 684 15.5.5 获取当前(47h) 684 15.5.6 获取和设置文件属性(7143h) 685 15.5.7 本节回顾 685 15.6 本章小结 685 15.7 关键术语 687 15.8 编程练习 687 第16章 BIOS级编程 689 16.1 引言 689 16.1.1 BIOS数据区 690 16.2 用INT 16h进行键盘输入 691 16.2.1 键盘如何工作 691 16.2.2 INT 16h功能 692 16.2.3 本节回顾 696 16.3 用INT 10h进行视频编程 697 16.3.1 基本背景 697 16.3.2 控制颜色 699 16.3.3 INT 10h视频功能 701 16.3.4 库过程示例 713 16.3.5 本节回顾 713 16.4 使用INT 10h绘制图形 714 16.4.1 INT 10h与像素有关的功能 715 16.4.2 程序DrawLine 716 16.4.3 笛卡儿坐标程序 718 16.4.4 将笛卡儿坐标转换为屏幕坐标 720 16.4.5 本节回顾 721 16.5 内存映射的图形 722 16.5.1 模式13h:320× 200,256色 722 16.5.2 内存映射图形程序 724 16.5.3 本节回顾 727 16.6 鼠标编程 727 16.6.1 鼠标INT 33h功能 727 16.6.2 鼠标跟踪程序 732 16.6.3 本节回顾 737 16.7 本章小结 738 16.8 编程练习 739 附录A MASM参考知识 741 附录B x86指令集 763 附录C BIOS和MS-DOS中断 797 附录D “本节回顾”的问题答案(第14~16章) 807 词汇表 816 Contents 1 Basic Concepts 1 1.1 Welcome to Assembly Language 1 1.1.1 Questions You Might Ask 2 1.1.2 Assembly Language Applications 5 1.1.3 Section Review 6 1.2 Virtual Machine Concept 7 1.2.1 Section Review 9 1.3 Data Representation 10 1.3.1 Binary Integers 10 1.3.2 Binary Addition 12 1.3.3 Integer Storage Sizes 13 1.3.4 Hexadecimal Integers 14 1.3.5 Hexadecimal Addition 16 1.3.6 Signed Binary Integers 16 1.3.7 Binary Subtraction 19 1.3.8 Character Storage 20 1.3.9 Binary-Coded Decimal (BCD) Numbers 22 1.3.10 Section Review 22 1.4 Boolean Expressions 23 1.4.1 Truth Tables for Boolean Functions 26 1.4.2 Section Review 27 1.5 Chapter Summary 27 1.6 Key Terms 28 1.7 Review Questions and Exercises 29 1.7.1 Short Answer 29 1.7.2 Algorithm Workbench 31 2 x86 Processor Architecture 33 2.1 General Concepts 34 2.1.1 Basic Microcomputer Design 34 2.1.2 Instruction Execution Cycle 35 2.1.3 Reading from Memory 36 2.1.4 Loading and Executing a Program 37 2.1.5 Section Review 38 2.2 32-Bit x86 Processors 39 2.2.1 Modes of Operation 39 2.2.2 Basic Execution Environment 39 2.2.3 x86 Memory Management 43 2.2.4 Section Review 44 2.3 64-Bit x86-64 Processors 44 2.3.1 64-Bit Operation Modes 45 2.3.2 Basic 64-Bit Execution Environment 45 2.3.3 Section Review 46 2.4 Components of a Typical x86 Computer 47 2.4.1 Motherboard 47 2.4.2 Memory 49 2.4.3 Section Review 49 2.5 Input-Output System 50 2.5.1 Levels of I/O Access 50 2.5.2 Section Review 52 2.6 Chapter Summary 53 2.7 Key Terms 54 2.8 Review Questions 55 3 Assembly Language Fundamentals 57 3.1 Basic Language Elements 58 3.1.1 First Assembly Language Program 58 3.1.2 Integer Literals 59 3.1.3 Constant Integer Expressions 60 3.1.4 Real Number Literals 61 3.1.5 Character Literals 61 3.1.6 String Literals 62 3.1.7 Reserved Words 62 3.1.8 Identifiers 62 3.1.9 Directives 63 3.1.10 Instructions 63 3.1.11 Section Review 67 3.2 Example: Adding and Subtracting Integers 67 3.2.1 The AddTwo Program 67 3.2.2 Running and Debugging the AddTwo Program 69 3.2.3 Program Template 73 3.2.4 Section Review 74 3.3 Assembling, Linking, and Running Programs 75 3.3.1 The Assemble-Link-Execute Cycle 75 3.3.2 Listing File 76 3.3.3 Section Review 78 3.4 Defining Data 78 3.4.1 Intrinsic Data Types 78 3.4.2 Data Definition Statement 79 3.4.3 Adding a Variable to the AddTwo Program 80 3.4.4 Defining BYTE and SBYTE Data 81 3.4.5 Defining WORD and SWORD Data 83 3.4.6 Defining DWORD and SDWORD Data 84 3.4.7 Defining QWORD Data 84 3.4.8 Defining Packed BCD (TBYTE) Data 85 3.4.9 Defining Floating-Point Types 85 3.4.10 A Program That Adds Variables 86 3.4.11 Little-Endian Order 87 3.4.12 Declaring Uninitialized Data 88 3.4.13 Section Review 88 3.5 Symbolic Constants 90 3.5.1 Equal-Sign Directive 90 3.5.2 Calculating the Sizes of Arrays and Strings 91 3.5.3 EQU Directive 92 3.5.4 TEXTEQU Directive 93 3.5.5 Section Review 94 3.6 Introducing 64-Bit Programming 95 3.7 Chapter Summary 96 3.8 Key Terms 98 3.8.1 Terms 98 3.8.2 Instructions, Operators, and Directives 98 3.9 Review Questions and Exercises 99 3.9.1 Short Answer 99 3.9.2 Algorithm Workbench 100 3.10 Programming Exercises 100 4 Data Transfers, Addressing, and Arithmetic 102 4.1 Data Transfer Instructions 103 4.1.1 Introduction 103 4.1.2 Operand Types 103 4.1.3 Direct Memory Operands 103 4.1.4 MOV Instruction 105 4.1.5 Zero/Sign Extension of Integers 106 4.1.6 LAHF and SAHF Instructions 108 4.1.7 XCHG Instruction 109 4.1.8 Direct-Offset Operands 109 4.1.9 Examples of Moving Data 110 4.1.10 Section Review 113 4.2 Addition and Subtraction 114 4.2.1 INC and DEC Instructions 114 4.2.2 ADD Instruction 114 4.2.3 SUB Instruction 115 4.2.4 NEG Instruction 115 4.2.5 Implementing Arithmetic Expressions 115 4.2.6 Flags Affected by Addition and Subtraction 116 4.2.7 Example Program (AddSubTest) 120 4.2.8 Section Review 121 4.3 Data-Related Operators and Directives 122 4.3.1 OFFSET Operator 122 4.3.2 ALIGN Directive 123 4.3.3 PTR Operator 124 4.3.4 TYPE Operator 125 4.3.5 LENGTHOF Operator 125 4.3.6 SIZEOF Operator 126 4.3.7 LABEL Directive 126 4.3.8 Section Review 127 4.4 Indirect Addressing 127 4.4.1 Indirect Operands 127 4.4.2 Arrays 128 4.4.3 Indexed Operands 129 4.4.4 Pointers 131 4.4.5 Section Review 132 4.5 JMP and LOOP Instructions 134 4.5.1 JMP Instruction 134 4.5.2 LOOP Instruction 135 4.5.3 Displaying an Array in the Visual Studio Debugger 136 4.5.4 Summing an Integer Array 137 4.5.5 Copying a String 138 4.5.6 Section Review 139 4.6 64-Bit Programming 140 4.6.1 MOV Instruction 140 4.6.2 64-Bit Version of SumArray 141 4.6.3 Addition and Subtraction 142 4.6.4 Section Review 143 4.7 Chapter Summary 144 4.8 Key Terms 145 4.8.1 Terms 145 4.8.2 Instructions, Operators, and Directives 145 4.9 Review Questions and Exercises 146 4.9.1 Short Answer 146 4.9.2 Algorithm Workbench 148 4.10 Programming Exercises 149 5 Procedures 151 5.1 Stack Operations 152 5.1.1 Runtime Stack (32-Bit Mode) 152 5.1.2 PUSH and POP Instructions 154 5.1.3 Section Review 157 5.2 Defining and Using Procedures 158 5.2.1 PROC Directive 158 5.2.2 CALL and RET Instructions 160 5.2.3 Nested Procedure Calls 161 5.2.4 Passing Register Arguments to Procedures 163 5.2.5 Example: Summing an Integer Array 163 5.2.6 Saving and Restoring Registers 165 5.2.7 Section Review 166 5.3 Linking to an External Library 167 5.3.1 Background Information 167 5.3.2 Section Review 168 5.4 The Irvine32 Library 169 5.4.1 Motivation for Creating the Library 169 5.4.2 The Win32 Console Window 171 5.4.3 Individual Procedure Descriptions 172 5.4.4 Library Test Programs 184 5.4.5 Section Review 192 5.5 64-Bit Assembly Programming 193 5.5.1 The Irvine64 Library 193 5.5.2 Calling 64-Bit Subroutines 194 5.5.3 The x64 Calling Convention 195 5.5.4 Sample Program that Calls a Procedure 195 5.5.5 Section Review 197 5.6 Chapter Summary 198 5.7 Key Terms 199 5.7.1 Terms 199 5.7.2 Instructions, Operators, and Directives 199 5.8 Review Questions and Exercises 199 5.8.1 Short Answer 199 5.8.2 Algorithm Workbench 202 5.9 Programming Exercises 203 6 Conditional Processing 205 6.1 Boolean and Comparison Instructions 206 6.1.1 The CPU Status Flags 206 6.1.2 AND Instruction 207 6.1.3 OR Instruction 208 6.1.4 Bit-Mapped Sets 209 6.1.5 XOR Instruction 211 6.1.6 NOT Instruction 212 6.1.7 TEST Instruction 212 6.1.8 CMP Instruction 213 6.1.9 Setting and Clearing Individual CPU Flags 214 6.1.10 Boolean Instructions in 64-Bit Mode 214 6.1.11 Section Review 215 6.2 Conditional Jumps 216 6.2.1 Conditional Structures 216 6.2.2 Jcond Instruction 217 6.2.3 Types of Conditional Jump Instructions 217 6.2.4 Conditional Jump Applications 221 6.2.5 Section Review 225 6.3 Conditional Loop Instructions 226 6.3.1 LOOPZ and LOOPE Instructions 226 6.3.2 LOOPNZ and LOOPNE Instructions 226 6.3.3 Section Review 227 6.4 Conditional Structures 228 6.4.1 Block-Structured IF Statements 228 6.4.2 Compound Expressions 233 6.4.3 WHILE Loops 234 6.4.4 Table-Driven Selection 237 6.4.5 Section Review 239 6.5 Application: Finite-State Machines 240 6.5.1 Validating an Input String 240 6.5.2 Validating a Signed Integer 241 6.5.3 Section Review 245 6.6 Conditional Control Flow Directives (Optional topic) 246 6.6.1 Creating IF Statements 246 6.6.2 Signed and Unsigned Comparisons 249 6.6.3 Compound Expressions 250 6.6.4 Creating Loops with .REPEAT and .WHILE 253 6.7 Chapter Summary 254 6.8 Key Terms 255 6.8.1 Terms 255 6.8.2 Instructions, Operators, and Directives 255 6.9 Review Questions and Exercises 256 6.9.1 Short Answer 256 6.9.2 Algorithm Workbench 258 6.10 Programming Exercises 259 6.10.1 Suggestions for Testing Your Code 259 6.10.2 Exercise Descriptions 260 7 Integer Arithmetic 263 7.1 Shift and Rotate Instructions 264 7.1.1 Logical Shifts and Arithmetic Shifts 264 7.1.2 SHL Instruction 265 7.1.3 SHR Instruction 266 7.1.4 SAL and SAR Instructions 267 7.1.5 ROL Instruction 268 7.1.6 ROR Instruction 269 7.1.7 RCL and RCR Instructions 269 7.1.8 Signed Overflow 270 7.1.9 SHLD/SHRD Instructions 270 7.1.10 Section Review 272 7.2 Shift and Rotate Applications 274 7.2.1 Shifting Multiple Doublewords 274 7.2.2 Multiplication by Shifting Bits 275 7.2.3 Displaying Binary Bits 276 7.2.4 Extracting File Date Fields 276 7.2.5 Section Review 277 7.3 Multiplication and Division Instructions 279 7.3.1 Unsigned Integer Multiplication (MUL) 279 7.3.2 Signed Integer Multiplication (IMUL) 281 7.3.3 Measuring Program Execution Times 284 7.3.4 Unsigned Integer Division (DIV) 286 7.3.5 Signed Integer Division (IDIV) 288 7.3.6 Implementing Arithmetic Expressions 292 7.3.7 Section Review 294 7.4 Extended Addition and Subtraction 295 7.4.1 ADC Instruction 295 7.4.2 Extended Addition Example 296 7.4.3 SBB Instruction 298 7.4.4 Section Review 299 7.5 ASCII and Unpacked Decimal Arithmetic 299 7.5.1 AAA Instruction 300 7.5.2 AAS Instruction 302 7.5.3 AAM Instruction 303 7.5.4 AAD Instruction 303 7.5.5 Section Review 303 7.6 Packed Decimal Arithmetic 304 7.6.1 DAA Instruction 305 7.6.2 DAS Instruction 306 7.6.3 Section Review 306 7.7 Chapter Summary 307 7.8 Key Terms 308 7.8.1 Terms 308 7.8.2 Instructions, Operators, and Directives 308 7.9 Review Questions and Exercises 308 7.9.1 Short Answer 308 7.9.2 Algorithm Workbench 310 7.10 Programming Exercises 311 8 Advanced Procedures 314 8.1 Introduction 315 8.2 Stack Frames 315 8.2.1 Stack Parameters 315 8.2.2 Disadvantages of Register Parameters 316 8.2.3 Accessing Stack Parameters 318 8.2.4 32-Bit Calling Conventions 321 8.2.5 Local Variables 323 8.2.6 Reference Parameters 324 8.2.7 LEA Instruction 326 8.2.8 ENTER and LEAVE Instructions 326 8.2.9 LOCAL Directive 328 8.2.10 The Microsoft x64 Calling Convention 329 8.2.11 Section Review 330 8.3 Recursion 331 8.3.1 Recursively Calculating a Sum 331 8.3.2 Calculating a Factorial 333 8.3.3 Section Review 339 8.4 INVOKE, ADDR, PROC, and PROTO 340 8.4.1 INVOKE Directive 340 8.4.2 ADDR Operator 341 8.4.3 PROC Directive 342 8.4.4 PROTO Directive 345 8.4.5 Parameter Classifications 348 8.4.6 Example: Exchanging Two Integers 8.4.7 Debugging Tips 350 8.4.8 WriteStackFrame Procedure 351 8.4.9 Section Review 352 8.5 Creating Multimodule Programs 349 8.5.1 Hiding and Exporting Procedure Names 352 8.5.2 Calling External Procedures 353 8.5.3 Using Variables and Symbols across Module Boundaries 354 8.5.4 Example: ArraySum Program 355 8.5.5 Creating the Modules Using Extern 356 8.5.6 Creating the Modules Using INVOKE and PROTO 359 8.5.7 Section Review 362 8.6 Advanced Use of Parameters (Optional Topic) 363 8.6.1 Stack Affected by the USES Operator 363 8.6.2 Passing 8-Bit and 16-Bit Arguments on the Stack 364 8.6.3 Passing 64-Bit Arguments 366 8.6.4 Non-Doubleword Local Variables 366 8.7 Java Bytecodes (Optional Topic) 369 8.7.1 Java Virtual Machine 369 8.7.2 Instruction Set 370 8.7.3 Java Disassembly Examples 371 8.7.4 Example: Conditional Branch 374 8.8 Chapter Summary 376 8.9 Key Terms 377 8.9.1 Terms 377 8.9.2 Instructions, Operators, and Directives 377 8.10 Review Questions and Exercises 377 8.10.1 Short Answer 377 8.10.2 Algorithm Workbench 378 8.11 Programming Exercises 378 9 Strings and Arrays 381 9.1 Introduction 381 9.2 String Primitive Instructions 382 9.2.1 MOVSB, MOVSW, and MOVSD 383 9.2.2 CMPSB, CMPSW, and CMPSD 384 9.2.3 SCASB, SCASW, and SCASD 385 9.2.4 STOSB, STOSW, and STOSD 385 9.2.5 LODSB, LODSW, and LODSD 385 9.2.6 Section Review 386 9.3 Selected String Procedures 387 9.3.1 Str_compare Procedure 388 9.3.2 Str_length Procedure 389 9.3.3 Str_copy Procedure 389 9.3.4 Str_trim Procedure 390 9.3.5 Str_ucase Procedure 393 9.3.6 String Library Demo Program 393 9.3.7 String Procedures in the Irvine64 Library 395 9.3.8 Section Review 398 9.4 Two-Dimensional Arrays 399 9.4.1 Ordering of Rows and Columns 399 9.4.2 Base-Index Operands 399 9.4.3 Base-Index-Displacement Operands 401 9.4.4 Base-Index Operands in 64-Bit Mode 402 9.4.5 Section Review 403 9.5 Searching and Sorting Integer Arrays 404 9.5.1 Bubble Sort 404 9.5.2 Binary Search 406 9.5.3 Section Review 412 9.6 Java Bytecodes: String Processing (Optional Topic) 413 9.7 Chapter Summary 414 9.8 Key Terms and Instructions 415 9.9 Review Questions and Exercises 415 9.9.1 Short Answer 415 9.9.2 Algorithm Workbench 416 9.10 Programming Exercises 416 10 Structures and Macros 421 10.1 Structures 421 10.1.1 Defining Structures 422 10.1.2 Declaring Structure Objects 424 10.1.3 Referencing Structure Objects 425 10.1.4 Example: Displaying the System Time 428 10.1.5 Structures Containing Structures 430 10.1.6 Example: Drunkard’s Walk 430 10.1.7 Declaring and Using Unions 434 10.1.8 Section Review 436 10.2 Macros 437 10.2.1 Overview 437 10.2.2 Defining Macros 438 10.2.3 Invoking Macros 439 10.2.4 Additional Macro Features 440 10.2.5 Using Our Macro Library (32-Bit Mode Only) 444 10.2.6 Example Program: Wrappers 451 10.2.7 Section Review 452 10.3 Conditional-Assembly Directives 453 10.3.1 Checking for Missing Arguments 454 10.3.2 Default Argument Initializers 455 10.3.3 Boolean Expressions 456 10.3.4 IF, ELSE, and ENDIF Directives 456 10.3.5 The IFIDN and IFIDNI Directives 457 10.3.6 Example: Summing a Matrix Row 458 10.3.7 Special Operators 461 10.3.8 Macro Functions 464 10.3.9 Section Review 466 10.4 Defining Repeat Blocks 467 10.4.1 WHILE Directive 467 10.4.2 REPEAT Directive 468 10.4.3 FOR Directive 468 10.4.4 FORC Directive 469 10.4.5 Example: Linked List 470 10.4.6 Section Review 471 10.5 Chapter Summary 473 10.6 Key Terms 474 10.6.1 Terms 474 10.6.2 Operators and Directives 474 10.7 Review Questions and Exercises 475 10.7.1 Short Answer 475 10.7.2 Algorithm Workbench 475 10.8 Programming Exercises 477 11 MS-Windows Programming 480 11.1 Win32 Console Programming 480 11.1.1 Background Information 481 11.1.2 Win32 Console Functions 485 11.1.3 Displaying a Message Box 487 11.1.4 Console Input 490 11.1.5 Console Output 496 11.1.6 Reading and Writing Files 498 11.1.7 File I/O in the Irvine32 Library 502 11.1.8 Testing the File I/O Procedures 504 11.1.9 Console Window Manipulation 507 11.1.10 Controlling the Cursor 510 11.1.11 Controlling the Text Color 511 11.1.12 Time and Date Functions 513 11.1.13 Using the 64-Bit Windows API 517 11.1.14 Section Review 518 11.2 Writing a Graphical Windows Application 519 11.2.1 Necessary Structures 519 11.2.2 The MessageBox Function 521 11.2.3 The WinMain Procedure 521 11.2.4 The WinProc Procedure 522 11.2.5 The ErrorHandler Procedure 523 11.2.6 Program Listing 523 11.2.7 Section Review 527 11.3 Dynamic Memory Allocation 528 11.3.1 HeapTest Programs 531 11.3.2 Section Review 535 11.4 32-bit x86 Memory Management 535 11.4.1 Linear Addresses 536 11.4.2 Page Translation 539 11.4.3 Section Review 541 11.5 Chapter Summary 541 11.6 Key Terms 543 11.7 Review Questions and Exercises 543 11.7.1 Short Answer 543 11.7.2 Algorithm Workbench 544 11.8 Programming Exercises 544 12 Floating-Point Processing and Instruction Encoding 547 12.1 Floating-Point Binary Representation 547 12.1.1 IEEE Binary Floating-Point Representation 548 12.1.2 The Exponent 550 12.1.3 Normalized Binary Floating-Point Numbers 550 12.1.4 Creating the IEEE Representation 550 12.1.5 Converting Decimal Fractions to Binary Reals 552 12.1.6 Section Review 554 12.2 Floating-Point Unit 555 12.2.1 FPU Register Stack 555 12.2.2 Rounding 558 12.2.3 Floating-Point Exceptions 560 12.2.4 Floating-Point Instruction Set 560 12.2.5 Arithmetic Instructions 563 12.2.6 Comparing Floating-Point Values 567 12.2.7 Reading and Writing Floating-Point Values 570 12.2.8 Exception Synchronization 571 12.2.9 Code Examples 572 12.2.10 Mixed-Mode Arithmetic 574 12.2.11 Masking and Unmasking Exceptions 575 12.2.12 Section Review 576 12.3 x86 Instruction Encoding 577 12.3.1 Instruction Format 577 12.3.2 Single-Byte Instructions 578 12.3.3 Move Immediate to Register 579 12.3.4 Register-Mode Instructions 580 12.3.5 Processor Operand-Size Prefix 581 12.3.6 Memory-Mode Instructions 582 12.3.7 Section Review 585 12.4 Chapter Summary 585 12.5 Key Terms 587 12.6 Review Questions and Exercises 587 12.6.1 Short Answer 587 12.6.2 Algorithm Workbench 588 12.7 Programming Exercises 589 13 High-Level Language Interface 593 13.1 Introduction 593 13.1.1 General Conventions 593 13.1.2 .MODEL Directive 595 13.1.3 Examining Compiler-Generated Code 597 13.1.4 Section Review 602 13.2 Inline Assembly Code 602 13.2.1 __asm Directive in Visual C++ 602 13.2.2 File Encryption Example 605 13.2.3 Section Review 608 13.3 Linking 32-Bit Assembly Language Code to C/C++ 609 13.3.1 IndexOf Example 610 13.3.2 Calling C and C++ Functions 613 13.3.3 Multiplication Table Example 615 13.3.4 Section Review 618 13.4 Chapter Summary 619 13.5 Key Terms 620 13.6 Review Questions 620 13.7 Programming Exercises 620 14 16-Bit MS-DOS Programming 622 14.1 MS-DOS and the IBM-PC 622 14.1.1 Memory Organization 623 14.1.2 Redirecting Input-Output 624 14.1.3 Software Interrupts 625 14.1.4 INT Instruction 626 14.1.5 Coding for 16-Bit Programs 627 14.1.6 Section Review 628 14.2 MS-DOS Function Calls (INT 21h) 628 14.2.1 Selected Output Functions 630 14.2.2 Hello World Program Example 632 14.2.3 Selected Input Functions 633 14.2.4 Date/Time Functions 637 14.2.5 Section Review 641 14.3 Standard MS-DOS File I/O Services 641 14.3.1 Create or Open File (716Ch) 643 14.3.2 Close File Handle (3Eh) 644 14.3.3 Move File Pointer (42h) 644 14.3.4 Get File Creation Date and Time 645 14.3.5 Selected Library Procedures 646 14.3.6 Example: Read and Copy a Text File 647 14.3.7 Reading the MS-DOS Command Tail 649 14.3.8 Example: Creating a Binary File 651 14.3.9 Section Review 654 14.4 Chapter Summary 655 14.5 Key Terms 657 14.6 Programming Exercises 657 15 Disk Fundamentals 659 15.1 Disk Storage Systems 659 15.1.1 Tracks, Cylinders, and Sectors 660 15.1.2 Disk Partitions (Volumes) 662 15.1.3 Section Review 663 15.2 File Systems 663 15.2.1 FAT12 664 15.2.2 FAT16 664 15.2.3 FAT32 665 15.2.4 NTFS 665 15.2.5 Primary Disk Areas 666 15.2.6 Section Review 667 15.3 Disk Directory 667 15.3.1 MS-DOS Directory Structure 668 15.3.2 Long Filenames in MS-Windows 671 15.3.3 File Allocation Table (FAT) 673 15.3.4 Section Review 673 15.4 Reading and Writing Disk Sectors 674 15.4.1 Sector Display Program 675 15.4.2 Section Review 679 15.5 System-Level File Functions 680 15.5.1 Get Disk Free Space (7303h) 680 15.5.2 Create Subdirectory (39h) 683 15.5.3 Remove Subdirectory (3Ah) 684 15.5.4 Set Current Directory (3Bh) 684 15.5.5 Get Current Directory (47h) 684 15.5.6 Get and Set File Attributes (7143h) 685 15.5.7 Section Review 685 15.6 Chapter Summary 685 15.7 Key Terms 687 15.8 Programming Exercises 687 16 BIOS-Level Programming 689 16.1 Introduction 689 16.1.1 BIOS Data Area 690 16.2 Keyboard Input with INT 16h 691 16.2.1 How the Keyboard Works 691 16.2.2 INT 16h Functions 692 16.2.3 Section Review 696 16.3 Video Programming with INT 10h 697 16.3.1 Basic Background 697 16.3.2 Controlling the Color 699 16.3.3 INT 10h Video Functions 701 16.3.4 Library Procedure Examples 713 16.3.5 Section Review 713 16.4 Drawing Graphics Using INT 10h 714 16.4.1 INT 10h Pixel-Related Functions 715 16.4.2 DrawLine Program 716 16.4.3 Cartesian Coordinates Program 718 16.4.4 Converting Cartesian Coordinates to Screen Coordinates 720 16.4.5 Section Review 721 16.5 Memory-Mapped Graphics 722 16.5.1 Mode 13h: 320 * 200, 256 Colors 722 16.5.2 Memory-Mapped Graphics Program 724 16.5.3 Section Review 727 16.6 Mouse Programming 727 16.6.1 Mouse INT 33h Functions 727 16.6.2 Mouse Tracking Program 732 16.6.3 Section Review 737 16.7 Chapter Summary 738 16.8 Programming Exercises 739 A MASM Reference 741 B The x86 Instruction Set 763 C BIOS and MS-DOS Interrupts 797 D Answers to Review Questions (Chapters 14–16) 807 Glossary 816 Index 828 |