Dividing and Conquering



  • Book: Assembly Language step by step

  • Complexity kills programs.

  • Remember to use comment headers.(Comment is very very important!)

More Than 25 lines and you're doing too much in one procedure.Split it up.

Calling and Returning


  1. call LoadBuff
  2. ...
  3. loadBuff:
  4. push eax ; a1
  5. push ebx ; a2
  6. push edx ; a3
  7. mov eax, 3 ; sys_read call
  8. mov ebx, 0 ; File Descriptor 0: stdin
  9. mov ecx, Buff ; offset of the buffer to read to
  10. mov edx, BUFFLEN ; number of bytes to read at one pass
  11. int 80h ; sys_read
  12. mov ebp, eax
  13. xor ecx, ecx
  14. pop edx ; b1
  15. pop ebx ; b2
  16. pop eax ; b3
  17. ret
  • a1~a3 and b1~b3 are store of stack
  • "xor ecx, ecx" is faster than "mov ecx, 0"

Saving the Caller's Registers


  1. pushad
  2. ...
  3. popad

Table Tricks


  1. DumpLin db " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
  2. DUMPLEN equ $-DumpLin
  3. ASCLin db "|................|", 10
  4. ASCLEN equ $-ASCLin
  5. FULLLEN equ $-DumpLin

lea edi, [edx*2+edx] ; trick to calculate 3*edx

Local Labels and the Lengths of Jumps


  1. Scan:
  2. xor eax, eax
  3. ...
  4. .modTest:
  5. test esi, 0000000Fh
  6. ...
  • "Scan" is a nonlocal label.(global label)
  • ".modTest" is a local label.
  • Local labels are local to the first nonlocal label that precedes them in the code.
  • A local label cannot be referenced higher in the source code file than the global label that owns it.
  • Local labels are not accessible as breakpoints from the command-line interface of GDB.
  • Good habits: local labels and all jumps to them should occur within a single screen of code.

Type of Jumps

  1. jne Scan ; Short jump, to within 127 bytes in either direction
  2. jne near Scan ; Near jump, anywhere in the current code segment

Building External Procedure Libraries


  • Each executable file can only contain one _start: label. External modules do not contain _start:

  • External modules do not return to Linux.(Only the main program module can make sys_exit INT 80h call)

main-program.asm:

  1. section .text
  2. extern ClearLine
  3. global _start
  4. _start:
  5. ...
  6. call ClearLine
  7. ...

lib.asm:

  1. section .text
  2. global ClearLine
  3. ...
  4. ClearLine:
  5. ...

Rules:

  • "extern" to declare all the labels that don't belong to the current file.
  • "global" to declare all the labels in the current file needed by other files.

