博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

汇编语句书写注意,一些汇编小程序

Posted on 2011-03-15 16:23  天地玄黄  阅读(824)  评论(0编辑  收藏  举报
mov eax,1

一定要注意:逗号之后没有空格,语句最后没有分号

 

MOVXCHG

mov destination,resource    把resource中的数copy到destination中,这两个只能有一个是memory location

xchg register1,register2    交换两个寄存器的值

section .data
section .text

        global _start
_start:
        nop
;put your experiments between the two nops ...
        mov ax,067FEh
        mov bx,ax
        mov cl,bh
        mov ch,bl
        xchg cl,ch
;put your experiments between the two nops ...
        nop
;section .bss

 

 

处于[]中的任何东西都叫做effective address(有效地址),represent the address of the data stored in memory,比如

mov eax,[ebx+ecx+esi+edi]

 

在汇编语言中,变量名代表的是地址,而不是数据:

EatMsg: db “Eat at Joe’s!“
. . . .
mov ecx,EatMsg  ;表示把EatMsg的地址放到ecx中
mov edx,[EatMsg]    ;去内存中把EatMsg所代表的地址开始的32bits的数据放到edx中
mov al,[EatMsg]     ;把1byte的数据放到al中
mov [EatMsg],byte 'G’   ;指定移动到内存中数据的大小,BYTE、WORD(16 bits)、DWORD(32 bits)

 

DEC, INC:

dec operand  减1, 

inc operand 加1

mov eax,0FFFFFFFFh
mov ebx,02Dh
dec ebx
inc eax

 

JNZJMP

jnz即Jump if Not Zero,检查ZF(zero flag)标记,如果没有设置这个标记位,则表示非零,那么就跳转。

jmp不管在什么时候都要跳转。

        mov eax,5
DoMore: dec eax
        jmp DoMore
section .data
        Snippet db "KANGAROO"
section .text
        global _start
_start:
        nop
;put your experiments between the two nops ...
        mov ebx,Snippet
        mov eax,8
DoMore: add byte [ebx],32
        inc ebx
        dec eax
        jnz DoMore
;put your experiments between the two nops ...
        nop
section .bss


NEGMOVSX

neg 把一个数取反。

movsx 即move with sign externsion,带符号移动,比如从ax移动到eax。它的两个操作数大小可以不相同,但是MOV的两个操作数大小必须相同。但是它的destination operand 必须得是register。

MOVSX

        mov ax,-42
        movsx ebx,ax

 

MULDIVIMULIDIV

mul和div对无符号的计算进行操作,后两个对有符号的计算进行操作。

· mul 的操作说一直有一个,其它的都是隐藏的操作数:

MUL

其中r/m8表示的是register/memory 8bits。

factor2的大小是根据factor1的大小确定的,它们应该一致。

当“乘”的结果大于16-bits时(第二条),“D”寄存器就会用来放结果的高位。如在第二条中,0x2a456f这个结果,DX中保存0x2a,AX中保存0x456f。

当“乘”的结果比较小的时候,存放高位数值的寄存器就会被清零。比如AH、DX、EDX。

特别注意:立即数(immediate values)不能用来当作MUL的操作数。mul 45就是错的。

mul会设置CF(Carry Flag)标记位。当有溢出发生时,这个标记位就会被设为1。

· div把除数放在EDX和EAX中,除数是自定义的factor(不能使用立即数),商放在EAX中,余数放在EDX中。被除数和除数都不能为0。

这个指令不会影响任何EFlags。

DIV

        mov eax,0xffffffff
        mov ebx,2
        mul ebx