mov (%rax) %rax
寄存器中%rax所保存的内存地址对应位置的值加载进%rax
mov -8(%rbp) rax
将%rbp内存所保存的值减8作为内存地址,喷对应的内存地址中的值进%rax
movq (%rcx,%rax,8) %rax
将R[%rcx]+R[%rax]*8对应的地址的值加载进%rax
有效地址=offset(%base,%index,scale)=偏移量(基数,索引,倍数)
offset,scale必须是常量
%base,%index必须是寄存器
scale必须是1,2,4,8
CPU在编码阶段就可以做简单的计算逻辑。
1)源代码
#include <stdio.h>
void main()
{
int ss=0;
for(int i=1;i<100;i++)
{
ss=ss+i;
}
printf("%d\n",ss);
}
2)gcc -S -O0 b6.array.c
得到:
.file "b6.array.c"
.text
.section .rodata
.LC0:
.string "%d\n"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $432, %rsp
movq %fs:40, %rax
movq %rax, -8(%rbp)
xorl %eax, %eax
movl $1116, %edi
movl $0, %eax
call srand@PLT
movl $0, -428(%rbp)
movl $0, -424(%rbp)
jmp .L2
.L3:
movl $0, %eax
call rand@PLT
movl %eax, %ecx
movl $1717986919, %edx
movl %ecx, %eax
imull %edx
sarl $2, %edx
movl %ecx, %eax
sarl $31, %eax
subl %eax, %edx
movl %edx, %eax
sall $2, %eax
addl %edx, %eax
addl %eax, %eax
subl %eax, %ecx
movl %ecx, %edx
movl -424(%rbp), %eax
cltq
movl %edx, -416(%rbp,%rax,4)
addl $1, -424(%rbp)
.L2:
cmpl $99, -424(%rbp)
jle .L3
movl $0, -420(%rbp)
jmp .L4
.L5:
movl -420(%rbp), %eax
cltq
movl -416(%rbp,%rax,4), %eax
addl %eax, -428(%rbp)
addl $1, -420(%rbp)
.L4:
cmpl $99, -420(%rbp)
jle .L5
movl -428(%rbp), %eax
movl %eax, %esi
leaq .LC0(%rip), %rdi
movl $0, %eax
call printf@PLT
nop
movq -8(%rbp), %rax
xorq %fs:40, %rax
je .L6
call __stack_chk_fail@PLT
.L6:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0"
.section .note.GNU-stack,"",@progbits
4)
gcc -g b6.array.s -o a.out
5)
gdb a.out
(gdb) l
1 .file "b6.array.c"
2 .text
3 .section .rodata
4 .LC0:
5 .string "%d\n"
6 .text
7 .globl main
8 .type main, @function
9 main:
10 .LFB0:
(gdb) b 13
打印值:
p $rax
(gdb) p $rbp
$18 = (void *) 0x7ffffffedd50
(gdb) x /4f 0x7ffffffedd50
0x7ffffffedd50: 3.85282232e-34 0 -1.72942894e+38 4.59163468e-41
p /x (char[30])*buf
6)大概知道怎么打印值了,就是16进制的加减法还不会。