Chinaunix首页 | 论坛 | 博客
  • 博客访问: 999967
  • 博文数量: 633
  • 博客积分: 30780
  • 博客等级: 大将
  • 技术积分: 7532
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-12 21:07
文章分类

全部博文(633)

文章存档

2011年(10)

2010年(500)

2009年(47)

2008年(76)

我的朋友

分类:

2011-03-06 01:38:30

1. 使用变址的内存位置:

.section .data
output:
        .asciz "The value is %d\n"
values:
        .int 10,15,20,25,30,35,40,45,50,55,60

.section .text
.global _start
_start:
        nop
        movl $0, %edi
loop:
        movl values(,%edi,4), %eax
        pushl %eax
        pushl $output
        call printf
        addl $8, %esp
        inc %edi
        cmpl $11, %edi
        jne loop
        movl $0, %ebx
        movl $1, %eax
        int $0x80

注意上面的代码
movl values(,%edi,4), %eax
表达格式是:base_addr(offset_addr, index, size)
获取的位置等于:
base_addr + offset_addr + index * size
如果其中的任何值为0,就可以忽略他们(但是仍然需要用逗号作为占位符)。offset_addr和index的值必须是寄存器,但size的值可以是数字值。

我们再看另外的例子:

.section .data
values:
        .int 10,15,20,25,30,35,40,45,50,55,60

.section .text
.global _start
_start:
        nop
        movl values, %eax
        movl $values, %edi
        movl $100, 4(%edi)
        movl $1, %edi
        movl values(,%edi,4), %ebx
        movl $1, %eax
        int $0x80

注意这里的
movl values, %eax
movl $values, %edi
第一个values,前面没有$符号,表示是引用内存values里面的值;
第二个values,前面有$符号,表示是引用符号values所在的地址。
这两者之间的区别,类似于C语言里的指针指向的内容,与指针本身的。
我们用gdb单步一下看看

[root@localhost assembly_study]# as -o movtest4.o -gstabs movtest4.s
[root@localhost assembly_study]# ld -o movtest4 movtest4.o
[root@localhost assembly_study]# gdb movtest4
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...
(gdb) break *_start+1
Breakpoint 1 at 0x8048075: file movtest4.s, line 9.
(gdb) list
1       .section .data
2       values:
3               .int 10,15,20,25,30,35,40,45,50,55,60
4
5       .section .text
6       .global _start
7       _start:
8               nop
9               movl values, %eax
10              movl $values, %edi
(gdb) list
11              movl $100, 4(%edi)
12              movl $1, %edi
13              movl values(,%edi,4), %ebx
14              movl $1, %eax
15              int $0x80
(gdb) run
Starting program: /home/assembly_study/movtest4

Breakpoint 1,
Current language:  auto; currently asm
(gdb) x/d $values
Value can't be converted to integer.
(gdb) x/4d &values
0x804909c :     10      15      20      25
(gdb) s
10              movl $values, %edi
(gdb) info registers
eax            0xa      10
ecx            0x0      0
edx            0x0      0
ebx            0x0      0
esp            0xbfffdd90       0xbfffdd90
ebp            0x0      0x0
esi            0x0      0
edi            0x0      0
eip            0x804807a        0x804807a
eflags         0x306    774
cs             0x23     35
ss             0x2b     43
ds             0x2b     43
es             0x2b     43
fs             0x0      0
gs             0x0      0
(gdb) x &values
0x804909c :     10
(gdb) x/4d &values
0x804909c :     10      15      20      25
(gdb) x/10d &values
0x804909c :     10      15      20      25
0x80490ac :  30      35      40      45
0x80490bc :  50      55
(gdb) x/20d &values
0x804909c :     10      15      20      25
0x80490ac :  30      35      40      45
0x80490bc :  50      55      60      0
0x80490cc:      0       0       0       0
0x80490dc:      0       0       0       0
(gdb) s
11              movl $100, 4(%edi)
(gdb) x/4d &values
0x804909c :     10      15      20      25
(gdb) list
6       .global _start
7       _start:
8               nop
9               movl values, %eax
10              movl $values, %edi
11              movl $100, 4(%edi)
12              movl $1, %edi
13              movl values(,%edi,4), %ebx
14              movl $1, %eax
15              int $0x80
(gdb) list
Line number 16 out of range; movtest4.s has 15 lines.
(gdb) info registers
eax            0xa      10
ecx            0x0      0
edx            0x0      0
ebx            0x0      0
esp            0xbfffdd90       0xbfffdd90
ebp            0x0      0x0
esi            0x0      0
edi            0x804909c        134516892
eip            0x804807f        0x804807f
eflags         0x306    774
cs             0x23     35
ss             0x2b     43
ds             0x2b     43
es             0x2b     43
fs             0x0      0
gs             0x0      0
(gdb) s
12              movl $1, %edi
(gdb) x/4d &values
0x804909c :     10      100     20      25
(gdb) s
13              movl values(,%edi,4), %ebx
(gdb) info registers
eax            0xa      10
ecx            0x0      0
edx            0x0      0
ebx            0x0      0
esp            0xbfffdd90       0xbfffdd90
ebp            0x0      0x0
esi            0x0      0
edi            0x1      1
eip            0x804808b        0x804808b
eflags         0x306    774
cs             0x23     35
ss             0x2b     43
ds             0x2b     43
es             0x2b     43
fs             0x0      0
gs             0x0      0
(gdb) s
14              movl $1, %eax
(gdb) info resigers
Undefined info command: "resigers".  Try "help info".
(gdb) info registers
eax            0xa      10
ecx            0x0      0
edx            0x0      0
ebx            0x64     100
esp            0xbfffdd90       0xbfffdd90
ebp            0x0      0x0
esi            0x0      0
edi            0x1      1
eip            0x8048092        0x8048092
eflags         0x306    774
cs             0x23     35
ss             0x2b     43
ds             0x2b     43
es             0x2b     43
fs             0x0      0
gs             0x0      0
(gdb) print $eax
$1 = 10
(gdb)

阅读(451) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~