PTR、OFFSET、ADDR

本文介绍了在汇编语言中如何使用PTR指定数据尺寸进行操作,OFFSET获取全局变量或标号的偏移地址,ADDR获取变量地址的方法,并对比了OFFSET与ADDR的区别及应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PTR: 指定要操作的数据尺寸

; Test12_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 
   val db 11h, 22h, 33h, 44h, 55h, 66h, 77h, 88h 
 
.code 
main proc 
   xor eax, eax       ;清空 EAX, 同 mov eax, 0 
   mov eax, dword ptr val  ; 
   PrintHex eax       ;44332211 
   
   xor eax, eax       ; 
   mov eax, dword ptr val+1 ; 
   PrintHex eax       ;55443322 
   
   xor eax, eax       ; 
   mov ax, 
word ptr val   ; 
   PrintHex eax       ;00002211 
   
   xor eax, eax       ; 
   mov al, byte ptr val   ; 
   PrintHex eax       ;00000011 
   ret 
main endp 
end main

 

常常这样使用:dword ptr;word ptr;byte ptr;分别指定要操作的数据长度为双字、字。字节。


OFFSET: 获取全局变量或标号的偏移地址

; Test12_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 
   v1 db 'abcdefg', 0 
   v2 dd 11223344h 
 
.code 
main proc 
   PrintHex offset v1  ;00403000 
   PrintHex offset v2  ;00403008 
   PrintHex offset main ;00401000 - 这里的 main 是个标号 
   ret 
;本例中的 offset 不能用 addr 代替 
main endp 
end main 

 

ADDR: 类似 offset 也是获取变量的地址...

; Test12_3.asm 
.386 
.model flat, stdcall 
 
;include  windows.inc 
include  kernel32.inc 
includelib kernel32.lib 
include  user32.inc 
includelib user32.lib 
 
.data 
   v1 dd 00434241h ;ABC 
   v2 dd 00636261h ;abc 
 
.code 
main proc 
   invoke MessageBox, 0, offset v1, offset v2, 0 ;现在 v1、v2 是全局变量 
   invoke MessageBox, 0,  addr v2,  addr v1, 0 ;使用 offset 和 addr 均可 
   invoke ExitProcess, 0 
main endp 
end main 

  获取局部变量的地址只能使用 ADDR:

; Test12_4.asm 
.386 
.model flat, stdcall 
 
;include  windows.inc 
include  kernel32.inc 
includelib kernel32.lib 
include  user32.inc 
includelib user32.lib 
 
.code 
main proc 
   LOCAL v1,v2 
   mov v1, 00434241h 
   mov v2, 00636261h 
   ;invoke MessageBox, 0, offset v1, offset v2, 0 ;offset 不能获取局部变量的地址 
   invoke MessageBox, 0,  addr v2,  addr v1, 0 
   invoke ExitProcess, 0 
main endp 
end main 

  OFFSET 和 ADDR 的异同:

  1、offset 不能获取局部变量的地址;

  2、addr 只能用于调用函数(invoke)时, 不能用于赋值操作;

  3、addr 面对局部变量时会转换为 lea 等指令, addr 面对全局变量时则直接调用 offset;

  4、在 invoke 中应尽量使用 addr, 其他只用 offset.

