Assembly call subprograms based on user input

隐身守侯 提交于 2019-12-02 13:31:35

cmp %esp, '0' is wrong, because it tries to compare the value of %esp to the value in memory at address '0'. At&t syntax uses reversed operands, and it needs a $ prefix for immediates. But you already know this, I guess you were just a little careless. The correct instruction is cmpb $'0', (%esp) to compare the byte in memory at address %esp to the ascii code of 0.

Furthermore, you allocated 4 bytes from the stack, but you never free that. When you eventually hit a ret it will use your local variable as return address which is of course a bad thing :) A nice trick is to use lea 4(%esp), %esp to free it without affecting the flags, so you can do this between the cmp and the jz. If you like less tricky stuff, you can of course just pop the input into a register and use that in the comparison, such as:

pop %eax
cmp $'0', %al

PS: Learn to use a debugger. That would have pointed you directly at the instruction, and then you probably could have figured out the problem yourself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!