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


call LoadBuff
...
loadBuff:
push eax ; a1
push ebx ; a2
push edx ; a3
mov eax, 3 ; sys_read call
mov ebx, 0 ; File Descriptor 0: stdin
mov ecx, Buff ; offset of the buffer to read to
mov edx, BUFFLEN ; number of bytes to read at one pass
int 80h ; sys_read
mov ebp, eax
xor ecx, ecx
pop edx ; b1
pop ebx ; b2
pop eax ; b3
ret
  • a1~a3 and b1~b3 are store of stack
  • "xor ecx, ecx" is faster than "mov ecx, 0"

Saving the Caller's Registers


pushad
...
popad

Table Tricks


DumpLin db " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
DUMPLEN equ $-DumpLin
ASCLin db "|................|", 10
ASCLEN equ $-ASCLin
FULLLEN equ $-DumpLin

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

Local Labels and the Lengths of Jumps


Scan:
xor eax, eax
...
.modTest:
test esi, 0000000Fh
...
  • "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

jne Scan ; Short jump, to within 127 bytes in either direction
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:

section .text
extern ClearLine
global _start
_start:
...
call ClearLine
...

lib.asm:

section .text
global ClearLine
...
ClearLine:
...

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


[section .data]
PositionTerm db 27, "[01;01H"  ;  <ESC>[<Y>;<X>H  -This sequences move the cursor to (X, Y)
ClearTerm db 27, "[2J"  ;  <ESC>[2J    -This sequences clears the display.
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


%macro WriteStr 2 ; 2 arguments
    push eax
    push ebx
    mov ecx, %1 ; %1 invokes the first argument (Prompt)
    mov edx, %2 ; %2 invokes the second argument (PROMPTLEN)
    mov eax, 4
    mov edx, 1
    int 80h
    pop ebx
    pop eax
%endmacro
....
....
WriteStr Prompt, PROMPTLEN
; When a macro is invoked, its arguments are separated by commas.

Local Labels Within Macros

%macro UpCase 2
    mov edx, %1
    mov ecx, %2
%%IsLC:
    cmp byte [edx+ecx-1], 'a'
    jb %%Bump
    cmp byte [edx+ecx-1], 'z'
    ja %%Bump
    sub byte [edx+ecx-1], 20h
%%Bump:
    dec ecx
    jnz %%IsLC
%endmacro

Macro Libraries As Include Files

%include "mylib.mac"

最新文章

  1. C#接口的显示和隐式实现
  2. 一个强大的jquery分页插件
  3. Reflection应用场景-利用反射机制将表单数据自动填充到JavaBean中
  4. yum只下载不安装:yumdownloader
  5. shell中括号的使用
  6. C.C++把整个文件内容读进一个buffer中
  7. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q75-Q77)
  8. windows OBJECT查找
  9. Winform开发框架之客户关系管理系统(CRM)的开发总结系列3-客户分类和配置管理实现
  10. GeoServer+MySQL安装及配置过程
  11. SharePoint 2013 Nintex Workflow 工作流帮助(七)
  12. UVA116 单向 DSP(多段图最短路)
  13. java设计模式类图大全
  14. php 提交保存成功页面 倒计时 跳转
  15. Vs2012在Linux应用程序开发(3):加入新平台hi3516
  16. cxf 报错:java.lang.NoSuchMethodError: org.apache.ws.commons.schema.XmlSchemaCollection.read(Lorg/w3c/dom/Document;Ljava/lang/String;)
  17. virtual box ubuntu 与Windows共享文件夹
  18. C#委托基础学习
  19. 深入浅出数据结构C语言版(14)——散列表
  20. ajax异步的问题,(主要解决有时候前台打断点和不打断点结果不一样的问题,一般情况下是存在异步的问题)

热门文章

  1. javascript创建对象的几种方式
  2. javascript笔记图
  3. Electron笔记
  4. 如何排查sharepoint2010用户配置文件同步服务启动问题
  5. Phonegap 之 iOS银联在线支付(js调用ios端银联支付控件)
  6. 【C语言】C语言标识符
  7. UIView属性
  8. Android消息机制源码分析
  9. django tmeplate 循环基数
  10. 【问题排查记录】Field &#39;id&#39; doesn&#39;t have a default value;