为什么我不能成功地在堆栈上推送变量内容?

时间:2016-04-07 08:59:46

标签: linux assembly x86 nasm

section .data
msg db 'enter number ',10,0
output db '%hd',0

section .bss
val resw 1

section .text
global main

extern printf 
extern scanf

main :
  push msg 
  call printf 
  add esp,4
  push val  
  push output
  call scanf 
  add esp,8
  ;movzx eax,word[val]
  ;push eax
  push word[val]
  push output
  call printf 
  add esp,8
ret

1 个答案:

答案 0 :(得分:2)

push word[val]

This only pushes a word and you need to have a dword on the stack.
You can do it like:

xor ax,ax
push ax           ;Gives a high word of zero, much like your MOVZX did.
push word[val]

Your segmentation fault comes from the fact that you pushed a total of 6 bytes but removed a total of 8 bytes through add esp,8.