10.汇编语言--伪指令(offset、prt、lengthof)

offset 伪指令的使用

;伪指令  类似与高级语言
; offset  偏移量  距离段启始位置的偏移地址,获取的始一个地址

1.数组的第一种使用

//省略了头
.data
arrNum dword 0,1,2,3

.code
main proc
    mov eax,[arrNum+4]   
    call ExitProcess
    add esp,4

main ENDP
END    main

 2.用offset 对地址操作

重要知识点:

    ;把arrNum 偏移地址移到eax
    mov eax,offset arrNum + 4
    ;因为eax里面保存的是地址,所以用 [] 是取地址的值
    mov eax,[eax]
.586    
.MODEL flat,stdcall
option casemap:none

; inc 是一个头文件
include  windows.inc
include user32.inc
include kernel32.inc
;msvcrt.inc 引用c中的输入输出功能
include  msvcrt.inc

;库文件
includelib user32.lib
includelib kernel32.lib
includelib msvcrt.lib

.data
arrNum dword 0,1,2,3


.code
main proc
    ;把arrNum 偏移地址移到eax
    mov eax,offset arrNum + 4
    ;因为eax里面保存的是地址,所以用 [] 是取地址的值
    mov eax,[eax]
    call ExitProcess
    add esp,4

main ENDP
END    main

ptr 伪指令的使用

;使用: word ptr 操作数   //取操作数的低16位,word 可以换位其他数据类型
;eax 是32位的  ax是16位,eax的低16位
.data
;arrNum word 0,1,2,3
arrNum dword 5

.code
main proc
    ;把arrNum 偏移地址移到eax
    mov ax,word ptr arrNum
    
    call ExitProcess
    add esp,4

main ENDP
END    main

;伪指令  类似与高级语言
; ptr
;使用: word ptr 操作数   //取操作数的低16位
;eax 是32位的  ax是16位,eax的低16位

lengthof、sizeof 伪指令的使用
lengthof 计算数组返回多少元素

lengthof使用:
mov eax,offset lengthof arrNum
sizeof 字节数*数组的个数
sizeof的使用:

mov eax,sizeof arrNum

.586    
.MODEL flat,stdcall
option casemap:none

; inc 是一个头文件
include  windows.inc
include user32.inc
include kernel32.inc
;msvcrt.inc 引用c中的输入输出功能
include  msvcrt.inc

;库文件
includelib user32.lib
includelib kernel32.lib
includelib msvcrt.lib

.data
;arrNum word 0,1,2,3
arrNum db 0,1,2,3

.code
main proc
    mov eax,offset lengthof arrNum
    
    call ExitProcess
    add esp,4

main ENDP
END    main

;伪指令  类似与高级语言
; type
;使用: word ptr 操作数   //获得数据的类型的字节数

;lengthof 计算数据返回多少元素

原文地址:https://www.cnblogs.com/trevain/p/14528184.html