为什么打印时出现分段错误? [英] why is segmention fault while printing?

查看:48
本文介绍了为什么打印时出现分段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 x86 汇编代码:

This is my x86 assembly code:

section .data 
  output db '%d',10,0
section .text 
  global main
  extern printf 
main :
  xor ecx,ecx
  xor eax,eax
  mov eax,1
  mov ecx,5
lable1:
  push ecx 
  push eax  
  cmp eax,0
  jg print
  pop eax
  pop ecx
  inc eax
loop lable1 
  ret 
print:
  push eax
  push output
  call printf 
  add esp,8
  ret 

这个程序应该打印 1 到 5 之间的所有数字.为什么我在打印1"后会出现分段错误?

This program should print all numbers between 1 to 5. Why am I getting a segmentation fault after printing '1'?

推荐答案

printret 指令结尾,这意味着你应该调用.所以 jg print 应该是 jng skip/call print/skip: (或者只是 call print,因为 > 0 检查似乎没有必要.
call 将返回地址放在堆栈中,jg 不会.

print ends with a ret instruction, which implies that it is something that you should call. So jg print should be jng skip / call print / skip: (or just call print, because the > 0 check seems unnecessary).
call places the return address on the stack, jg does not.

这篇关于为什么打印时出现分段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