无法使用间接寻址模式将值存储在存储器中

时间:2013-09-11 10:49:17

标签: assembly x86 att

我有一个下面的汇编代码:

indirect1.s

.section .data
t1: 
.long 5
.section .text
.globl _start
_start: 
movl $t1, %ecx          #we are passing the address to %ecx
movl $5, %eax           #we are passing value 5 to %eax
movl (%ecx), %ebx   #Using indirect addressing mode we are getting the value from t1 and passing it to ebx
addl %eax, %ebx     # add the values in %eax, %ebx and store it in %ebx
movl $1, %eax       # call exit program
int $0x80       # Call Master Bruce Wayne

运行上述程序时,我按预期得到值10

[ashok@localhost asm-32]$ as indirect1.s -gstabs+ -o indirect1.o
[ashok@localhost asm-32]$ ld indirect1.o -o indirect1
[ashok@localhost asm-32]$ ./indirect1 
[ashok@localhost asm-32]$ echo $?
10

修改上述程序以消除%ecx寄存器:

indirect2.s

.section .data
t1: 
.long 5
.section .text
.globl _start
_start: 
    movl $t1, %ebx      # we are passing the address to %ebx
    movl $5, %eax       # we are passing value 5 to %eax
    addl %eax, (%ebx)   # add the values in %eax, %ebx and store it in %ebx
    movl $1, %eax       # call exit program
    int $0x80       # Call Master Bruce Wayne

当我运行上述程序时,我没有得到预期的输出,即10和我似乎 获取存储在%ebx

中的地址
[ashok@localhost asm-32]$ as indirect2.s -gstabs+ -o indirect2.o
[ashok@localhost asm-32]$ ld indirect2.o -o indirect2
[ashok@localhost asm-32]$ ./indirect2
[ashok@localhost asm-32]$ echo $?
136

我在indirect2.s程序中做错了什么。

2 个答案:

答案 0 :(得分:1)

我认为你想要的是这样的:

movl $t1, %ebx      # ebx = address of t1
movl $5, %eax       # eax = 5
addl (%ebx), %eax   # eax += (ebx)
movl %eax, %ebx     # exit value
movl $1, %eax       # exit()
int $0x80          

答案 1 :(得分:1)

或者,让你的第二个例子起作用:

.section .data
t1: 
.long 5
.section .text
.globl _start
_start: 
    movl $t1, %ebx      # we are passing the address to %ebx
    movl $5, %eax       # we are passing value 5 to %eax
    addl %eax, (%ebx)   # add the values in %eax, %ebx and store it in %ebx
    movl (%ebx), %ebx   # THE FORGOTTEN INSTRUCTION (read result back into %ebx)
    movl $1, %eax       # call exit program
    int $0x80       # Call Master Bruce Wayne

您的初始版本的indirect2打印出$t1的相对地址,这是%ebx程序退出时的内容。