Assembly 如何将intel x86堆栈中的元素正确弹出到所选寄存器中(在x86中打印整数)
这段代码应该在英特尔x86汇编中打印一个整数;然而,当我运行它时,它不会产生任何输出,我也不知道为什么。另外,我希望有人解释如何正确地从堆栈中检索元素,因为我不太理解代码中(下一个)标签下的逻辑Assembly 如何将intel x86堆栈中的元素正确弹出到所选寄存器中(在x86中打印整数),assembly,integer,stack,x86-64,intel,Assembly,Integer,Stack,X86 64,Intel,这段代码应该在英特尔x86汇编中打印一个整数;然而,当我运行它时,它不会产生任何输出,我也不知道为什么。另外,我希望有人解释如何正确地从堆栈中检索元素,因为我不太理解代码中(下一个)标签下的逻辑 section .data msg db "Hello", 0xa len equ $ - msg section .text global _start _start: mov rax, 947852 ; the number that we are goin
section .data
msg db "Hello", 0xa
len equ $ - msg
section .text
global _start
_start:
mov rax, 947852 ; the number that we are going to print
xor r10, r10 ; basically, makes rsi equal to 0
loop:
xor rdx, rdx ; zeroes the rdx to place the remainder in it
mov rbx, 10 ; what we are going to divide by
div rbx ; div rax by rbx and places the remainder in rdx
add rdx, 48 ; convert it to its ascii equiavlent
push rdx ; push this ascii on the stack
inc r10 ; a counter of the digits in the number we want to print
cmp rax, 0 ;
jz next ; if the number is now zero, then go to the fucntion that would print it in reverse
jmp loop
next:
cmp r10, 0
jz exit
dec r10
mov rax, 4 ; syscall for sys_Write
mov rcx, rsp ;what we want to print
mov rbx, 1 ; stdout as the destination
mov rdx, 1 ; its size
int 0x80 ; interrupt the processor to perform the system call
add rsp, 1
jmp next ; loop again
exit:
mov rax, 1 ; syscall for exiting properly
mov rbx, 0 ; error code 0
int 0x80 ; interrupt to exit
不要在64位代码中使用
int 0x80
系统调用。这可能就是你问题的原因。是的strace
应显示write
返回-EFAULT
mov rcx,rsp
无法用于int 0x80
,因为rsp具有非零高字节。@PeterCordes我用syscalls替换了它们,但仍然存在相同的问题。我还尝试将代码转换为32位,并保留int 0x80,没有任何更改。我认为错误在于我没有从堆栈中弹出我已经正确推过的内容。在这一部分你能帮忙吗?@fuz这实际上不是问题所在,我认为这与正确处理堆栈有关。你可能还有其他错误,但在64位代码中使用int 0x80
绝对是一个好办法。运行strace./my_程序
查看指针指向的内存内容+传递给write
系统调用的长度。64位ABI使用不同的调用号和寄存器,因此,如果您只是用syscall替换int 0x80,那么这当然行不通。