编程语言
首页 > 编程语言> > 汇编语言-004(LABEL 、间接寻址、变址操作数、指针使用、TypeDef、LOOP、DWORD变量交换高位低位字)

汇编语言-004(LABEL 、间接寻址、变址操作数、指针使用、TypeDef、LOOP、DWORD变量交换高位低位字)

作者:互联网

1:
LABEL : 为一个标号定义大小属性,但不分配内存与下一个变量共用内存,与C中UNION类似

.386
.model flat,stdcall

.stack 4096
ExitProcess PROTO,dwExitCoed:DWORD

.data
val16 LABEL WORD
val32 DWORD 12345678h

LongValue LABEL DWORD
val1 WORD 5678h
val2 WORD 1234h

.code
main PROC
   mov ax,val16    ;5678h
   mov dx,[val16+2];1234h

   mov eax,LongValue  ;12345678h
   INVOKE ExitProcess,0
main ENDP
END main

2:
间接寻址,因为常数偏移量寻址多个数组元素时,直接寻址不实用,反之,用寄存器作为指针(称为间接寻址)
并控制寄存器的值,如果一个操作数使用的间接寻址,就称为间接操作数

.386
.model flat,stdcall

.stack 4096
ExitProcess PROTO,dwExitCode:DWORD

.data
byteVal BYTE 10h

.code
main PROC
   mov esi,OFFSET byteVal
   mov al,[esi]

   ;PTR与间接操作数一起使用
   ;inc [esi] 一个操作数的大小无法从指令看出,报错
   inc BYTE PTR [esi] 
   INVOKE ExitProcess,0
main ENDP
END main

3:
间接寻址访问数组案例

.386
.model flat,stdcall

.stack 4096
ExitProcess PROTO,dwExitCode:DWORD

.data
arrayB BYTE 10h,20h,30h
arrayW WORD 1000h,2000h,3000h
arrayD DWORD 10000h,20000h,30000h

.code
main PROC
   mov esi,OFFSET arrayB
   mov al,[esi]
   inc esi
   mov al,[esi]
   inc esi
   mov al,[esi]

   mov esi,OFFSET arrayW
   mov ax,[esi]
   add esi,2
   mov ax,[esi]
   add esi,2
   mov ax,[esi]

   mov esi,OFFSET arrayD
   mov eax,[esi]
   add esi,4
   mov eax,[esi]
   add esi,4
   mov eax,[esi]
   INVOKE ExitProcess,0
main ENDP
END main

4:变址操作数,访问数组更灵活

.386
.model flat,stdcall

.stack 4096
ExitProcess PROTO,dwExitCode:DWORD

.data
arrayB BYTE 10h,20h,30h
arrayW WORD 1000h,2000h,3000h
arrayD DWORD 10000h,20000h,30000h

.code
main PROC
   mov esi,0
   mov al,arrayB[esi]  ;第一种格式constant [reg]

   mov esi,OFFSET arrayW ;第二种格式 [constant + reg]
   mov ax,[esi]
   mov ax,[esi+2]
   mov ax,[esi+4]

   ;比例因子
   mov esi,3 * TYPE arrayD
   mov eax,arrayD[esi]

   ;更简单的做法
   mov esi,3 ;下标
   mov eax,arrayD[esi * 4]
   ;使用TYPE 更灵活
   mov eax,arrayD[esi * TYPE arrayD]
   INVOKE ExitProcess,0
main ENDP
END main

5:
指针使用

.386
.model flat,stdcall

.stack 4096
ExitProcess PROTO,dwExitCode:DWORD

.data
arrayB BYTE 10h,20h,30h,40h
arrayW WORD 1000h,2000h,3000h,4000h
ptrB DWORD arrayB
ptrW DWORD arrayW
;使用OFFSET运算符使这种关系更加准确
optrB DWORD OFFSET arrayB
optrW DWORD OFFSET arrayW

.code
main PROC
     INVOKE ExitProcess,0
main ENDP
END main

6:
TypeDef : 定义新类型,与C的typedef 类似,不在.data段内定义的

.386
.model flat,stdcall

.stack 4096
ExitProcess PROTO,dwExitCode:DWORD

PBYTE TYPEDEF PTR BYTE     ;字节指针
PWORD TYPEDEF PTR WORD     ;字指针
PDWORD TYPEDEF PTR DWORD  ;双字指针

.data
arrayB BYTE 10h,20h,30h,40h
arrayW WORD 1,2,3
arrayD DWORD 4,5,6



ptr0 PBYTE ? ;未初始化
ptr1 PBYTE arrayB
ptr2 PWORD arrayW
ptr3 PDWORD arrayD

.code
main PROC
     
     mov esi,ptr1
	 mov al,[esi]

	 mov esi,ptr2
	 mov ax,[esi]
	 
	 mov esi,ptr3
	 mov eax,[esi]
     INVOKE ExitProcess,0
main ENDP
END main

7:
LOOP : 循环伪指令使用

.386
.model flat,stdcall

.stack 4096
ExitProcess PROTO,dwExitCode:DWORD

.data
count DWORD ?
bytes BYTE 1,2,3,4
.code
main PROC
     mov ecx,100     ;设置外层循环计数
L1:
     mov count,ecx    ;保存外层计数
	 mov ecx,20       ;设置内层计数
L2:
     mov eax,ecx
     loop L2       ;重复内层
	 mov ecx,count ;恢复外层计数
	 loop L1       ;重复外层
     INVOKE ExitProcess,0
main ENDP
END main

8:
LOOP_ARRAY : 数组元素求和例子

.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD

.data
intarray DWORD 10000h,20000h,30000h,40000h

.code
main PROC
     mov edi,OFFSET intarray    ;EDI=intarray地址 作变址操作数
	 mov ecx,LENGTHOF intarray  ;循环计数器初始化为元素个数
	 mov eax,0                  ;EAX 记录总数
L1:
     add eax,[edi]              ;加一个元素
	 add edi,TYPE intarray      ;指向下一个元素
	 loop L1                    ;重复
   
     INVOKE ExitProcess,0
main ENDP
END main

9:
LOOP_STRAING : 复制字符串例子

.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD

.data
source BYTE "This is the source string",0
target BYTE  SIZEOF source DUP(0)

.code
main PROC
     mov esi,0               ;变址计数器
	 mov ecx,SIZEOF source   ;循环计数器
L1:
     mov al,source[esi]      ;从源字符串取一个字符
	 mov target[esi],al      ;保存到目标字符串
	 inc esi                 ;指向下一个字节
	 loop L1                 ;重复
  
    INVOKE ExitProcess,0
main ENDP
END main

10:
DWORD变量交换高位低位字

.386
.model flat,stdcall
 
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD

.data
tword LABEL WORD
three DWORD 12345678h  ;56781234

.code
main PROC

    mov si,tword            ;或者用WORD PTR three
	mov di,[tword+2]        ;WORD PTR [three + 2]
	mov tword,di            ;WORD PTR three
	mov [tword+2],si        ;WORD PTR [three + 2]
	mov eax,three
	INVOKE ExitProcess,0
main ENDP
END main

标签:TypeDef,WORD,变址,mov,LABEL,ExitProcess,DWORD,main,esi
来源: https://blog.csdn.net/m0_37599645/article/details/111636436