美文网首页
16位汇编

16位汇编

作者: 金发萌音 | 来源:发表于2014-06-21 00:31 被阅读92次

Logic Instructions

  • AND DST, SRC ; (DST)<--(DST) & (SRC)

  • OR DST, SRC ; (DST)<--(DST) | (SRC)

  • XOR DST, SRC ; (DST)<--(DST) xor (SRC)

    这三条指令有几个常用的功能,原理是基于数字0~9与其ASK码相差30H

    • Convert one ASCII code to integer
      SUB AL, 30h
      AND AL, 0Fh

    • Convert one digit integer to ASCII
      OR BL, 30h

    • Change the case of ASCII code
      XOR CL , 00100000b

    • TEST OPR1, OPR2 ; (OPR1)&(OPR2)
      与and功能一样,不同的是test并不改变目标操作数
      上述4条指令对FLAG寄存器的影响
      CF OF 设为0 PF SF ZF看结果

    • NOT DST
      取反操作

    • OP DST, CNT; OP is SHL, SAL, SHR, SAR 移位指令

      • SHL/SAL 逻辑/算数左移 产生的效果是一样的 都是 左边被移除的数字进入CF中 右边补0
      • SHR Logically shift right 逻辑右移 最低进入 CF 最高位补0
      • SAR Arithmetically shift right 算数左移
    • Rotate Instructions
      OP DST, CNT; OP is ROL, RCL, ROR, RCR

      • ROL 向左循环移动 cnt次
      • RCL 连同CF位向左循环
      • ROR 向右循环
      • RCR 连同CF位向右循环

      Example
      Convert ASCII string ‘96’ to packed BCD 96H

      MOV AL, ASC ; AL<--39H (‘9’)
      MOV CL, 4
      SHL AL, CL ;39H->90H
      MOV BL, AL ;BL<--90H
      MOV AL, ASC+1 ;AL<--36H (‘6’)
      AND AL, 0FH ;AL<--06H
      OR BL, AL ;BL<--96H
      MOV BCD, BL

Jump Instructions

  • Short Jump

    JMP SHORT OPR
    (E)IP=(E)IP+8-bit displacement

  • Near Jump

    JMP OPR ;当前段内跳转
    (E)IP=(E)IP+16-bit displacement

  • Far Jump

    JMP FAR PTR OPR

  • Jumps with Register Operands

    JMP reg16/32

  • Indirect Jumps Using an index

    JMP WORD PTR OPR ; NEAR JUMP
    JMP DWORD PTR OPR ; FAR JUMP
    * New address is
    * NEAR JUMP
    IP = (OPR)
    * FAR JUMP
    (E)IP<--((DS)16+OPR)
    CS<--((DS)
    16+OPR+2/4)

  • Conditional Jump

    Jcc disp8
    * if (cc) is TRUE THEN
    (E)IP =(E)IP+8-bit displacement
    * else
    continue to execute the next instruction

```
Conditional Jump (Jcc)
    Assembly    Operation       Tested Condition                        判断条件
    JC          Jump            if Carry CF=1 
    JNC         Jump            if no Carry CF=0 
    JZ/JE       Jump            if equal or Zero ZF=1 
    JNZ/JNE     Jump            if no equal or no Zero ZF=0 
    JS          Jump            if sign (negative) SF=1 
    JNS         Jump            if no sign (positive) SF=0 
    JO          Jump            if overflow OF=1 
    JNO         Jump            if no overflow OF=0 
    JP/JPE      Jump            if parity even PF=1 
    JNP/JPO     Jump            if parity odd PF=0 
    JCXZ        Jump            if CX is Zoro CX=0 
    JECXZ       Jump            if ECX is Zero ECX=0
    JA JNBE                     ZF=0 and CF=0                           A>B Unsigned number
    JB JNAE                     ZF=0,CF=1                               A<B Unsigned number
    JAE JNB                     ZF=1 or CF=0                            A>=B Unsigned number
    JBE JNA                     ZF=1 or CF=1                            A<=B Unsigned number
    
    JG JNLE                     SF=OF and ZF=0                          A>B Signed number
    JL JNGE                     SF<>OF and ZF=0                         A<B Signed number
    JGE JNL                     SF=OF or ZF=1                           A>=B Signed number
    JLE JNG                     SF<>OF或ZF=1                            A<=B Signed number
```
  • Branch Structure Programming
    举例:
    There is a WORD array named ARRAY which has N elements, please examine the array, then put the number of positive in DI register, the number of 0 in SI register, and the number of negative in AX register

    
    
    
  • Machine Control Instructions
    CLC —— clear carry flag; CF=0
    CMC —— complement carry flag; CF=~CF
    STC —— set carry flag; CF=1
    CLD —— clear direction flag; DF=0
    STD —— set direction flag; DF=1
    CLI —— clear interrupt flag; IF=0
    STI —— set interrupt flag; IF=13

Loop Instructions

LOOP LABEL ; LOOP
LOOPZ/LOOPE LABEL ; Condition LOOP 配合CMP语句可以判断相等
LOOPNZ/LOOPNE LABEL ; Condition LOOP

这一部分的关键在于使用CMP 和JCC的组合...之后会写几个程序练练手

String Instructions

对于所有的串操作:SI里一般村原串地址,DI存目标串地址(段地址DS,ES同理)
并且SI,DI里面的值会自动改变,改变的方式依赖于DF位的设立,如果DF=0则为"正方向"每次自增1个单位,DF=1时为"反方向",每次操作递减1个单位(一个单位具体代表多少看具体的命令,就用LODS指令来说,LODSB 一个单位=1,LODSW =2 LODSD=4

  • LODS指令读取原串.将SI指向的串存入AX中

  • STOS指令存目标串,将AX中的串存入DI指向的串,经常与REP一起使用

  • MOVS 将DS:SI中的内容复制到ES:DI,经常与REP一起使用

  • INS outs 从IO设备中存取字符串

  • SCAS 比较ds:di中的数据与AX中的数据

    ;查找String中的'a'字符
    CLD
    LEA EDI, String
    MOV AL, ‘a’
    MOV ECX, 11
    REPNZ SCASB
    JZ FOUND

  • CMPS 比较DI与SI中的数据

    ;Compare SOURCE and DESTIN, if not same, load the first char in SOURCE that not match into AL register
    

LEA SI, SOURCE
LEA DI, DESTIN
CLD
MOV CX,100
REPZ CMPSB
JCXZ MATCH
DEC SI ; When terminal the loop, (E)SI and
; (E)DI point to the next position
LODSB
...
MATCH:
```

程序指令

简单来说就是利用CALL指令和RET指令来完成函数调用
用寄存器或者堆栈来传参

相关文章

网友评论

      本文标题:16位汇编

      本文链接:https://www.haomeiwen.com/subject/alljtttx.html