簡體   English   中英

無法使用間接尋址模式將值存儲在存儲器中

[英]Unable to get value stored in memory using Indirect addressing mode

我有一個下面的匯編代碼:

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程序中做錯了什么。

我想你想要的是這樣的:

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          

或者,讓你的第二個例子工作:

.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的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM