如果使用纯 x86 程序集,您的意思是不必包含/链接任何第 3 方库,那么可以,您可以使用内核服务 write 打印到控制台。我不认为它比使用 C 库更不正确。我将以下以 UTF-8 编码的示例保存为“sample.asm”,然后组装、链接并运行
nasm -f ELF32 sample.asm -o sample.o
ld sample.o -o sample -m elf_i386
./sample
它按预期工作。
SEGMENT .data
Sample DB "Sample text of mixed alphabets:",10
DB "Éireannach (Eireannach in western European alphabet)",10
DB "Čapek (Capek in central European alphabet)",10
DB "Ørsted (Oersted in Nordic alphabet)",10
DB "Aukštaičių (Aukshtaiciu in Baltic alphabet)",10
DB "Ὅμηρος (Homer in Greek alphabet)",10
DB "Yumuşak ğ (Yumushak g in Turkish aplhabet)",10
DB "Maðkur (Mathkur in Icelandic alphabet)",10
DB "דגבא (ABGD in Hebrew alphabet)",10
DB "Достоевский (Dostoevsky in Cyrillic alphabet)",10
DB 0
SizeOfSample EQU $ - Sample
SEGMENT .text
GLOBAL _start:
_start:MOV EAX,4 ; Kernel function sys_write in 32bit mode.
MOV EBX,1 ; File descriptor of standard output.
MOV ECX,Sample ; Offset of the written text.
MOV EDX,SizeOfSample ; Number of bytes (not characters).
INT 0x80 ; Invoke kernel service.
MOV EAX,1 ; Kernel function sys_exit in 32bit mode.
INT 0x80 ; Invoke kernel service.
以下是 64 位模式下的相同示例:
SEGMENT .data
Sample DB "Sample text of mixed alphabets:",10
DB "Éireannach (Eireannach in western European alphabet)",10
DB "Čapek (Capek in central European alphabet)",10
DB "Ørsted (Oersted in Nordic alphabet)",10
DB "Aukštaičių (Aukshtaiciu in Baltic alphabet)",10
DB "Ὅμηρος (Homer in Greek alphabet)",10
DB "Yumuşak ğ (Yumushak g in Turkish aplhabet)",10
DB "Maðkur (Mathkur in Icelandic alphabet)",10
DB "דגבא (ABGD in Hebrew alphabet)",10
DB "Достоевский (Dostoevsky in Cyrillic alphabet)",10
DB 0
SizeOfSample EQU $ - Sample
SEGMENT .text
GLOBAL _start:
_start:MOV RAX,1 ; Kernel function sys_write in 64bit mode.
MOV RDI,1 ; File descriptor of standard output.
LEA RSI,[Sample] ; Offset of the written text.
MOV RDX,SizeOfSample ; Number of bytes (not characters).
SYSCALL ; Invoke kernel service.
MOV EAX,60 ; Kernel function sys_exit in 64bit mode.
SYSCALL ; Invoke kernel service.
创建于
nasm -f ELF64 sample.asm -o sample.o
ld sample.o -o sample
./sample