Simple Cursor Control in the Linux Console


  1. [section .data]
  2. PositionTerm db 27, "[01;01H" ; <ESC>[<Y>;<X>H -This sequences move the cursor to (X, Y)
  3. ClearTerm db 27, "[2J" ; <ESC>[2J -This sequences clears the display.
  4. GreenBack db 27, "[42m" ; <ESC>[42m -turns the consoles background green

For more details about console escape codes: man console_codes

Creating and Using Macros


  1. %macro WriteStr 2 ; 2 arguments
  2. push eax
  3. push ebx
  4. mov ecx, %1 ; %1 invokes the first argument (Prompt)
  5. mov edx, %2 ; %2 invokes the second argument (PROMPTLEN)
  6. mov eax, 4
  7. mov edx, 1
  8. int 80h
  9. pop ebx
  10. pop eax
  11. %endmacro
  12. ....
  13. ....
  14. WriteStr Prompt, PROMPTLEN
  15. ; When a macro is invoked, its arguments are separated by commas.

Local Labels Within Macros

  1. %macro UpCase 2
  2. mov edx, %1
  3. mov ecx, %2
  4. %%IsLC:
  5. cmp byte [edx+ecx-1], 'a'
  6. jb %%Bump
  7. cmp byte [edx+ecx-1], 'z'
  8. ja %%Bump
  9. sub byte [edx+ecx-1], 20h
  10. %%Bump:
  11. dec ecx
  12. jnz %%IsLC
  13. %endmacro

Macro Libraries As Include Files

  1. %include "mylib.mac"

LinuxAsm#Chapter10的更多相关文章

  1. 【Python编程:从入门到实践】chapter10 文件和异常

    chapter10 文件和异常 10.1 从文件中读取数据 10.1.1 读取整个文件 with open("pi.txt") as file_object: contents = ...

  2. 【Linux_Unix系统编程】Chapter10 时间

    chapter10 时间 1:真实时间:度量这一时间的起点有二:(1)某个标准点:(2)进程生命周期内的某个固定时点(通常为程序启动) 2:进程时间:一个进程所使用的CPU时间总量,适用于对程序,算法 ...

  3. Chapter10

    package scala import java.io.{PrintStream, PrintWriter}import java.util.Date import scala.util.loggi ...

  4. 【笔记】《Redis设计与实现》chapter10 RDB持久化

    chapter10 RDB持久化 10.1 RDB文件的创建和载入 有两个Redis命令可以用于生成RDB文件,SAVE和BGSAVE SAVE阻塞服务器进程进行RDB文件的创建,BGSAVE则创建服 ...

  5. JavaWeb chapter10 JavaWeb开发模式

    1.  开发模式 (1)开发模式1:JSP+JavaBean (2)开发模式2:Servlet+JSP+JavaBean (MVC) 2.JavaBean 本质上是一个普通的Java类:需要遵循一定的 ...

  6. Chapter10:泛型算法

    泛型算法的基础是迭代器. 迭代器令算法不依赖于容器,但是算法依赖于元素类型的操作.也即:算法永远不会执行容器的操作. 那么,如果想向容器中添加元素或者执行其他的一些操作呢?标准库提供了插入迭代器来完成 ...

  7. Chapter10(泛型算法)--C++Prime笔记

    关键:算法通过在迭代器上进行操作来实现类型无关.算法不改变所操作序列的大小. 1.算法大多都定义在algorithm头文件中,标准库还在头文件numeric中定义了一组数值泛型算法. 2.泛型算法永远 ...

  8. 【APUE】Chapter10 Signals

    Signal主要分两大部分: A. 什么是Signal,有哪些Signal,都是干什么使的. B. 列举了非常多不正确(不可靠)的处理Signal的方式,以及怎么样设计来避免这些错误出现. 10.2 ...

  9. [SharePoint][SharePoint Designer 入门经典]Chapter10 Web部件链接

    本章概要: 1.Web部件作用 2.如何添加和配置 3.如何个性化 4.如何导出,并在其他站点重利用 5.通过组合web part创建复杂的用户界面

随机推荐

  1. 高性能javascript学习笔记系列(4) -算法和流程控制

    参考高性能javascript for in 循环  使用它可以遍历对象的属性名,但是每次的操作都会搜索实例或者原型的属性 导致使用for in 进行遍历会产生更多的开销 书中提到不要使用for in ...

  2. Ajax原生写法

    用太久JQuery了,别忘了Ajax原生是怎么写的 var Ajax = { get : function (url, callback) { var req = Ajax.getRequest(ca ...

  3. JavaScript一些基础技巧和注意事项,你了解这些吗?

    总结了一些JavaScript在开发编码中的使用技巧,如有不对,欢迎指正. 一.JavaScript在HTML和XHTML的使用 使用<script>元素有两种方式:直接在页面中嵌入Jav ...

  4. silverlight如何通过单独部署的WCF站点访问sharepoint2013的图片库

    最近有项目silverlight通过单独部署的WCF站点访问sharepoint2013的图片库,需要做个笑脸墙效果如下: 结果开发完毕后无法在SP站点显示出来.使用VS自带的WCF工具进行测试.如下 ...

  5. 数据持久化之NSKeyedArchiver

    基本的数据类型如NSString.NSDictionary.NSArray.NSData.NSNumber等可以用属性列表的方法持久化到.plist 文件中,但如果是一些自定义的类的话,属性列表的方法 ...

  6. Android Content Provider基础

    Android Content Provider基础 Content Providers Content providers管理对一个结构化的数据集合的访问.它们封装了数据,并且提供了保护数据安全性的 ...

  7. 操作系统开发系列—10.内核HelloWorld ●

    a.我们先来体验一下在Linux下用汇编编程的感觉,见代码 [section .data] ; 数据在此 strHello db "Hello, world!", 0Ah STRL ...

  8. Android 扒开美女衣服

    本文主要实现一个小的扒开美女衣服的游戏项目 效果如下: 项目布局设计: <FrameLayout xmlns:android="http://schemas.android.com/a ...

  9. Android开发小问题记录

    安卓资源文件无法命名大写字母,否则导致不会生成R类!!! 资源文件的命名容许的字符为“a-z0-9_.”,即只容许有小写字母,数字0-9,下划线和点 Notification不显示 有些手机会对not ...

  10. 利用RxJava获取手机已安装的App的图片、应用名称和版本号

    先上效果图: 获取手机已安装的App列表利用Android系统API就可以办到,这里为什么要引入RxJava?现在我们假设一下有下面几个需求: 1.我们不需要所有的App,只需要用户安装的第三方App ...