2017-09-08 100 views
1

我想解決我得到的練習。任務是從終端讀取10個整數並以相反的順序打印出來。要做到這一點,應該使用堆棧。我已經試過這樣:組裝堆棧 - 反向輸入並打印出

%include "asm_io.inc" 

segment .data 
prompt  db "Please enter a number: ", 0 

segment .text 
    global asm_main 

asm_main: 
    enter 0,0 
    pusha 

    mov   ecx, 10   ; for loop counter 

for_loop: 
    mov   eax, prompt 
    call  print_string ; print prompt 
    call  read_int  ; read input 
    push  dword eax  ; push input to stack 
    loop  for_loop 

    mov   ecx, 10   ; set loop counter for output 

swap_loop: 
    pop   eax    ; get last input from stack 
    call  print_int 
    call  print_nl 
    add   esp, 4   ; increment esp for next value to take from stack 
    loop  swap_loop 

    popa 
    mov   eax, 0 
    leave 
    ret 

當我執行該程序,並從1到10輸入所有數字依次我得到以下結果:

    Should Be: 
10     10 
8     9 
6     8 
4     7 
2     6 
-1217249280   5 
-1079315368   4 
0     3 
-1079315312   2 
-1079315336   1 

回答

3

只需刪除線add esp, 4
堆棧指針已經增加了pop eax