在64位Linux上使用中断0x80

Ale*_*der 6 linux assembly stack x86-64 system-calls

我有一个简单的64位汇编程序,用于打印'O'和'K'后跟换行符.但是,'K'永远不会打印出来.程序的目标之一是将rax寄存器的低位中的值打印为ASCII字母.该程序专门用于64位Linux,用于教育目的,因此不需要使用C风格的系统调用.

我怀疑这个问题要么mov QWORD [rsp], rax或是mov rcx, rsp.

目前,该程序仅输出"O"后跟换行符.

如何更改程序以使其使用rax中的值然后打印"K"以使完整输出为"OK",然后换行?

bits 64

section .data

o:  db "O"      ; 'O'
nl: dq 10       ; newline

section .text

;--- function main ---
global main         ; make label available to the linker
global _start       ; make label available to the linker
_start:             ; starting point of the program
main:               ; name of the function

;--- call interrupt 0x80 ---
mov rax, 4          ; function call: 4
mov rbx, 1          ; parameter #1 is 1
mov rcx, o          ; parameter #2 is &o
mov rdx, 1          ; parameter #3 is length of string
int 0x80            ; perform the call

;--- rax = 'K' ---
mov rax, 75         ; rax = 75

;--- call interrupt 0x80 ---
sub rsp, 8          ; make some space for storing rax on the stack
mov QWORD [rsp], rax        ; move rax to a memory location on the stack
mov rax, 4          ; function call: 4
mov rbx, 1          ; parameter #1 is 1
mov rcx, rsp            ; parameter #2 is rsp
mov rdx, 1          ; parameter #3 is length of string
int 0x80            ; perform the call
add rsp, 8          ; move the stack pointer back

;--- call interrupt 0x80 ---
mov rax, 4          ; function call: 4
mov rbx, 1          ; parameter #1 is 1
mov rcx, nl         ; parameter #2 is nl
mov rdx, 1          ; parameter #3 is length of string
int 0x80            ; perform the call

;--- exit program ---
mov rax, 1          ; function call: 1
xor rbx, rbx            ; return code 0
int 0x80            ; exit program
Run Code Online (Sandbox Code Playgroud)

Mar*_*nau 6

显然,你编写了一个64位程序,并使用"int 0x80"指令."int 0x80"但只能在32位程序中正常工作.

堆栈的地址在32位程序无法访问的范围内.因此,"int 0x80"式系统调用很可能不允许访问此内存区域.

要解决这个问题,有两种可能:

  • 编译为32位应用程序(使用32位寄存器,如EAX而不是像RAX这样的64位寄存器).在不使用任何共享库的情况下进行链接时,32位程序将在64位Linux上完美运行.
  • 使用"syscall"式系统调用而不是"int 0x80"式系统调用.这些的使用与"int 0x80"式的有很大不同!

32位代码:

mov eax,4    ; In "int 0x80" style 4 means: write
mov ebx,1    ; ... and the first arg. is stored in ebx
mov ecx,esp  ; ... and the second arg. is stored in ecx
mov edx,1    ; ... and the third arg. is stored in edx
int 0x80
Run Code Online (Sandbox Code Playgroud)

64位代码:

mov rax,1    ; In "syscall" style 1 means: write
mov rdi,1    ; ... and the first arg. is stored in rdi (not rbx)
mov rsi,rsp  ; ... and the second arg. is stored in rsi (not rcx)
mov rdx,1    ; ... and the third arg. is stored in rdx
syscall
Run Code Online (Sandbox Code Playgroud)

---编辑---

背景资料:

"int 0x80"适用于32位程序.从64位程序调用时,它的行为方式与从32位程序调用时的行为方式相同(使用32位调用约定).

这也意味着"int 0x80"的参数将在32位寄存器中传递,而64位寄存器的高32位将被忽略.

(我刚刚在Ubuntu 16.10,64位上测试过.)

但是,这意味着当使用"int 0x80"时,您只能访问低于2 ^ 32(甚至低于2 ^ 31)的内存,因为您无法在32位寄存器中传递高于2 ^ 32的地址.

如果要写入的数据位于低于2 ^ 31的地址,则可以使用"int 0x80"来写入数据.如果它位于2 ^ 32以上则不能.堆栈(RSP)很可能位于2 ^ 32之上,因此您无法使用"int 0x80"在堆栈上写入数据.

因为你的程序很可能会使用2 ^ 32以上的内存,所以我写道:"int 0x80不适用于64位程序."

  • `int 0x80`在64位模式下工作正常,但你仍然必须遵循64位调用约定(要使用的寄存器和系统调用号). (2认同)