本文介绍了x86汇编pushl / popl不&QUOT工作;错误:后缀或操作数无效"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,以汇编编程,通过与GNU汇编一个Ubuntu x86_64的桌面上工作v2.20.1。

我已经能够组装/链接执行我的code,直到我可以使用操纵堆栈pushl / popl指令。下面code未能组装:

  .section伪。数据#空 .section伪的.text
.globl _start
_开始:
 pushl $ 1#推值1到堆栈
 popl%EAX#流行1从堆栈到%EAX寄存器
 诠释$#0x80的退出,退出code计划'1'

使用为test.s -o test.o,这些错误会显示在终端上和test.o不会创建:

  test.s:汇编的消息:
test.s:9:错误:后缀或操作数'推'无效
test.s:10:错误:后缀或操作数popl'无效

我检查的文件,而我使用pushl和popl操作数是有效的。这不完全是一个调试的问题 - 所以有什么错我的code?抑或是我的汇编程序?


解决方案

在64位模式下,你不能推及弹出32位值;你需要 pushq popq

此外,你不会得到一个适当的退出这种方式。在32位x86,您需要设置%EAX 1,选择退出()系统调用, 的设置%EBX 来退出code你真的希望。在64位x86(也就是你使用的是什么),公约是不同的:对于系统调用号出口() 60,不是1;第一个系统调用的参数会%RDI ,而不是%RBX ;该系统调用调用运code不是 INT 0x80的$ ,但特殊的,X86-64,仅OP code 系统调用

这导致了:

  .section伪。数据
.section伪的.text
.globl _start
_开始:
    pushq $ 60
    popq%RAX
    pushq $ 1
    popq%RDI
    系统调用

(每个 / 弹出序列可以用一个简单的 MOV ,当然,我想你正试图明确测试弹出

I'm a newbie to assembly programming, working through Programming Ground Up on an Ubuntu x86_64 desktop with GNU assembler v2.20.1.

I've been able to assemble/link execute my code, up until I get to using pushl/popl instructions for manipulating the stack. The following code fails to assemble:

 .section .data  # empty

 .section .text
.globl _start
_start:
 pushl $1       # push the value 1 onto the stack
 popl %eax      # pop 1 off the stack and into the %eax register
 int $0x80      # exit the program with exit code '1'

Using "as test.s -o test.o", these errors appear on the terminal and test.o is not created:

test.s: Assembler messages:
test.s:9: Error: suffix or operands invalid for 'push'
test.s:10:  Error: suffix or operands invalid for 'popl'

I've checked the documentation, and the operands I'm using for pushl and popl are valid. This isn't exactly a debugging question--so what's wrong with my code? Or is it my assembler?

解决方案

In 64-bit mode you cannot push and pop 32-bit values; you need pushq and popq.

Also, you will not get a proper exit this way. On 32-bit x86, you would need to set %eax to 1 to select the exit() system call, and set %ebx to the exit code you actually wish. On 64-bit x86 (that's what you are using), conventions are different: the system call number for exit() is 60, not 1; the first system call parameter goes in %rdi, not %rbx; the system-call invocation opcode is not int $0x80 but the special, x86-64-only opcode syscall.

Which leads to:

.section .data
.section .text
.globl _start
_start:
    pushq   $60
    popq    %rax
    pushq   $1
    popq    %rdi
    syscall

(each push/pop sequence can be replaced with a simple mov, of course; I suppose that you are trying to explicitly test push and pop.)

这篇关于x86汇编pushl / popl不&QUOT工作;错误:后缀或操作数无效"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:40