<分区>
我有一个简单的 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
更新:请注意,这是一个使用 int 80h 的 64 位 x86 汇编程序,与使用 int 80h 的 32 位 x86 汇编程序有很大不同。