2013-12-15 65 views
0

我試圖掃描程序集中的100號字符串並使用scanf和printf打印它。程序集中的scanf字符串

 .data 
strString: .string "%s" 
stringToRev: .space 100 
.text #the beginning of the code 
.globl main #the label "main" is used to state the initial point of this program 

    .type main, @function # the label "main" representing the beginning of a function 
main: # the main function: 
    pushl %ebp #save the old frame pointer 
    movl %esp, %ebp #create the new frame pointer 
    pushl %ebx #saving a callee save register. 

leal -12(%ebp), %edx  #choice will be saved in edx 
    pushl %edx 
    pushl $strString  
    call scanf 
    movl (%edx), $stringToRev 

    pushl -12(%ebp) 
    pushl $strString 
    call printf 
    call Reverse 

    pushl %eax 
    pushl $strString 
    call printf 

如果我輸入一個字符串,例如「asfdg」,它將打印0(而不是此字符串)。 我對此感到絕望 - 在網上沒有任何答案!

+0

當您通過調試器完成此任務時發現了什麼? –

+0

我是裝配初學者。我在調試器中不流利...... – zvika

+1

然後,你應該花一些時間學習使用調試器。調試可能是編程的50%。 –

回答

0

你的問題是你沒有爲你的字符串分配內存,所以它被放在隨機的地方。我建議你在數據部分爲你的字符串保留一些空間,並將指向這個空間的指針作爲參數push到scanf。

如果您使用的是本地堆棧變量,因爲你暗示這裏:

pushl -12(%ebp) 

然後,還必須保留一些stackspace它,你不作爲可以在這裏看到:

pushl %ebp #save the old frame pointer 
movl %esp, %ebp #create the new frame pointer 
pushl %ebx #saving a callee save register. 

leal -12(%ebp), %edx  #choice will be saved in edx 

您不會爲堆棧中的本地指針保留任何空間。當然,你必須決定它是否應該是一個指針,那麼你仍然必須爲實際的字符串提供一些空間,或者把整個字符串放在堆棧上並在那裏保留足夠的空間。