[地址] 是取地址指向的内容:
; Test14_1.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
    
.data
    dwVal dd 123
.code
main proc
    PrintDec offset dwVal ;4206592 - 这是变量 dwVal 的地址
    PrintDec dwVal        ;123     - 这是变量值
    
    lea ebx, dwVal        ;Lea 是专门获取地址的指令, 这同 mov ebx, offset dwVal
    PrintDec ebx          ;4206592
    
    mov eax, [ebx]        ;现在 ebx 中放着 dwVal 变量的地址, [ebx] 则表示地址指向的值
    PrintDec eax          ;123
    ret
main endp
end main


地址指向的是什么数据?
; Test14_2.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
    
.data
    wVal dw 123
.code
main proc
    lea ebx, wVal  ;把变量 wVal 的地址放到 ebx
    
    ;但现在 wVal 中放着的是 word 类型的数据, 而 [ebx] 默认是取 32 位的数据
    ;这可通过伪指令 ptr 指定数据大小
    ;同时需要把接收着换成 16 的(如 ax), 因为 mov 要求两个操作数的大小须一致
    xor eax, eax  ;清空 eax
    mov ax, word ptr [ebx]
    PrintDec eax  ;123
    
    ;也可以直接使用 movzx, 它可以从小到大(movzx r16/r32, r/8/r16/m8/m16)
    movzx eax, word ptr [ebx]
    PrintDec eax  ;123
    ret
main endp
end main


[] 一般用于数组:
; Test14_3.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
    
.data
    wArr dw 11,22,33
.code
main proc
    ;把数组 bArr 的起始地址放到 ebx, 并获取数组元素:
    lea ebx, wArr  
    movzx eax, word ptr [ebx]
    movzx ecx, word ptr [ebx+2]
    movzx edx, word ptr [ebx+4]
    PrintDec eax  ;11
    PrintDec ecx  ;22
    PrintDec edx  ;33
    
    ;这样做更好些:
    lea ebx, wArr
    mov esi, type wArr ;把元素大小放在 esi
    movzx eax, word ptr [ebx]
    movzx ecx, word ptr [ebx + esi * 1]
    movzx edx, word ptr [ebx + esi * 2]
    PrintDec eax  ;11
    PrintDec ecx  ;22
    PrintDec edx  ;33
    
    ;这样也行:
    lea ebx, wArr
    mov esi, type wArr ;把元素大小放在 esi
    movzx eax, word ptr [ebx]
    movzx ecx, word ptr [ebx][esi * 1]
    movzx edx, word ptr [ebx][esi * 2]
    PrintDec eax  ;11
    PrintDec ecx  ;22
    PrintDec edx  ;33
    
    ;如果直接使用变量就更像高级语言里的数组了:
    lea ebx, wArr
    mov esi, type wArr ;把元素大小放在 esi
    movzx eax, word ptr wArr
    movzx ecx, word ptr wArr[esi * 1]
    movzx edx, word ptr wArr[esi * 2]
    PrintDec eax  ;11
    PrintDec ecx  ;22
    PrintDec edx  ;33

    ret
main endp
end main


当然也可以写入数组:
; Test14_4.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
    
.data
    wArr dw 11h ,22h, 33h
.code
main proc
    DumpMem offset wArr, sizeof wArr ;11 00 22 00 33 00
    lea ebx, wArr  
    mov word ptr [ebx],   44h
    mov word ptr [ebx+2], 55h
    mov word ptr [ebx+4], 66h
    DumpMem offset wArr, sizeof wArr ;44 00 55 00 66 00
    
    ;或者:
    mov word ptr wArr,    77h
    mov word ptr wArr[2], 88h
    mov word ptr wArr[4], 99h
    DumpMem offset wArr, sizeof wArr ;77 00 88 00 99 00
    ret
main endp
end main


好像 [] 不能直接是变量或常数(变量后面跟的 [] 除外).

posted on 2010-04-08 15:52  万一  阅读(3881)  评论(1编辑  收藏  举报