0

我有以下汇编代码:

间接1.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 寄存器:

间接2.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 程序中做错了什么。

4

2 回答 2

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          
于 2013-09-11T12:54:23.263 回答
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

于 2013-09-11T16:22:31.887 回答