交流
商城
MCN
登入
注册
首页
提问
分享
讨论
建议
公告
动态
发表新帖
发表新帖
7 指针
分享
未结
0
891
李延
LV6
2022-06-07
悬赏:20积分
# 1 指针 指针使用的是leaq指令 ```c #include <stdio.h> int print(int *q) { printf("leaq %llu\n", q); printf("value %d\n", *q); return 0; } int main() { int q = 55; print(&q); return 0; } ``` ```asm .file "leaq.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "leaq %llu\n" .LC1: .string "value %d\n" .text .globl print .type print, @function print: .LFB11: .cfi_startproc pushq %rbx .cfi_def_cfa_offset 16 .cfi_offset 3, -16 movq %rdi, %rbx movq %rdi, %rsi movl $.LC0, %edi movl $0, %eax call printf movl (%rbx), %esi movl $.LC1, %edi movl $0, %eax call printf movl $0, %eax popq %rbx .cfi_def_cfa_offset 8 ret .cfi_endproc .LFE11: .size print, .-print .globl main .type main, @function main: .LFB12: .cfi_startproc subq $24, %rsp .cfi_def_cfa_offset 32 movl $55, 12(%rsp) leaq 12(%rsp), %rdi call print movl $0, %eax addq $24, %rsp .cfi_def_cfa_offset 8 ret .cfi_endproc .LFE12: .size main, .-main .ident "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-44)" .section .note.GNU-stack,"",@progbits ``` # 2 解析 这里我们主要关注3点 1. &q 获取 变量q的地址,对应汇编 ```asm leaq 12(%rsp), %rdi ``` 通过leaq 将地址加载到rdi中 2. printf("leaq %llu\n", q); 这里我们没有加指针前面的* ,是直接打印的地址,我们看到 ```asm movq %rdi, %rsi ``` 3. printf("value %d\n", *q); 这里打印的是 *q指向的值, ```asm movl (%rbx), %esi ```
回帖
消灭零回复
提交回复
热议榜
java 相关知识分享
8
好的程序员与不好的程序员
6
写给工程师的十条精进原则
5
spring boot以jar包运行配置的logback日志文件没生成
5
一步一步分析SpringBoot启动源码(一)
5
MockMvc测试
5
【吐槽向】是不是有个吐槽的板块比较好玩
4
logstash jdbc同步mysql多表数据到elasticsearch
3
IntelliJ IDEA 优质License Server
3
.gitignore忽略规则
3
SpringBoot启动源码分析
3
一步一步分析SpringBoot启动源码(三)
3
2
一步一步分析SpringBoot启动源码(二)
2
积分不够将无法发表新帖
2
官方产品
Meta-Boot - 基于MCN
MCN - 快速构建SpringBoot应用
微信扫码关注公众号