struct task_struct_offset { int16_t pid_offset; int16_t tgid_offset; int16_t thread_pid_offset; int16_t ptracer_cred_offset; int16_t real_cred_offset; int16_t cred_offset; int16_t comm_offset; int16_t fs_offset; int16_t files_offset; int16_t loginuid_offset; int16_t sessionid_offset; int16_t seccomp_offset; int16_t security_offset; int16_t stack_offset; int16_t tasks_offset; int16_t mm_offset; int16_t active_mm_offset; }; int resolve_current() { uint64_t sp_el0, sp; asm volatile("mrs %0, sp_el0" : "=r"(sp_el0)); asm volatile("mov %0, sp" : "=r"(sp)); sp_el0_is_current = 0; sp_el0_is_thread_info = 0; init_task = (struct task_struct *)kallsyms_lookup_name("init_task"); uint64_t init_thread_union_addr = kallsyms_lookup_name("init_thread_union"); if (is_kimg_range(sp_el0)) { if (sp_el0 == init_thread_union_addr) { sp_el0_is_thread_info = 1; } else if ((uint64_t)init_task == sp_el0 || (sp_el0 & (page_size - 1)) || (task_struct_offset.comm_offset = find_swapper_comm_offset(sp_el0, TASK_STRUCT_MAX_SIZE)) > 0) { sp_el0_is_current = 1; init_task = (struct task_struct *)sp_el0; } else { sp_el0_is_thread_info = 1; } } int thread_shift_cand[] = { 14, 15, 16 }; for (int i = 0; i < sizeof(thread_shift_cand) / sizeof(thread_shift_cand[0]); i++) { int tsz = 1 << thread_shift_cand[i]; uint64_t sp_low = sp & ~(tsz - 1); uint64_t psp = sp_low; for (; psp < sp_low + THREAD_INFO_MAX_SIZE; psp += 8) { if (*(uint64_t *)psp == STACK_END_MAGIC) { if (psp == sp_low) { thread_size = tsz; stack_end_offset = 0; thread_info_in_task = 1; } else { thread_size = tsz; stack_end_offset = psp - sp_low; thread_info_in_task = 0; } break; } } } if (!thread_info_in_task) { uint64_t thread_info_addr = (uint64_t)current_thread_info_sp(); if (init_task) { for (uint64_t ptr = thread_info_addr; ptr < thread_info_addr + stack_end_offset; ptr += sizeof(uint64_t)) { uint64_t pv = *(uint64_t *)ptr; if (pv == (uint64_t)init_task) { task_in_thread_info_offset = ptr - thread_info_addr; break; } } } else { for (uint64_t ptr = thread_info_addr; ptr < thread_info_addr + stack_end_offset; ptr += sizeof(uint64_t)) { uint64_t pv = *(uint64_t *)ptr; task_struct_offset.comm_offset = find_swapper_comm_offset(pv, TASK_STRUCT_MAX_SIZE); if (task_struct_offset.comm_offset > 0) { init_task = (struct task_struct *)pv; task_in_thread_info_offset = ptr - thread_info_addr; } } } } if (task_struct_offset.comm_offset <= 0) { task_struct_offset.comm_offset = find_swapper_comm_offset((uint64_t)init_task, TASK_STRUCT_MAX_SIZE); } uint64_t stack_base = (sp & ~(thread_size - 1)); for (uintptr_t i = (uintptr_t)init_task; i < (uintptr_t)init_task + TASK_STRUCT_MAX_SIZE; i += sizeof(uintptr_t)) { uintptr_t val = *(uintptr_t *)i; if (stack_base == val) { stack_in_task_offset = i - (uintptr_t)init_task; task_struct_offset.stack_offset = stack_in_task_offset; break; } } return 0; }请问tasks_offset应该按照这样的代码方法获取呢?
03-23
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <sys/mman.h> #define _GNU_SOURCE #include <dlfcn.h> typedef void* (*hook_mmap)(void *addr, size_t length, int prot, int flags, int fd, off_t offset); typedef void* (*hook_munmap)(void *addr, size_t length); void* fun(void *ptr){ while(1){ } } void *__GI___mmap64(void *addr, size_t length, int prot, int flags, int fd, off_t offset){ static hook_mmap pmmap = NULL; if(pmmap == NULL){ pmmap = (hook_mmap)dlsym(RTLD_NEXT, "__GI___mmap64"); } static char buffer[128] = {0}; void *caller_addr = __builtin_return_address(0); void *ptr = pmmap(addr, length, prot, flags, fd, offset); int ret = sprintf(buffer, "%p: __mmap:%p,%ld\n", caller_addr, ptr, length); write(1, buffer, ret); write(fd, buffer, ret); fsync(fd); return ptr; } void* __wrap___mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) { printf("拦截__mmap调用! size=%zu\n", length); // 调用原始__mmap函数 static hook_mmap pmmap = NULL; if(pmmap == NULL){ pmmap = (hook_mmap)dlsym(RTLD_NEXT, "mmap64"); } static char buffer[128] = {0}; void *caller_addr = __builtin_return_address(0); void *ptr = pmmap(addr, length, prot, flags, fd, offset); int ret = sprintf(buffer, "%p: __mmap:%p,%ld\n", caller_addr, ptr, length); write(1, buffer, ret); write(fd, buffer, ret); fsync(fd); printf("分配地址: %p\n", ptr); return ptr; } int main(){ void *paddr = malloc(10); printf("Allocated memory at %p\n", paddr); pthread_t tid = 0; pthread_create(&tid, NULL, fun, NULL); void *ptr = mmap(NULL, 1024, 0, 0, -1, 0); while(1){ } free(paddr); return 0; } gcc memtest.c -o memtest -Wl,--wrap=__mmap
07-06
两个type结构体的指针分配有误 ! 设置大数组共享内存 - 分为5个变量 subroutine setup_shared_memory_data(node_comm, node_rank, ensemble_pi_ptr, & ensemble_u_ptr, ensemble_v_ptr, ensemble_th_ptr, ensemble_q_ptr, & back_data_ptr, true_data_ptr, & win_ensemble_pi, win_ensemble_u, win_ensemble_v, & win_ensemble_th, win_ensemble_q, win_back, win_true) use module_initial use iso_c_binding integer, intent(in) :: node_comm, node_rank real, pointer, intent(out) :: ensemble_pi_ptr(:,:,:,:), ensemble_u_ptr(:,:,:,:), ensemble_v_ptr(:,:,:,:), & ensemble_th_ptr(:,:,:,:), ensemble_q_ptr(:,:,:,:) type(model_data), pointer, intent(inout) :: back_data_ptr, true_data_ptr integer, intent(out) :: win_ensemble_pi, win_ensemble_u, win_ensemble_v, win_ensemble_th, win_ensemble_q, win_back, win_true integer(kind=MPI_ADDRESS_KIND) :: ssize_ensemble, ssize_data integer :: ierr, disp_unit type(c_ptr) :: baseptr_pi, baseptr_u, baseptr_v, baseptr_th, baseptr_q, baseptr_back, baseptr_true ! --------------------------------------------------- ! back_data_ptr 和 true_data_ptr 是 model_data 类型的指针 integer(kind=c_intptr_t) :: addr type(c_ptr) :: c_ptr_back, c_ptr_true ! --------------------------------------------------- disp_unit = 4 ! sizeof(real) ! 集合数据大小 - 每个变量单独分配 if (node_rank == 0) then ssize_ensemble = int(nx * nz * ny * nsample, MPI_ADDRESS_KIND) ! 单个变量 ssize_data = int(nx * nz * ny * 5, MPI_ADDRESS_KIND) ! 5个变量 else ssize_ensemble = 0 ssize_data = 0 end if ! 分配5个集合变量的共享内存 call MPI_Win_allocate_shared(ssize_ensemble * disp_unit, disp_unit, MPI_INFO_NULL, & node_comm, baseptr_pi, win_ensemble_pi, ierr) if (node_rank /= 0) then call MPI_Win_shared_query(win_ensemble_pi, 0, ssize_ensemble, disp_unit, baseptr_pi, ierr) end if call c_f_pointer(baseptr_pi, ensemble_pi_ptr, [nx, nz, ny, nsample]) call MPI_Win_allocate_shared(ssize_ensemble * disp_unit, disp_unit, MPI_INFO_NULL, & node_comm, baseptr_u, win_ensemble_u, ierr) if (node_rank /= 0) then call MPI_Win_shared_query(win_ensemble_u, 0, ssize_ensemble, disp_unit, baseptr_u, ierr) end if call c_f_pointer(baseptr_u, ensemble_u_ptr, [nx, nz, ny, nsample]) call MPI_Win_allocate_shared(ssize_ensemble * disp_unit, disp_unit, MPI_INFO_NULL, & node_comm, baseptr_v, win_ensemble_v, ierr) if (node_rank /= 0) then call MPI_Win_shared_query(win_ensemble_v, 0, ssize_ensemble, disp_unit, baseptr_v, ierr) end if call c_f_pointer(baseptr_v, ensemble_v_ptr, [nx, nz, ny, nsample]) call MPI_Win_allocate_shared(ssize_ensemble * disp_unit, disp_unit, MPI_INFO_NULL, & node_comm, baseptr_th, win_ensemble_th, ierr) if (node_rank /= 0) then call MPI_Win_shared_query(win_ensemble_th, 0, ssize_ensemble, disp_unit, baseptr_th, ierr) end if call c_f_pointer(baseptr_th, ensemble_th_ptr, [nx, nz, ny, nsample]) call MPI_Win_allocate_shared(ssize_ensemble * disp_unit, disp_unit, MPI_INFO_NULL, & node_comm, baseptr_q, win_ensemble_q, ierr) if (node_rank /= 0) then call MPI_Win_shared_query(win_ensemble_q, 0, ssize_ensemble, disp_unit, baseptr_q, ierr) end if call c_f_pointer(baseptr_q, ensemble_q_ptr, [nx, nz, ny, nsample]) ! 分配背景场共享内存 call MPI_Win_allocate_shared(ssize_data * disp_unit, disp_unit, MPI_INFO_NULL, & node_comm, baseptr_back, win_back, ierr) if (node_rank /= 0) then call MPI_Win_shared_query(win_back, 0, ssize_data, disp_unit, baseptr_back, ierr) end if ! call c_f_pointer(baseptr_back, back_data_ptr) call c_f_pointer(baseptr_back, back_data_ptr%pi, [nx, nz, ny]) addr = transfer(baseptr_back, addr) + 1 * nx * nz * ny * disp_unit c_ptr_back = transfer(addr, c_ptr_back) call c_f_pointer(c_ptr_back, back_data_ptr%u, [nx, nz, ny]) addr = transfer(baseptr_back, addr) + 2 * nx * nz * ny * disp_unit c_ptr_back = transfer(addr, c_ptr_back) call c_f_pointer(c_ptr_back, back_data_ptr%v, [nx, nz, ny]) addr = transfer(baseptr_back, addr) + 3 * nx * nz * ny * disp_unit c_ptr_back = transfer(addr, c_ptr_back) call c_f_pointer(c_ptr_back, back_data_ptr%th, [nx, nz, ny]) addr = transfer(baseptr_back, addr) + 4 * nx * nz * ny * disp_unit c_ptr_back = transfer(addr, c_ptr_back) call c_f_pointer(c_ptr_back, back_data_ptr%q, [nx, nz, ny]) ! 分配真值场共享内存 call MPI_Win_allocate_shared(ssize_data * disp_unit, disp_unit, MPI_INFO_NULL, & node_comm, baseptr_true, win_true, ierr) if (node_rank /= 0) then call MPI_Win_shared_query(win_true, 0, ssize_data, disp_unit, baseptr_true, ierr) end if ! call c_f_pointer(baseptr_true, true_data_ptr) call c_f_pointer(baseptr_true, true_data_ptr%pi, [nx, nz, ny]) addr = transfer(baseptr_true, addr) + 1 * nx * nz * ny * disp_unit c_ptr_true = transfer(addr, c_ptr_true) call c_f_pointer(c_ptr_true, true_data_ptr%u, [nx, nz, ny]) addr = transfer(baseptr_true, addr) + 2 * nx * nz * ny * disp_unit c_ptr_true = transfer(addr, c_ptr_true) call c_f_pointer(c_ptr_true, true_data_ptr%v, [nx, nz, ny]) addr = transfer(baseptr_true, addr) + 3 * nx * nz * ny * disp_unit c_ptr_true = transfer(addr, c_ptr_true) call c_f_pointer(c_ptr_true, true_data_ptr%th, [nx, nz, ny]) addr = transfer(baseptr_true, addr) + 4 * nx * nz * ny * disp_unit c_ptr_true = transfer(addr, c_ptr_true) call c_f_pointer(c_ptr_true, true_data_ptr%q, [nx, nz, ny]) end subroutine setup_shared_memory_data (pylk312) [hejx@login04 da_svd]$ cat mylog.e21493684 srun: ROUTE: split_hostlist: hl=a3310n[13-14],b3305r8n8,b3306r1n[1-8],b3306r2n[1-5],b3309r2n[4-8],b3309r3n[1-8],b3309r4n[1-3] tree_width 0 forrtl: severe (174): SIGSEGV, segmentation fault occurred Image PC Routine Line Source libifcoremt.so.5 00002B0FCC1622F6 for__signal_handl Unknown Unknown libpthread-2.17.s 00002B0FCBCD45D0 Unknown Unknown Unknown da_svd.exe 000000000041BD81 da_system_IP_setu 852 NUDT_RFS_DA.F90 da_svd.exe 0000000000402AD6 MAIN__ 152 NUDT_RFS_DA.F90 da_svd.exe 0000000000401D4E Unknown Unknown Unknown libc-2.17.so 00002B0FCDE3A3D5 __libc_start_main Unknown Unknown da_svd.exe 0000000000401C59 Unknown Unknown Unknown forrtl: severe (174): SIGSEGV, segmentation fault occurred Image PC Routine Line Source libifcoremt.so.5 00002B5530B802F6 for__signal_handl Unknown Unknown libpthread-2.17.s 00002B55306F25D0 Unknown Unknown Unknown da_svd.exe 000000000041BD81 da_system_IP_setu 852 NUDT_RFS_DA.F90 da_svd.exe 0000000000402AD6 MAIN__ 152 NUDT_RFS_DA.F90 da_svd.exe 0000000000401D4E Unknown Unknown Unknown libc-2.17.so 00002B55328583D5 __libc_start_main Unknown Unknown da_svd.exe 0000000000401C59 Unknown Unknown Unknown forrtl: severe (174): SIGSEGV, segmentation fault occurred Image PC Routine Line Source libifcoremt.so.5 00002B1B63F5B2F6 for__signal_handl Unknown Unknown libpthread-2.17.s 00002B1B63ACD5D0 Unknown Unknown Unknown da_svd.exe 000000000041BD81 da_system_IP_setu 852 NUDT_RFS_DA.F90 da_svd.exe 0000000000402AD6 MAIN__ 152 NUDT_RFS_DA.F90 da_svd.exe 0000000000401D4E Unknown Unknown Unknown libc-2.17.so 00002B1B65C333D5 __libc_start_main Unknown Unknown da_svd.exe 0000000000401C59 Unknown Unknown Unknown forrtl: severe (174): SIGSEGV, segmentation fault occurred
最新发布
07-22
#include <iostream> #include <conio.h> #define MAX_SIZE 500 #define MAX_NUM 10 typedef struct abc { void* start_addr = nullptr;//起始位置 int _size = 0;//大小 int _state = 0;//存在状态 }DISK; char* disk = (char*)malloc(MAX_SIZE); DISK ary[MAX_NUM]; int offset = 0; int num = 0; //分配空间 void function_1() { int size = 0; char s[100]; printf("大小:"); scanf("%d",&size); printf("内容:"); scanf("%s",s); if (offset + size > MAX_SIZE) { printf("内存不足\n"); return; } strncpy(disk + offset,s,size); offset += size; ary[num].start_addr = disk + offset; ary[num]._size = size; ary[num]._state = 1; num++; printf("已创建\n"); } //释放空间 void function_2() { printf("几号:NO."); int m = 0; scanf("%d",&m); if (m > num || m < 0) { printf("无效的序号\n"); return; } m--; if (ary[m]._state == 0) { printf("重复释放\n"); return; } memset(ary[m].start_addr,0xcd,ary[m]._size); ary[m]._state = 0; ary[m].start_addr = NULL; printf("已释放\n"); } //显示空间 void function_3() { printf("\n"); for (int i = 0; i < num; i++) { if (ary[i]._state == 1) { printf("%dU", ary[i]._size); } else { printf("%dF", ary[i]._size); } } printf("%dF\n",MAX_SIZE - offset); printf("\n"); for (int i = 0; i < num; i++) { printf("NO.%d %p %d ",i + 1,ary[i].start_addr,ary[i]._size); if (ary[i]._state == 1) { printf("正在使用"); } else { printf("已释放"); } printf("\n"); } printf("\n"); } //整理空间 void function_4() { int count = 0; DISK _new[MAX_NUM]; int _newoffset[MAX_NUM]; int _offset = 0; for (int i = 0; i < num; i++) { if (ary[i]._state == 1) { _new[count].start_addr = ary[i].start_addr; _new[count]._state = 1; _new[count]._size = ary[i]._size; for (int j = 0; j < i; j++) { _offset += ary[j]._size; } _newoffset[count] = _offset; count++; } } memcpy(ary,_new,sizeof(ary)); num = count; for (int i = 0; i < num; i ++ ) { offset -= ary[i]._size; } /*int new_free_ptr = 0; for (int i = 0; i < MAX_NUM; i++) { if (ary[i].start_addr != NULL) { memmove(disk + new_free_ptr, ary[i].start_addr, ary[i]._size); ary[i].start_addr = disk + new_free_ptr; new_free_ptr += ary[i]._size; } if (ary[i].start_addr == NULL && ary[i]._state == 1) { ary[i]._state = 0; ary[i]._size = 0; } } offset = new_free_ptr; memset(disk + offset, 0xcd, MAX_SIZE - offset); printf("空间整理完成!\n");*/ } int main() { printf("1,分配空间\n"); printf("2.释放空间\n"); printf("3.显示空间\n"); printf("4.整理空间\n"); printf("5.退出\n"); while (1) { char num = _getch(); switch (num) { case '1': function_1(); break; case '2': function_2(); break; case '3': function_3(); break; case '4': function_4(); break; default: break; } } free(disk); return 0; }
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值